Skip to content

Commit

Permalink
added progress() and notify()
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith committed Jul 11, 2013
1 parent ffc8dd4 commit 8e2a7c3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Spork/Deferred/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class Deferred implements DeferredInterface
{
private $state;
private $progressCallbacks;
private $alwaysCallbacks;
private $doneCallbacks;
private $failCallbacks;
Expand All @@ -25,6 +26,7 @@ public function __construct()
{
$this->state = DeferredInterface::STATE_PENDING;

$this->progressCallbacks = array();
$this->alwaysCallbacks = array();
$this->doneCallbacks = array();
$this->failCallbacks = array();
Expand All @@ -35,6 +37,17 @@ public function getState()
return $this->state;
}

public function progress($progressCallback)
{
if (!is_callable($progressCallback)) {
throw new UnexpectedTypeException($progressCallback, 'callable');
}

$this->progressCallbacks[] = $progressCallback;

return $this;
}

public function always($alwaysCallback)
{
if (!is_callable($alwaysCallback)) {
Expand Down Expand Up @@ -99,6 +112,20 @@ public function then($doneCallback, $failCallback = null)
return $this;
}

public function notify()
{
if (DeferredInterface::STATE_PENDING !== $this->state) {
throw new \LogicException('Cannot notify a deferred object that is no longer pending');
}

$args = func_get_args();
foreach ($this->progressCallbacks as $func) {
call_user_func_array($func, $args);
}

return $this;
}

public function resolve()
{
if (DeferredInterface::STATE_REJECTED === $this->state) {
Expand Down
7 changes: 7 additions & 0 deletions src/Spork/Deferred/DeferredAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public function getChildren()
return $this->children;
}

public function progress($progressCallback)
{
$this->delegate->progress($progressCallback);

return $this;
}

public function always($alwaysCallback)
{
$this->delegate->always($alwaysCallback);
Expand Down
1 change: 1 addition & 0 deletions src/Spork/Deferred/DeferredInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

interface DeferredInterface extends PromiseInterface
{
function notify();
function resolve();
function reject();
}
1 change: 1 addition & 0 deletions src/Spork/Deferred/PromiseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface PromiseInterface
const STATE_REJECTED = 'rejected';

function getState();
function progress($progressCallback);
function always($alwaysCallback);
function done($doneCallback);
function fail($failCallback);
Expand Down
17 changes: 17 additions & 0 deletions src/Spork/Fork.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ public function getState()
return $this->defer->getState();
}

public function progress($callback)
{
$this->defer->progress($callback);

return $this;
}

public function always($callback)
{
$this->defer->always($callback);
Expand Down Expand Up @@ -187,6 +194,16 @@ public function then($done, $fail = null)
return $this;
}

public function notify()
{
$args = func_get_args();
array_unshift($args, $this);

call_user_func_array(array($this->defer, 'notify'), $args);

return $this;
}

public function resolve()
{
$args = func_get_args();
Expand Down

0 comments on commit 8e2a7c3

Please sign in to comment.