2020-05-13 17:15:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TValue
|
|
|
|
* @template TSend
|
|
|
|
* @template TReturn
|
|
|
|
*/
|
2020-08-23 16:18:28 +02:00
|
|
|
final class AsyncGenerator implements Pipeline
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-28 19:59:55 +02:00
|
|
|
/** @var Internal\EmitSource<TValue, TSend> */
|
2020-05-19 17:49:40 +02:00
|
|
|
private $source;
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-07-17 18:19:36 +02:00
|
|
|
/** @var Coroutine<TReturn>|null */
|
2020-05-13 17:15:21 +02:00
|
|
|
private $coroutine;
|
|
|
|
|
2020-07-16 20:43:11 +02:00
|
|
|
/** @var \Generator|null */
|
|
|
|
private $generator;
|
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
/**
|
|
|
|
* @param callable(callable(TValue):Promise<TSend>):\Generator $callable
|
|
|
|
*
|
2020-05-19 17:49:40 +02:00
|
|
|
* @throws \Error Thrown if the callable throws any exception.
|
2020-05-19 00:01:14 +02:00
|
|
|
* @throws \TypeError Thrown if the callable does not return a Generator.
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
|
|
|
public function __construct(callable $callable)
|
|
|
|
{
|
2020-05-28 19:59:55 +02:00
|
|
|
$this->source = $source = new Internal\EmitSource;
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
if (\PHP_VERSION_ID < 70100) {
|
2020-05-28 19:59:55 +02:00
|
|
|
$emit = static function ($value) use ($source): Promise {
|
|
|
|
return $source->emit($value);
|
2020-05-13 17:15:21 +02:00
|
|
|
};
|
|
|
|
} else {
|
2020-05-28 19:59:55 +02:00
|
|
|
$emit = \Closure::fromCallable([$source, "emit"]);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 17:49:40 +02:00
|
|
|
try {
|
2020-07-16 20:43:11 +02:00
|
|
|
$this->generator = $callable($emit);
|
2020-05-19 17:49:40 +02:00
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
throw new \Error("The callable threw an exception", 0, $exception);
|
|
|
|
}
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-07-16 20:43:11 +02:00
|
|
|
if (!$this->generator instanceof \Generator) {
|
2020-05-13 17:15:21 +02:00
|
|
|
throw new \TypeError("The callable did not return a Generator");
|
|
|
|
}
|
2020-05-19 17:49:40 +02:00
|
|
|
}
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-19 17:49:40 +02:00
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->source->dispose();
|
2020-05-13 23:48:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
/**
|
2020-05-14 21:14:54 +02:00
|
|
|
* @inheritDoc
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
|
|
|
public function continue(): Promise
|
|
|
|
{
|
2020-07-16 20:43:11 +02:00
|
|
|
if ($this->coroutine === null) {
|
|
|
|
$this->getReturn(); // Starts execution of the coroutine.
|
|
|
|
}
|
|
|
|
|
2020-05-19 17:49:40 +02:00
|
|
|
return $this->source->continue();
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a value to the async generator, resolving the back-pressure promise with the given value.
|
2020-05-28 19:59:55 +02:00
|
|
|
* The first emitted value must be retrieved using {@see continue()}.
|
2020-05-13 17:15:21 +02:00
|
|
|
*
|
|
|
|
* @param mixed $value Value to send to the async generator.
|
|
|
|
*
|
|
|
|
* @psalm-param TSend $value
|
|
|
|
*
|
2020-08-23 16:18:28 +02:00
|
|
|
* @return Promise<mixed|null> Resolves with null if the pipeline has completed.
|
2020-05-18 20:49:56 +02:00
|
|
|
*
|
2020-05-21 17:11:22 +02:00
|
|
|
* @psalm-return Promise<TValue|null>
|
2020-05-19 00:01:14 +02:00
|
|
|
*
|
2020-05-28 19:59:55 +02:00
|
|
|
* @throws \Error If the first emitted value has not been retrieved using {@see continue()}.
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
|
|
|
public function send($value): Promise
|
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
return $this->source->send($value);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws an exception into the async generator, failing the back-pressure promise with the given exception.
|
2020-05-28 19:59:55 +02:00
|
|
|
* The first emitted value must be retrieved using {@see continue()}.
|
2020-05-13 17:15:21 +02:00
|
|
|
*
|
|
|
|
* @param \Throwable $exception Exception to throw into the async generator.
|
|
|
|
*
|
2020-08-23 16:18:28 +02:00
|
|
|
* @return Promise<mixed|null> Resolves with null if the pipeline has completed.
|
2020-05-19 00:01:14 +02:00
|
|
|
*
|
2020-05-21 17:11:22 +02:00
|
|
|
* @psalm-return Promise<TValue|null>
|
2020-05-18 20:49:56 +02:00
|
|
|
*
|
2020-05-28 19:59:55 +02:00
|
|
|
* @throws \Error If the first emitted value has not been retrieved using {@see continue()}.
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
|
|
|
public function throw(\Throwable $exception): Promise
|
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
return $this->source->throw($exception);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 21:59:31 +02:00
|
|
|
/**
|
|
|
|
* Notifies the generator that the consumer is no longer interested in the generator output.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dispose()
|
|
|
|
{
|
2020-05-19 17:49:40 +02:00
|
|
|
$this->source->dispose();
|
2020-05-13 21:59:31 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
/**
|
2020-05-19 00:01:14 +02:00
|
|
|
* @return Promise<mixed>
|
|
|
|
*
|
|
|
|
* @psalm-return Promise<TReturn>
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
|
|
|
public function getReturn(): Promise
|
|
|
|
{
|
2020-07-16 20:43:11 +02:00
|
|
|
if ($this->coroutine !== null) {
|
|
|
|
return $this->coroutine;
|
|
|
|
}
|
|
|
|
|
2020-07-17 18:19:36 +02:00
|
|
|
/** @psalm-suppress PossiblyNullArgument */
|
2020-07-16 20:43:11 +02:00
|
|
|
$this->coroutine = new Coroutine($this->generator);
|
|
|
|
$this->generator = null;
|
|
|
|
|
|
|
|
$source = $this->source;
|
|
|
|
$this->coroutine->onResolve(static function ($exception) use ($source) {
|
2020-07-30 21:33:39 +02:00
|
|
|
if ($source->isDisposed()) {
|
2020-07-17 18:19:36 +02:00
|
|
|
return; // AsyncGenerator object was destroyed.
|
|
|
|
}
|
|
|
|
|
2020-07-16 20:43:11 +02:00
|
|
|
if ($exception) {
|
|
|
|
$source->fail($exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$source->complete();
|
|
|
|
});
|
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
return $this->coroutine;
|
|
|
|
}
|
|
|
|
}
|