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.
|
2016-06-01 19:18:11 +02:00
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
final class Coroutine implements Promise {
|
2016-05-21 16:44:52 +02:00
|
|
|
use Internal\Placeholder;
|
|
|
|
|
2016-08-18 05:25:54 +02:00
|
|
|
/** @var \Generator */
|
2016-05-21 16:44:52 +02:00
|
|
|
private $generator;
|
|
|
|
|
2016-08-18 05:25:54 +02:00
|
|
|
/** @var callable(\Throwable|null $exception, mixed $value): void */
|
2017-03-21 17:23:37 +01:00
|
|
|
private $onResolve;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-05-20 16:26:56 +02:00
|
|
|
private $immediate = false;
|
|
|
|
private $exception;
|
|
|
|
private $value;
|
2017-05-20 09:46:01 +02:00
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
/**
|
|
|
|
* @param \Generator $generator
|
|
|
|
*/
|
2016-05-21 19:07:08 +02:00
|
|
|
public function __construct(\Generator $generator) {
|
2016-05-21 16:44:52 +02:00
|
|
|
$this->generator = $generator;
|
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02: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 16:44:52 +02:00
|
|
|
*/
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->onResolve = function ($exception, $value) {
|
2017-05-20 16:26:56 +02:00
|
|
|
if ($this->immediate) {
|
|
|
|
$this->immediate = false;
|
|
|
|
$this->exception = $exception;
|
|
|
|
$this->value = $value;
|
2017-05-20 09:40:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-21 16:44:52 +02:00
|
|
|
try {
|
2017-05-20 16:26:56 +02:00
|
|
|
do {
|
|
|
|
if ($exception) {
|
|
|
|
// Throw exception at current execution point.
|
|
|
|
$yielded = $this->generator->throw($exception);
|
|
|
|
} else {
|
|
|
|
// Send the new value and execute to next yield statement.
|
|
|
|
$yielded = $this->generator->send($value);
|
2017-02-20 21:53:58 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 16:26:56 +02:00
|
|
|
if (!$yielded instanceof Promise) {
|
|
|
|
if (!$this->generator->valid()) {
|
|
|
|
$this->resolve($this->generator->getReturn());
|
|
|
|
$this->onResolve = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$yielded = $this->transform($yielded);
|
|
|
|
}
|
2016-08-13 18:37:59 +02:00
|
|
|
|
2017-05-20 16:26:56 +02:00
|
|
|
$this->immediate = true;
|
|
|
|
$yielded->onResolve($this->onResolve);
|
|
|
|
if ($this->immediate) {
|
|
|
|
$this->immediate = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$exception = $this->exception;
|
|
|
|
$this->exception = null;
|
|
|
|
$value = $this->value;
|
|
|
|
$this->value = null;
|
|
|
|
} while (true);
|
2016-05-21 16:44:52 +02:00
|
|
|
} catch (\Throwable $exception) {
|
2017-04-24 18:10:05 +02:00
|
|
|
$this->fail($exception);
|
|
|
|
$this->onResolve = null;
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2016-06-15 05:35:28 +02:00
|
|
|
$yielded = $this->generator->current();
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-02-20 21:53:58 +01:00
|
|
|
if (!$yielded instanceof Promise) {
|
|
|
|
if (!$this->generator->valid()) {
|
|
|
|
$this->resolve($this->generator->getReturn());
|
2017-04-06 16:24:21 +02:00
|
|
|
$this->onResolve = null;
|
2017-02-20 21:53:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-06-15 05:35:28 +02:00
|
|
|
|
2017-03-16 01:10:23 +01:00
|
|
|
$yielded = $this->transform($yielded);
|
2016-06-02 17:46:22 +02:00
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$yielded->onResolve($this->onResolve);
|
2016-06-15 05:35:28 +02:00
|
|
|
} catch (\Throwable $exception) {
|
2017-04-24 18:10:05 +02:00
|
|
|
$this->fail($exception);
|
|
|
|
$this->onResolve = null;
|
2016-06-15 05:35:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 17:46:22 +02:00
|
|
|
|
2017-03-16 01:10:23 +01:00
|
|
|
/**
|
2017-04-24 20:05:53 +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`.
|
2017-03-16 01:10:23 +01:00
|
|
|
*
|
|
|
|
* @param mixed $yielded Non-promise yielded from generator.
|
|
|
|
*
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
|
|
|
private function transform($yielded): Promise {
|
|
|
|
try {
|
|
|
|
if (\is_array($yielded)) {
|
|
|
|
return Promise\all($yielded);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($yielded instanceof ReactPromise) {
|
|
|
|
return Promise\adapt($yielded);
|
|
|
|
}
|
|
|
|
|
2017-04-24 20:05:53 +02:00
|
|
|
// No match, continue to returning Failure below.
|
2017-03-16 01:10:23 +01:00
|
|
|
} catch (\Throwable $exception) {
|
2017-04-24 20:05:53 +02:00
|
|
|
// Conversion to promise failed, fall-through to returning Failure below.
|
2017-03-16 01:10:23 +01:00
|
|
|
}
|
|
|
|
|
2017-04-24 20:05:53 +02:00
|
|
|
return new Failure(new InvalidYieldError(
|
2017-03-16 01:10:23 +01:00
|
|
|
$this->generator,
|
|
|
|
\sprintf(
|
|
|
|
"Unexpected yield; Expected an instance of %s or %s or an array of such instances",
|
|
|
|
Promise::class,
|
|
|
|
ReactPromise::class
|
|
|
|
),
|
|
|
|
$exception ?? null
|
2017-04-24 20:05:53 +02:00
|
|
|
));
|
2017-03-16 01:10:23 +01:00
|
|
|
}
|
2016-06-01 19:06:43 +02:00
|
|
|
}
|