Skip to content

Commit 3fd6727

Browse files
authored
Merge pull request #82 from reactphp/queue-class
Replace the queue() function with a static Queue class
2 parents 34cdf71 + f9676bf commit 3fd6727

File tree

7 files changed

+64
-21
lines changed

7 files changed

+64
-21
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+
Queue::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+
Queue::enqueue(function () use ($onFulfilled) {
4444
$result = $onFulfilled($this->value);
4545

4646
if ($result instanceof PromiseInterface) {

src/Queue.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace React\Promise;
4+
5+
use React\Promise\Queue\DriverInterface;
6+
7+
final class Queue
8+
{
9+
/**
10+
* @var DriverInterface
11+
*/
12+
private static $driver;
13+
14+
public static function setDriver(DriverInterface $driver = null)
15+
{
16+
self::$driver = $driver;
17+
}
18+
19+
public static function getDriver()
20+
{
21+
if (!self::$driver) {
22+
self::$driver = new Queue\SynchronousDriver();
23+
}
24+
25+
return self::$driver;
26+
}
27+
28+
public static function enqueue(callable $task)
29+
{
30+
if (!self::$driver) {
31+
self::getDriver();
32+
}
33+
34+
self::$driver->enqueue($task);
35+
}
36+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Promise\Queue;
44

5-
interface QueueInterface
5+
interface DriverInterface
66
{
77
public function enqueue(callable $task);
88
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Promise\Queue;
44

5-
final class SynchronousQueue implements QueueInterface
5+
final class SynchronousDriver implements DriverInterface
66
{
77
private $queue = [];
88

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+
Queue::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+
Queue::enqueue(function () use ($onRejected) {
4040
if (null === $onRejected) {
4141
throw UnhandledRejectionException::resolve($this->reason);
4242
}

src/functions.php

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

192-
function queue(Queue\QueueInterface $queue = null)
193-
{
194-
static $globalQueue;
195-
196-
if ($queue) {
197-
return ($globalQueue = $queue);
198-
}
199-
200-
if (!$globalQueue) {
201-
$globalQueue = new Queue\SynchronousQueue();
202-
}
203-
204-
return $globalQueue;
205-
}
206-
207192
/**
208193
* @internal
209194
*/

tests/QueueTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace React\Promise;
4+
5+
class QueueTest extends TestCase
6+
{
7+
/** @test */
8+
public function canSetDriver()
9+
{
10+
$oldDriver = Queue::getDriver();
11+
12+
$newDriver = $this
13+
->getMockBuilder('React\Promise\Queue\DriverInterface')
14+
->getMock();
15+
16+
Queue::setDriver($newDriver);
17+
18+
$this->assertSame($newDriver, Queue::getDriver());
19+
20+
Queue::setDriver($oldDriver);
21+
}
22+
}

0 commit comments

Comments
 (0)