Fix Deferred

(cherry picked from commit 828a9fb002)
This commit is contained in:
Simon Podlipsky 2018-12-28 04:39:16 +07:00 committed by Vladimir Razuvaev
parent f52dfcfaef
commit ababa18157

View File

@ -11,7 +11,7 @@ use Throwable;
class Deferred class Deferred
{ {
/** @var SplQueue */ /** @var SplQueue|null */
private static $queue; private static $queue;
/** @var callable */ /** @var callable */
@ -20,21 +20,6 @@ class Deferred
/** @var SyncPromise */ /** @var SyncPromise */
public $promise; public $promise;
public static function getQueue()
{
return self::$queue ?: self::$queue = new SplQueue();
}
public static function runQueue()
{
$q = self::$queue;
while ($q && ! $q->isEmpty()) {
/** @var self $dfd */
$dfd = $q->dequeue();
$dfd->run();
}
}
public function __construct(callable $callback) public function __construct(callable $callback)
{ {
$this->callback = $callback; $this->callback = $callback;
@ -42,6 +27,25 @@ class Deferred
self::getQueue()->enqueue($this); self::getQueue()->enqueue($this);
} }
public static function getQueue() : SplQueue
{
if (self::$queue === null) {
self::$queue = new SplQueue();
}
return self::$queue;
}
public static function runQueue() : void
{
$queue = self::getQueue();
while (! $queue->isEmpty()) {
/** @var self $dequeuedNodeValue */
$dequeuedNodeValue = $queue->dequeue();
$dequeuedNodeValue->run();
}
}
public function then($onFulfilled = null, $onRejected = null) public function then($onFulfilled = null, $onRejected = null)
{ {
return $this->promise->then($onFulfilled, $onRejected); return $this->promise->then($onFulfilled, $onRejected);