1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/lib/Coroutine.php

130 lines
4.4 KiB
PHP
Raw Normal View History

2016-08-18 05:25:54 +02:00
<?php declare(strict_types = 1);
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
2016-08-16 20:07:51 +02:00
use Interop\Async\{ Awaitable, Loop };
2016-05-21 16:44:52 +02:00
2016-06-01 19:18:11 +02:00
/**
2016-08-13 18:37:59 +02:00
* Creates an awaitable from a generator function yielding awaitables.
*
* When an awaitable is yielded, execution of the generator is interrupted until the awaitable is resolved. A success
* 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-05-21 16:44:52 +02:00
final class Coroutine implements Awaitable {
use Internal\Placeholder;
2016-08-13 18:37:59 +02:00
/**
* Maximum number of immediate coroutine continuations before deferring next continuation to the loop.
*
* @internal
*/
2016-06-01 19:06:43 +02:00
const MAX_CONTINUATION_DEPTH = 3;
2016-05-21 16:44:52 +02:00
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 */
2016-05-21 16:44:52 +02:00
private $when;
2016-08-18 05:25:54 +02:00
/** @var int */
2016-05-21 16:44:52 +02:00
private $depth = 0;
/**
* @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.
2016-08-13 18:37:59 +02:00
* @param mixed $value Value to be sent into the generator.
2016-05-21 16:44:52 +02:00
*/
2016-05-24 17:39:19 +02:00
$this->when = function ($exception, $value) {
2016-08-13 18:37:59 +02:00
if ($this->depth > self::MAX_CONTINUATION_DEPTH) { // Defer continuation to avoid blowing up call stack.
2016-05-21 16:44:52 +02:00
Loop::defer(function () use ($exception, $value) {
2016-08-11 21:35:58 +02:00
($this->when)($exception, $value);
2016-05-21 16:44:52 +02:00
});
return;
}
try {
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);
}
if ($yielded instanceof Awaitable) {
2016-08-15 15:38:52 +02:00
++$this->depth;
$yielded->when($this->when);
2016-08-15 15:38:52 +02:00
--$this->depth;
return;
}
2016-08-13 18:37:59 +02:00
if ($this->generator->valid()) {
2016-08-18 05:25:54 +02:00
$got = \is_object($yielded) ? \get_class($yielded) : \gettype($yielded);
2016-08-11 21:35:58 +02:00
throw new InvalidYieldError(
$this->generator,
2016-08-13 18:37:59 +02:00
\sprintf("Unexpected yield (%s expected, got %s)", Awaitable::class, $got)
2016-08-11 21:35:58 +02:00
);
}
2016-08-11 21:35:58 +02:00
$this->resolve($this->generator->getReturn());
2016-05-21 16:44:52 +02:00
} catch (\Throwable $exception) {
$this->dispose($exception);
2016-05-21 16:44:52 +02:00
}
};
try {
$yielded = $this->generator->current();
2016-05-21 16:44:52 +02:00
if ($yielded instanceof Awaitable) {
++$this->depth;
$yielded->when($this->when);
--$this->depth;
return;
}
if ($this->generator->valid()) {
2016-08-18 05:25:54 +02:00
$got = \is_object($yielded) ? \get_class($yielded) : \gettype($yielded);
2016-08-11 21:35:58 +02:00
throw new InvalidYieldError(
$this->generator,
2016-08-13 18:37:59 +02:00
\sprintf("Unexpected yield (%s expected, got %s)", Awaitable::class, $got)
2016-08-11 21:35:58 +02:00
);
2016-06-02 17:46:22 +02:00
}
2016-05-21 16:44:52 +02:00
2016-08-11 21:35:58 +02:00
$this->resolve($this->generator->getReturn());
} catch (\Throwable $exception) {
$this->dispose($exception);
}
}
2016-06-02 17:46:22 +02:00
/**
* Runs the generator to completion then fails the coroutine with the given exception.
*
2016-08-11 21:35:58 +02:00
* @param \Throwable $exception
*/
2016-08-11 21:35:58 +02:00
private function dispose(\Throwable $exception) {
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);
}
2016-06-01 19:06:43 +02:00
}