Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Queue internal #86

Merged
merged 1 commit into from
Jan 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions src/Queue/SynchronousDriver.php → src/Internal/Queue.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

namespace React\Promise\Queue;
namespace React\Promise\Internal;

final class SynchronousDriver implements DriverInterface
/**
* @internal
*/
final class Queue
{
private $queue = [];

Expand Down
36 changes: 0 additions & 36 deletions src/Queue.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Queue/DriverInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
}

return new Promise(function (callable $resolve, callable $reject) use ($onRejected) {
Queue::enqueue(function () use ($resolve, $reject, $onRejected) {
enqueue(function () use ($resolve, $reject, $onRejected) {
try {
$resolve($onRejected($this->reason));
} catch (\Throwable $exception) {
Expand All @@ -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);
}
Expand Down
14 changes: 14 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/Internal/QueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace React\Promise\Internal;

use React\Promise\TestCase;

class QueueTest extends TestCase
{
/** @test */
public function excutesTasks()
{
$queue = new Queue();

$queue->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);
}
}
22 changes: 0 additions & 22 deletions tests/QueueTest.php

This file was deleted.