2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 18:47:14 +02:00
|
|
|
namespace Amp;
|
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Emitter is a container for an iterator that can emit values using the emit() method and completed using the
|
|
|
|
* complete() and fail() methods of this object. The contained iterator may be accessed using the iterate()
|
|
|
|
* method. This object should not be part of a public API, but used internally to create and emit values to an
|
|
|
|
* iterator.
|
|
|
|
*/
|
|
|
|
final class Emitter {
|
2017-12-20 17:30:43 +01:00
|
|
|
/** @var object Has public emit, complete, and fail methods. */
|
|
|
|
private $emitter;
|
|
|
|
|
|
|
|
/** @var \Amp\Iterator Hides producer methods. */
|
2017-12-03 02:07:46 +01:00
|
|
|
private $iterator;
|
|
|
|
|
|
|
|
public function __construct() {
|
2017-12-20 17:30:43 +01:00
|
|
|
$this->emitter = new class {
|
2017-12-03 02:07:46 +01:00
|
|
|
use Internal\Producer {
|
|
|
|
emit as public;
|
|
|
|
complete as public;
|
|
|
|
fail as public;
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|
2017-12-03 02:07:46 +01:00
|
|
|
};
|
2017-12-20 17:30:43 +01:00
|
|
|
|
|
|
|
$this->iterator = new class($this->emitter) implements Iterator {
|
|
|
|
/** @var \Amp\Iterator */
|
|
|
|
private $iterator;
|
|
|
|
public function __construct($iterator) {
|
|
|
|
$this->iterator = $iterator;
|
|
|
|
}
|
|
|
|
public function advance(): Promise {
|
|
|
|
return $this->iterator->advance();
|
|
|
|
}
|
|
|
|
public function getCurrent() {
|
|
|
|
return $this->iterator->getCurrent();
|
|
|
|
}
|
|
|
|
};
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
|
|
|
public function iterate(): Iterator {
|
|
|
|
return $this->iterator;
|
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Emits a value to the iterator.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
|
|
|
public function emit($value): Promise {
|
2017-12-20 17:30:43 +01:00
|
|
|
return $this->emitter->emit($value);
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Completes the iterator.
|
|
|
|
*/
|
|
|
|
public function complete() {
|
2017-12-20 17:30:43 +01:00
|
|
|
$this->emitter->complete();
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2016-06-01 18:19:19 +02:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Fails the iterator with the given reason.
|
|
|
|
*
|
|
|
|
* @param \Throwable $reason
|
|
|
|
*/
|
|
|
|
public function fail(\Throwable $reason) {
|
2017-12-20 17:30:43 +01:00
|
|
|
$this->emitter->fail($reason);
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|