2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 05:48:28 +02:00
|
|
|
namespace Amp;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-02-20 21:53:58 +01:00
|
|
|
use React\Promise\PromiseInterface as ReactPromise;
|
|
|
|
|
2016-06-01 19:18:11 +02:00
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* Creates a promise from a generator function yielding promises.
|
2016-08-13 18:37:59 +02:00
|
|
|
*
|
2016-11-14 20:59:21 +01:00
|
|
|
* When a promise is yielded, execution of the generator is interrupted until the promise is resolved. A success
|
2016-08-13 18:37:59 +02:00
|
|
|
* value is sent into the generator, while a failure reason is thrown into the generator. Using a coroutine,
|
|
|
|
* asynchronous code can be written without callbacks and be structured like synchronous code.
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @template-covariant TReturn
|
|
|
|
* @template-implements Promise<TReturn>
|
2016-06-01 19:18:11 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
final class Coroutine implements Promise
|
|
|
|
{
|
2016-05-21 16:44:52 +02:00
|
|
|
use Internal\Placeholder;
|
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
/**
|
|
|
|
* Attempts to transform the non-promise yielded from the generator into a promise, otherwise returns an instance
|
|
|
|
* `Amp\Failure` failed with an instance of `Amp\InvalidYieldError`.
|
|
|
|
*
|
|
|
|
* @param mixed $yielded Non-promise yielded from generator.
|
|
|
|
* @param \Generator $generator No type for performance, we already know the type.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
private static function transform(mixed $yielded, \Generator $generator): Promise
|
2019-05-14 21:37:08 +02:00
|
|
|
{
|
2020-03-28 12:23:46 +01:00
|
|
|
$exception = null; // initialize here, see https://github.com/vimeo/psalm/issues/2951
|
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
try {
|
|
|
|
if (\is_array($yielded)) {
|
|
|
|
return Promise\all($yielded);
|
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
if ($yielded instanceof ReactPromise) {
|
|
|
|
return Promise\adapt($yielded);
|
|
|
|
}
|
2017-08-05 05:50:19 +02:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
// No match, continue to returning Failure below.
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
// Conversion to promise failed, fall-through to returning Failure below.
|
|
|
|
}
|
2017-08-05 05:50:19 +02:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
return new Failure(new InvalidYieldError(
|
|
|
|
$generator,
|
|
|
|
\sprintf(
|
|
|
|
"Unexpected yield; Expected an instance of %s or %s or an array of such instances",
|
|
|
|
Promise::class,
|
|
|
|
ReactPromise::class
|
|
|
|
),
|
2020-03-28 12:23:46 +01:00
|
|
|
$exception
|
2019-05-14 21:37:08 +02:00
|
|
|
));
|
|
|
|
}
|
2017-05-20 09:46:01 +02:00
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
/**
|
|
|
|
* @param \Generator $generator
|
2020-03-28 13:52:48 +01:00
|
|
|
* @psalm-param \Generator<mixed,Promise|ReactPromise|array<array-key,
|
|
|
|
* Promise|ReactPromise>,mixed,Promise<TReturn>|ReactPromise|TReturn> $generator
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __construct(\Generator $generator)
|
|
|
|
{
|
2017-08-05 05:50:19 +02:00
|
|
|
try {
|
2019-05-14 21:37:08 +02:00
|
|
|
$yielded = $generator->current();
|
2017-08-05 05:50:19 +02:00
|
|
|
|
|
|
|
if (!$yielded instanceof Promise) {
|
2019-05-14 21:37:08 +02:00
|
|
|
if (!$generator->valid()) {
|
|
|
|
$this->resolve($generator->getReturn());
|
2017-08-05 05:50:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
$yielded = self::transform($yielded, $generator);
|
2017-08-05 05:50:19 +02:00
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->fail($exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
/**
|
2019-05-14 21:37:08 +02:00
|
|
|
* @param \Throwable|null $e Exception to be thrown into the generator.
|
|
|
|
* @param mixed $v Value to be sent into the generator.
|
2020-03-28 12:23:46 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @psalm-suppress MissingClosureParamType
|
|
|
|
* @psalm-suppress MissingClosureReturnType
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
$onResolve = function (?\Throwable $e, mixed $v) use ($generator, &$onResolve): void {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var bool $immediate Used to control iterative coroutine continuation. */
|
2019-05-20 21:10:59 +02:00
|
|
|
static $immediate = true;
|
|
|
|
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var \Throwable|null $exception Promise failure reason when executing next coroutine step, null at all other times. */
|
2019-05-20 21:10:59 +02:00
|
|
|
static $exception;
|
|
|
|
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var mixed $value Promise success value when executing next coroutine step, null at all other times. */
|
2019-05-20 21:10:59 +02:00
|
|
|
static $value;
|
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
$exception = $e;
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress MixedAssignment */
|
2019-05-14 21:37:08 +02:00
|
|
|
$value = $v;
|
2017-05-22 07:01:10 +02:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
if (!$immediate) {
|
|
|
|
$immediate = true;
|
2017-05-20 09:40:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
try {
|
2019-05-20 20:29:09 +02:00
|
|
|
try {
|
|
|
|
do {
|
|
|
|
if ($exception) {
|
|
|
|
// Throw exception at current execution point.
|
|
|
|
$yielded = $generator->throw($exception);
|
|
|
|
} else {
|
|
|
|
// Send the new value and execute to next yield statement.
|
|
|
|
$yielded = $generator->send($value);
|
2017-05-20 16:26:56 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 20:29:09 +02:00
|
|
|
if (!$yielded instanceof Promise) {
|
|
|
|
if (!$generator->valid()) {
|
|
|
|
$this->resolve($generator->getReturn());
|
|
|
|
$onResolve = null;
|
|
|
|
return;
|
|
|
|
}
|
2016-08-13 18:37:59 +02:00
|
|
|
|
2019-05-20 20:29:09 +02:00
|
|
|
$yielded = self::transform($yielded, $generator);
|
|
|
|
}
|
2017-05-22 07:01:10 +02:00
|
|
|
|
2019-05-20 20:29:09 +02:00
|
|
|
$immediate = false;
|
|
|
|
$yielded->onResolve($onResolve);
|
|
|
|
} while ($immediate);
|
|
|
|
|
|
|
|
$immediate = true;
|
|
|
|
} catch (\Throwable $exception) {
|
2019-05-14 21:37:08 +02:00
|
|
|
$this->fail($exception);
|
|
|
|
$onResolve = null;
|
2019-05-20 20:29:09 +02:00
|
|
|
} finally {
|
2019-05-14 21:37:08 +02:00
|
|
|
$exception = null;
|
|
|
|
$value = null;
|
|
|
|
}
|
2019-05-20 20:29:09 +02:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Loop::defer(static function () use ($e) {
|
|
|
|
throw $e;
|
|
|
|
});
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
try {
|
|
|
|
$yielded->onResolve($onResolve);
|
|
|
|
|
2019-05-20 21:10:59 +02:00
|
|
|
unset($generator, $yielded, $onResolve);
|
2019-05-14 21:37:08 +02:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Loop::defer(static function () use ($e) {
|
|
|
|
throw $e;
|
|
|
|
});
|
|
|
|
}
|
2016-06-15 05:35:28 +02:00
|
|
|
}
|
2016-06-01 19:06:43 +02:00
|
|
|
}
|