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

91 lines
2.8 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\Internal;
2016-05-21 09:44:52 -05:00
use Amp\Coroutine;
use Amp\Loop;
use Amp\Promise;
use React\Promise\PromiseInterface as ReactPromise;
2016-05-21 09:44:52 -05:00
2016-06-01 12:18:11 -05:00
/**
2016-11-14 13:59:21 -06:00
* Stores a set of functions to be invoked when a promise is resolved.
2016-06-01 12:18:11 -05:00
*
* @internal
2020-03-28 20:27:42 +01:00
* @psalm-internal Amp\Internal
2016-06-01 12:18:11 -05:00
*/
final class ResolutionQueue
2018-06-18 20:00:01 +02:00
{
/** @var array<array-key, callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
* mixed>|null) | callable(\Throwable|null, mixed): void> */
2020-09-24 11:52:22 -05:00
private array $queue = [];
2016-05-21 09:44:52 -05:00
/**
* @param callable|null $callback Initial callback to add to queue.
*
* @psalm-param null|callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
* mixed>|null) | callable(\Throwable|null, mixed): void $callback
2016-05-21 09:44:52 -05:00
*/
2018-06-18 20:00:01 +02:00
public function __construct(callable $callback = null)
{
if ($callback !== null) {
2016-05-21 09:44:52 -05:00
$this->push($callback);
}
}
/**
* Unrolls instances of self to avoid blowing up the call stack on resolution.
*
* @param callable $callback
*
* @psalm-param callable(\Throwable|null, mixed): (Promise|\React\Promise\PromiseInterface|\Generator<mixed,
* Promise|\React\Promise\PromiseInterface|array<array-key, Promise|\React\Promise\PromiseInterface>, mixed,
* mixed>|null) | callable(\Throwable|null, mixed): void $callback
*
* @return void
*/
2020-09-24 11:52:22 -05:00
public function push(callable $callback): void
2018-06-18 20:00:01 +02:00
{
if ($callback instanceof self) {
$this->queue = \array_merge($this->queue, $callback->queue);
return;
}
$this->queue[] = $callback;
}
2016-05-21 09:44:52 -05:00
/**
* Calls each callback in the queue, passing the provided values to the function.
*
2016-08-11 14:35:58 -05:00
* @param \Throwable|null $exception
* @param mixed $value
*
* @return void
2016-05-21 09:44:52 -05:00
*/
2020-09-24 11:52:22 -05:00
public function __invoke(?\Throwable $exception, mixed $value): void
2018-06-18 20:00:01 +02:00
{
2016-05-21 09:44:52 -05:00
foreach ($this->queue as $callback) {
try {
$result = $callback($exception, $value);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
Promise\rethrow($result);
}
2016-05-21 09:44:52 -05:00
} catch (\Throwable $exception) {
2017-06-05 00:21:45 -05:00
Loop::defer(static function () use ($exception) {
throw $exception;
});
2016-05-21 09:44:52 -05:00
}
}
}
}