Skip to content

Global queue #28

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

Merged
merged 16 commits into from
May 3, 2016
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
28 changes: 17 additions & 11 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public function then(callable $onFulfilled = null, callable $onRejected = null,
return $this;
}

try {
return resolve($onFulfilled($this->value));
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
return new Promise(function (callable $resolve, callable $reject) use ($onFulfilled) {
queue()->enqueue(function () use ($resolve, $reject, $onFulfilled) {
try {
$resolve($onFulfilled($this->value));
} catch (\Throwable $exception) {
$reject($exception);
} catch (\Exception $exception) {
$reject($exception);
}
});
});
}

public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
Expand All @@ -36,11 +40,13 @@ public function done(callable $onFulfilled = null, callable $onRejected = null,
return;
}

$result = $onFulfilled($this->value);
queue()->enqueue(function () use ($onFulfilled) {
$result = $onFulfilled($this->value);

if ($result instanceof ExtendedPromiseInterface) {
$result->done();
}
if ($result instanceof ExtendedPromiseInterface) {
$result->done();
}
});
}

public function otherwise(callable $onRejected)
Expand Down
16 changes: 10 additions & 6 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,24 @@ private function notify($update = null)
return;
}

foreach ($this->progressHandlers as $handler) {
$handler($update);
}
$handlers = $this->progressHandlers;

queue()->enqueue(function () use ($handlers, $update) {
foreach ($handlers as $handler) {
$handler($update);
}
});
}

private function settle(ExtendedPromiseInterface $promise)
private function settle(ExtendedPromiseInterface $result)
{
$handlers = $this->handlers;

$this->progressHandlers = $this->handlers = [];
$this->result = $promise;
$this->result = $result;

foreach ($handlers as $handler) {
$handler($promise);
$handler($result);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Queue/QueueInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace React\Promise\Queue;

interface QueueInterface
{
public function enqueue(callable $task);
}
38 changes: 38 additions & 0 deletions src/Queue/SynchronousQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace React\Promise\Queue;

class SynchronousQueue implements QueueInterface
{
private $queue = [];

public function enqueue(callable $task)
{
if (1 === array_push($this->queue, $task)) {
$this->drain();
}
}

private function drain()
{
for ($i = key($this->queue); isset($this->queue[$i]); $i++) {
$task = $this->queue[$i];

$exception = null;

try {
$task();
} catch (\Throwable $exception) {
} catch (\Exception $exception) {
}

unset($this->queue[$i]);

if ($exception) {
throw $exception;
}
}

$this->queue = [];
}
}
40 changes: 23 additions & 17 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,36 @@ public function then(callable $onFulfilled = null, callable $onRejected = null,
return $this;
}

try {
return resolve($onRejected($this->reason));
} catch (\Throwable $exception) {
return new RejectedPromise($exception);
} catch (\Exception $exception) {
return new RejectedPromise($exception);
}
return new Promise(function (callable $resolve, callable $reject) use ($onRejected) {
queue()->enqueue(function () use ($resolve, $reject, $onRejected) {
try {
$resolve($onRejected($this->reason));
} catch (\Throwable $exception) {
$reject($exception);
} catch (\Exception $exception) {
$reject($exception);
}
});
});
}

public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null === $onRejected) {
throw UnhandledRejectionException::resolve($this->reason);
}
queue()->enqueue(function () use ($onRejected) {
if (null === $onRejected) {
throw UnhandledRejectionException::resolve($this->reason);
}

$result = $onRejected($this->reason);
$result = $onRejected($this->reason);

if ($result instanceof self) {
throw UnhandledRejectionException::resolve($result->reason);
}
if ($result instanceof self) {
throw UnhandledRejectionException::resolve($result->reason);
}

if ($result instanceof ExtendedPromiseInterface) {
$result->done();
}
if ($result instanceof ExtendedPromiseInterface) {
$result->done();
}
});
}

public function otherwise(callable $onRejected)
Expand Down
15 changes: 15 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null)
}, $cancellationQueue);
}

function queue(Queue\QueueInterface $queue = null)
{
static $globalQueue;

if ($queue) {
return ($globalQueue = $queue);
}

if (!$globalQueue) {
$globalQueue = new Queue\SynchronousQueue();
}

return $globalQueue;
}

// Internal functions
function _checkTypehint(callable $callback, $object)
{
Expand Down
5 changes: 2 additions & 3 deletions tests/FunctionResolveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ public function shouldSupportVeryDeepNestedPromises()
{
$deferreds = [];

// @TODO Increase count once global-queue is merged
for ($i = 0; $i < 10; $i++) {
for ($i = 0; $i < 250; $i++) {
$deferreds[] = $d = new Deferred();
$p = $d->promise();

$last = $p;
for ($j = 0; $j < 10; $j++) {
for ($j = 0; $j < 250; $j++) {
$last = $last->then(function($result) {
return $result;
});
Expand Down