Skip to content

Commit 8f83c59

Browse files
authored
Merge pull request #86 from reactphp/make-queue-internal
Make Queue internal
2 parents 3fd6727 + e117e90 commit 8f83c59

File tree

8 files changed

+69
-72
lines changed

8 files changed

+69
-72
lines changed

src/FulfilledPromise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
2222
}
2323

2424
return new Promise(function (callable $resolve, callable $reject) use ($onFulfilled) {
25-
Queue::enqueue(function () use ($resolve, $reject, $onFulfilled) {
25+
enqueue(function () use ($resolve, $reject, $onFulfilled) {
2626
try {
2727
$resolve($onFulfilled($this->value));
2828
} catch (\Throwable $exception) {
@@ -40,7 +40,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
4040
return;
4141
}
4242

43-
Queue::enqueue(function () use ($onFulfilled) {
43+
enqueue(function () use ($onFulfilled) {
4444
$result = $onFulfilled($this->value);
4545

4646
if ($result instanceof PromiseInterface) {

src/Queue/SynchronousDriver.php renamed to src/Internal/Queue.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3-
namespace React\Promise\Queue;
3+
namespace React\Promise\Internal;
44

5-
final class SynchronousDriver implements DriverInterface
5+
/**
6+
* @internal
7+
*/
8+
final class Queue
69
{
710
private $queue = [];
811

src/Queue.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Queue/DriverInterface.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/RejectedPromise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
2222
}
2323

2424
return new Promise(function (callable $resolve, callable $reject) use ($onRejected) {
25-
Queue::enqueue(function () use ($resolve, $reject, $onRejected) {
25+
enqueue(function () use ($resolve, $reject, $onRejected) {
2626
try {
2727
$resolve($onRejected($this->reason));
2828
} catch (\Throwable $exception) {
@@ -36,7 +36,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
3636

3737
public function done(callable $onFulfilled = null, callable $onRejected = null)
3838
{
39-
Queue::enqueue(function () use ($onRejected) {
39+
enqueue(function () use ($onRejected) {
4040
if (null === $onRejected) {
4141
throw UnhandledRejectionException::resolve($this->reason);
4242
}

src/functions.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ function reduce(array $promisesOrValues, callable $reduceFunc, $initialValue = n
189189
}, $cancellationQueue);
190190
}
191191

192+
/**
193+
* @internal
194+
*/
195+
function enqueue(callable $task)
196+
{
197+
static $queue;
198+
199+
if (!$queue) {
200+
$queue = new Internal\Queue();
201+
}
202+
203+
$queue->enqueue($task);
204+
}
205+
192206
/**
193207
* @internal
194208
*/

tests/Internal/QueueTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace React\Promise\Internal;
4+
5+
use React\Promise\TestCase;
6+
7+
class QueueTest extends TestCase
8+
{
9+
/** @test */
10+
public function excutesTasks()
11+
{
12+
$queue = new Queue();
13+
14+
$queue->enqueue($this->expectCallableOnce());
15+
$queue->enqueue($this->expectCallableOnce());
16+
}
17+
18+
/** @test */
19+
public function excutesNestedEnqueuedTasks()
20+
{
21+
$queue = new Queue();
22+
23+
$nested = $this->expectCallableOnce();
24+
25+
$task = function() use ($queue, $nested) {
26+
$queue->enqueue($nested);
27+
};
28+
29+
$queue->enqueue($task);
30+
}
31+
32+
/** @test */
33+
public function rethrowsExceptionsThrownFromTasks()
34+
{
35+
$this->setExpectedException('\Exception', 'test');
36+
37+
$mock = $this->createCallableMock();
38+
$mock
39+
->expects($this->once())
40+
->method('__invoke')
41+
->will($this->throwException(new \Exception('test')));
42+
43+
$queue = new Queue();
44+
$queue->enqueue($mock);
45+
}
46+
}

tests/QueueTest.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)