Skip to content

Commit

Permalink
Simplify queue usage and add QueueInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Mar 22, 2015
1 parent 784d646 commit 22add4c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
28 changes: 16 additions & 12 deletions src/Promise.php
Expand Up @@ -24,6 +24,10 @@ public function __construct(callable $resolver, callable $canceller = null, Queu

public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null !== $this->result) {
return $this->result->then($onFulfilled, $onRejected, $onProgress);
}

$canceller = null;

if (null !== $this->canceller) {
Expand Down Expand Up @@ -101,16 +105,6 @@ public function cancel()

private function resolver(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null !== $this->result) {
return function ($resolve, $reject, $notify) use ($onFulfilled, $onRejected) {
$this->ensureQueue()->enqueue(function() use ($onFulfilled, $onRejected, $resolve, $reject, $notify) {
$this->result
->then($onFulfilled, $onRejected)
->done($resolve, $reject, $notify);
});
};
}

return function ($resolve, $reject, $notify) use ($onFulfilled, $onRejected, $onProgress) {
if ($onProgress) {
$progressHandler = function ($update) use ($notify, $onProgress) {
Expand Down Expand Up @@ -158,7 +152,13 @@ private function notify($update = null)
return;
}

$this->ensureQueue()->enqueueHandlers($this->progressHandlers, $update);
$handlers = $this->progressHandlers;

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

private function settle(ExtendedPromiseInterface $result)
Expand All @@ -168,7 +168,11 @@ private function settle(ExtendedPromiseInterface $result)
$this->progressHandlers = $this->handlers = [];
$this->result = $result;

$this->ensureQueue()->enqueueHandlers($handlers, $result);
$this->ensureQueue()->enqueue(function () use ($handlers, $result) {
foreach ($handlers as $handler) {
$handler($result);
}
});
}

private function call(callable $callback)
Expand Down
11 changes: 1 addition & 10 deletions src/Queue.php
Expand Up @@ -2,19 +2,10 @@

namespace React\Promise;

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

public function enqueueHandlers(array $handlers, $value)
{
$this->enqueue(function () use ($handlers, $value) {
foreach ($handlers as $handler) {
$handler($value);
}
});
}

public function enqueue(callable $task)
{
$length = array_push($this->queue, $task);
Expand Down
8 changes: 8 additions & 0 deletions src/QueueInterface.php
@@ -0,0 +1,8 @@
<?php

namespace React\Promise;

interface QueueInterface
{
public function enqueue(callable $task);
}

0 comments on commit 22add4c

Please sign in to comment.