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-10 21:58:46 +01:00
|
|
|
use Amp\Promise\ErrorHandler;
|
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
|
|
|
|
*/
|
2016-05-21 16:44:52 +02:00
|
|
|
class WhenQueue {
|
2016-08-18 05:25:54 +02:00
|
|
|
/** @var callable[] */
|
2016-05-21 16:44:52 +02:00
|
|
|
private $queue = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable|null $callback Initial callback to add to queue.
|
|
|
|
*/
|
|
|
|
public function __construct(callable $callback = null) {
|
|
|
|
if (null !== $callback) {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function push(callable $callback) {
|
|
|
|
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
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2016-08-12 23:38:36 +02:00
|
|
|
public function __invoke($exception, $value) {
|
2016-05-21 16:44:52 +02:00
|
|
|
foreach ($this->queue as $callback) {
|
|
|
|
try {
|
|
|
|
$callback($exception, $value);
|
|
|
|
} catch (\Throwable $exception) {
|
2016-12-29 21:09:49 +01:00
|
|
|
ErrorHandler::notify($exception);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|