2016-12-29 14:09:49 -06:00
|
|
|
<?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
|
|
|
*/
|
2016-11-14 13:59:21 -06:00
|
|
|
final class Coroutine implements Promise {
|
2016-05-21 09:44:52 -05:00
|
|
|
use Internal\Placeholder;
|
|
|
|
|
2016-08-17 22:25:54 -05:00
|
|
|
/** @var \Generator */
|
2016-05-21 09:44:52 -05:00
|
|
|
private $generator;
|
|
|
|
|
2016-08-17 22:25:54 -05:00
|
|
|
/** @var callable(\Throwable|null $exception, mixed $value): void */
|
2017-03-21 17:23:37 +01:00
|
|
|
private $onResolve;
|
2016-05-21 09:44:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Generator $generator
|
|
|
|
*/
|
2016-05-21 12:07:08 -05:00
|
|
|
public function __construct(\Generator $generator) {
|
2016-05-21 09:44:52 -05:00
|
|
|
$this->generator = $generator;
|
|
|
|
|
|
|
|
/**
|
2016-08-11 14:35:58 -05:00
|
|
|
* @param \Throwable|null $exception Exception to be thrown into the generator.
|
2017-03-10 21:58:46 +01:00
|
|
|
* @param mixed $value Value to be sent into the generator.
|
2016-05-21 09:44:52 -05:00
|
|
|
*/
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->onResolve = function ($exception, $value) {
|
2016-05-21 09:44:52 -05:00
|
|
|
try {
|
|
|
|
if ($exception) {
|
|
|
|
// Throw exception at current execution point.
|
2016-06-14 22:35:28 -05:00
|
|
|
$yielded = $this->generator->throw($exception);
|
|
|
|
} else {
|
|
|
|
// Send the new value and execute to next yield statement.
|
|
|
|
$yielded = $this->generator->send($value);
|
|
|
|
}
|
|
|
|
|
2017-02-20 14:53:58 -06:00
|
|
|
if (!$yielded instanceof Promise) {
|
|
|
|
if (!$this->generator->valid()) {
|
|
|
|
$this->resolve($this->generator->getReturn());
|
2017-04-06 09:24:21 -05:00
|
|
|
$this->onResolve = null;
|
2017-02-20 14:53:58 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:10:23 -05:00
|
|
|
$yielded = $this->transform($yielded);
|
2016-06-14 22:35:28 -05:00
|
|
|
}
|
2016-08-13 18:37:59 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$yielded->onResolve($this->onResolve);
|
2016-05-21 09:44:52 -05:00
|
|
|
} catch (\Throwable $exception) {
|
2016-06-16 22:44:32 -05:00
|
|
|
$this->dispose($exception);
|
2016-05-21 09:44:52 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2016-06-14 22:35:28 -05:00
|
|
|
$yielded = $this->generator->current();
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2017-02-20 14:53:58 -06:00
|
|
|
if (!$yielded instanceof Promise) {
|
|
|
|
if (!$this->generator->valid()) {
|
|
|
|
$this->resolve($this->generator->getReturn());
|
2017-04-06 09:24:21 -05:00
|
|
|
$this->onResolve = null;
|
2017-02-20 14:53:58 -06:00
|
|
|
return;
|
|
|
|
}
|
2016-06-14 22:35:28 -05:00
|
|
|
|
2017-03-15 19:10:23 -05:00
|
|
|
$yielded = $this->transform($yielded);
|
2016-06-02 10:46:22 -05:00
|
|
|
}
|
2016-05-21 09:44:52 -05:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$yielded->onResolve($this->onResolve);
|
2016-06-14 22:35:28 -05:00
|
|
|
} catch (\Throwable $exception) {
|
2016-06-16 22:44:32 -05:00
|
|
|
$this->dispose($exception);
|
2016-06-14 22:35:28 -05:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 10:46:22 -05:00
|
|
|
|
2017-03-15 19:10:23 -05:00
|
|
|
/**
|
|
|
|
* Attempts to transform the non-promise yielded from the generator into a promise, otherwise throws an instance
|
2017-04-23 15:29:08 +02:00
|
|
|
* of `Amp\InvalidYieldError`.
|
2017-03-15 19:10:23 -05:00
|
|
|
*
|
|
|
|
* @param mixed $yielded Non-promise yielded from generator.
|
|
|
|
*
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*
|
|
|
|
* @throws \Amp\InvalidYieldError If the value could not be converted to a promise.
|
|
|
|
*/
|
|
|
|
private function transform($yielded): Promise {
|
|
|
|
try {
|
|
|
|
if (\is_array($yielded)) {
|
|
|
|
return Promise\all($yielded);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($yielded instanceof ReactPromise) {
|
|
|
|
return Promise\adapt($yielded);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match, continue to throwing error below.
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
// Conversion to promise failed, fall-through to throwing error below.
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidYieldError(
|
|
|
|
$this->generator,
|
|
|
|
\sprintf(
|
|
|
|
"Unexpected yield; Expected an instance of %s or %s or an array of such instances",
|
|
|
|
Promise::class,
|
|
|
|
ReactPromise::class
|
|
|
|
),
|
|
|
|
$exception ?? null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-16 22:44:32 -05:00
|
|
|
/**
|
|
|
|
* Runs the generator to completion then fails the coroutine with the given exception.
|
|
|
|
*
|
2016-08-11 14:35:58 -05:00
|
|
|
* @param \Throwable $exception
|
2016-06-16 22:44:32 -05:00
|
|
|
*/
|
2016-08-11 14:35:58 -05:00
|
|
|
private function dispose(\Throwable $exception) {
|
2016-06-16 22:44:32 -05:00
|
|
|
if ($this->generator->valid()) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// Ensure generator has run to completion to avoid throws from finally blocks on destruction.
|
|
|
|
do {
|
|
|
|
$this->generator->throw($exception);
|
|
|
|
} while ($this->generator->valid());
|
|
|
|
} finally {
|
|
|
|
// Throw from finally to attach any exception thrown from generator as previous exception.
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
// $exception will be used to fail the coroutine.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail($exception);
|
2017-04-06 09:24:21 -05:00
|
|
|
$this->onResolve = null;
|
2016-06-16 22:44:32 -05:00
|
|
|
}
|
2016-06-01 12:06:43 -05:00
|
|
|
}
|