public function __construct(Generator $generator)
{
parent::__construct();
$this->generator = $generator;
/**
* @param mixed $value The value to send to the generator.
*/
$this->send = function ($value = null) {
if ($this->busy) {
Loop\queue($this->send, $value);
// Queue continuation to avoid blowing up call stack.
return;
}
try {
// Send the new value and execute to next yield statement.
$this->next($this->generator->send($value));
} catch (Throwable $exception) {
$this->reject($exception);
}
};
/**
* @param \Throwable $exception Exception to be thrown into the generator.
*/
$this->capture = function (Throwable $exception) {
if ($this->busy) {
Loop\queue($this->capture, $exception);
// Queue continuation to avoid blowing up call stack.
return;
}
try {
// Throw exception at current execution point.
$this->next($this->generator->throw($exception));
} catch (Throwable $exception) {
$this->reject($exception);
}
};
try {
$this->next($this->generator->current());
} catch (Throwable $exception) {
$this->reject($exception);
}
}