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