diff --git a/src/FulfilledPromise.php b/src/FulfilledPromise.php index fd6d6337..caa98336 100644 --- a/src/FulfilledPromise.php +++ b/src/FulfilledPromise.php @@ -22,7 +22,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) } return new Promise(function (callable $resolve, callable $reject) use ($onFulfilled) { - Queue::enqueue(function () use ($resolve, $reject, $onFulfilled) { + enqueue(function () use ($resolve, $reject, $onFulfilled) { try { $resolve($onFulfilled($this->value)); } catch (\Throwable $exception) { @@ -40,7 +40,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null) return; } - Queue::enqueue(function () use ($onFulfilled) { + enqueue(function () use ($onFulfilled) { $result = $onFulfilled($this->value); if ($result instanceof PromiseInterface) { diff --git a/src/Queue/SynchronousDriver.php b/src/Internal/Queue.php similarity index 88% rename from src/Queue/SynchronousDriver.php rename to src/Internal/Queue.php index 33c0372d..be1a08ea 100644 --- a/src/Queue/SynchronousDriver.php +++ b/src/Internal/Queue.php @@ -1,8 +1,11 @@ enqueue($task); - } -} diff --git a/src/Queue/DriverInterface.php b/src/Queue/DriverInterface.php deleted file mode 100644 index 7d2865f3..00000000 --- a/src/Queue/DriverInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -reason)); } catch (\Throwable $exception) { @@ -36,7 +36,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null) public function done(callable $onFulfilled = null, callable $onRejected = null) { - Queue::enqueue(function () use ($onRejected) { + enqueue(function () use ($onRejected) { if (null === $onRejected) { throw UnhandledRejectionException::resolve($this->reason); } diff --git a/src/functions.php b/src/functions.php index 479b2c9a..6b32daf2 100644 --- a/src/functions.php +++ b/src/functions.php @@ -189,6 +189,20 @@ function reduce(array $promisesOrValues, callable $reduceFunc, $initialValue = n }, $cancellationQueue); } +/** + * @internal + */ +function enqueue(callable $task) +{ + static $queue; + + if (!$queue) { + $queue = new Internal\Queue(); + } + + $queue->enqueue($task); +} + /** * @internal */ diff --git a/tests/Internal/QueueTest.php b/tests/Internal/QueueTest.php new file mode 100644 index 00000000..14fde6bb --- /dev/null +++ b/tests/Internal/QueueTest.php @@ -0,0 +1,46 @@ +enqueue($this->expectCallableOnce()); + $queue->enqueue($this->expectCallableOnce()); + } + + /** @test */ + public function excutesNestedEnqueuedTasks() + { + $queue = new Queue(); + + $nested = $this->expectCallableOnce(); + + $task = function() use ($queue, $nested) { + $queue->enqueue($nested); + }; + + $queue->enqueue($task); + } + + /** @test */ + public function rethrowsExceptionsThrownFromTasks() + { + $this->setExpectedException('\Exception', 'test'); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->will($this->throwException(new \Exception('test'))); + + $queue = new Queue(); + $queue->enqueue($mock); + } +} diff --git a/tests/QueueTest.php b/tests/QueueTest.php deleted file mode 100644 index 4846649d..00000000 --- a/tests/QueueTest.php +++ /dev/null @@ -1,22 +0,0 @@ -getMockBuilder('React\Promise\Queue\DriverInterface') - ->getMock(); - - Queue::setDriver($newDriver); - - $this->assertSame($newDriver, Queue::getDriver()); - - Queue::setDriver($oldDriver); - } -}