From 828a9fb002e975ddf5945d3f4af91e4e4e8f23e2 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 27 Dec 2018 22:39:16 +0100 Subject: [PATCH] Fix Deferred --- src/Deferred.php | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Deferred.php b/src/Deferred.php index 197eeeb..6b7bd78 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -11,7 +11,7 @@ use Throwable; class Deferred { - /** @var SplQueue */ + /** @var SplQueue|null */ private static $queue; /** @var callable */ @@ -20,21 +20,6 @@ class Deferred /** @var SyncPromise */ 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) { $this->callback = $callback; @@ -42,6 +27,25 @@ class Deferred 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) { return $this->promise->then($onFulfilled, $onRejected);