1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00
amp/lib/Coroutine.php

165 lines
5.2 KiB
PHP
Raw Normal View History

<?php
2016-08-15 23:46:26 -05:00
2016-05-23 22:48:28 -05:00
namespace Amp;
2016-05-21 09:44:52 -05:00
2017-02-20 14:53:58 -06:00
use React\Promise\PromiseInterface as ReactPromise;
2016-06-01 12:18:11 -05:00
/**
2016-11-14 13:59:21 -06:00
* Creates a promise from a generator function yielding promises.
2016-08-13 18:37:59 +02:00
*
2016-11-14 13:59:21 -06: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.
2016-06-01 12:18:11 -05:00
*/
2018-06-18 20:00:01 +02:00
final class Coroutine implements Promise
{
2016-05-21 09:44:52 -05:00
use Internal\Placeholder;
/**
* 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
*/
private static function transform($yielded, $generator): Promise
{
try {
if (\is_array($yielded)) {
return Promise\all($yielded);
}
2016-05-21 09:44:52 -05:00
if ($yielded instanceof ReactPromise) {
return Promise\adapt($yielded);
}
// No match, continue to returning Failure below.
} catch (\Throwable $exception) {
// Conversion to promise failed, fall-through to returning Failure below.
}
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
),
$exception ?? null
));
}
2017-05-20 09:46:01 +02:00
2016-05-21 09:44:52 -05:00
/**
* @param \Generator $generator
*/
2018-06-18 20:00:01 +02:00
public function __construct(\Generator $generator)
{
try {
$yielded = $generator->current();
if (!$yielded instanceof Promise) {
if (!$generator->valid()) {
$this->resolve($generator->getReturn());
return;
}
$yielded = self::transform($yielded, $generator);
}
} catch (\Throwable $exception) {
$this->fail($exception);
return;
}
/** @var bool Used to control iterative coroutine continuation. */
$immediate = true;
/** @var \Throwable|null Promise failure reason when executing next coroutine step, null at all other times. */
$exception = null;
/** @var mixed Promise success value when executing next coroutine step, null at all other times. */
$value = null;
2016-05-21 09:44:52 -05:00
/**
* @param \Throwable|null $e Exception to be thrown into the generator.
* @param mixed $v Value to be sent into the generator.
2016-05-21 09:44:52 -05:00
*/
$onResolve = function ($e, $v) use ($generator, &$exception, &$value, &$immediate, &$onResolve) {
$exception = $e;
$value = $v;
2017-05-22 00:01:10 -05:00
if (!$immediate) {
$immediate = true;
return;
}
2016-05-21 09:44:52 -05: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-02-20 14:53:58 -06:00
}
if (!$yielded instanceof Promise) {
if (!$generator->valid()) {
$this->resolve($generator->getReturn());
$onResolve = null;
return;
}
$yielded = self::transform($yielded, $generator);
}
2016-08-13 18:37:59 +02:00
$immediate = false;
$yielded->onResolve($onResolve);
} while ($immediate);
2017-05-22 00:01:10 -05:00
$immediate = true;
2016-05-21 09:44:52 -05:00
} catch (\Throwable $exception) {
try {
$this->fail($exception);
$onResolve = null;
} catch (\Throwable $e) {
Loop::defer(static function () use ($e) {
throw $e;
});
}
2017-05-22 00:01:10 -05:00
} finally {
try {
$exception = null;
$value = null;
} catch (\Throwable $e) {
Loop::defer(static function () use ($e) {
throw $e;
});
}
2016-05-21 09:44:52 -05:00
}
};
try {
$yielded->onResolve($onResolve);
unset($generator, $yielded, $exception, $value, $immediate, $onResolve);
} catch (\Throwable $e) {
Loop::defer(static function () use ($e) {
throw $e;
});
}
}
2016-06-02 10:46:22 -05:00
public function __destruct()
2018-06-18 20:00:01 +02:00
{
try {
$this->result = null;
} catch (\Throwable $e) {
Loop::defer(static function () use ($e) {
throw $e;
});
}
}
2016-06-01 12:06:43 -05:00
}