Skip to content

Commit

Permalink
Make loop instance the first argument
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Aug 4, 2015
1 parent f5fc66d commit d3a15e9
Show file tree
Hide file tree
Showing 27 changed files with 490 additions and 497 deletions.
10 changes: 5 additions & 5 deletions src/Coroutine/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ class Coroutine extends Promise implements CoroutineInterface
private $initial = true;

/**
* @param \Icicle\Loop\LoopInterface $loop
* @param \Generator $generator
* @param \Icicle\Loop\LoopInterface|null $loop
*/
public function __construct(Generator $generator, LoopInterface $loop = null)
public function __construct(LoopInterface $loop, Generator $generator)
{
$this->generator = $generator;

parent::__construct(
$loop,
function (callable $resolve, callable $reject, LoopInterface $loop) {
/**
* @param mixed $value The value to send to the generator.
Expand Down Expand Up @@ -94,7 +95,7 @@ function (callable $resolve, callable $reject, LoopInterface $loop) {
}

if ($this->current instanceof Generator) {
$this->current = new self($this->current, $loop);
$this->current = new self($loop, $this->current);
}

if ($this->current instanceof PromiseInterface) {
Expand Down Expand Up @@ -134,8 +135,7 @@ function (callable $resolve, callable $reject, LoopInterface $loop) {
$this->close();
}
};
},
$loop
}
);
}

Expand Down
26 changes: 10 additions & 16 deletions src/Coroutine/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
* Wraps the function returning a \Generator in a callable function that returns a new coroutine each time the
* function is called.
*
* @param \Icicle\Loop\LoopInterface $loop
* @param callable $worker
* @param \Icicle\Loop\LoopInterface|null $loop
*
* @return callable
*/
function wrap(callable $worker, LoopInterface $loop = null)
function wrap(LoopInterface $loop, callable $worker)
{
/**
* @param mixed ...$args
Expand All @@ -34,34 +34,28 @@ function wrap(callable $worker, LoopInterface $loop = null)
* not return a Generator.
*/
return function (/* ...$args */) use ($worker, $loop) {
$args = func_get_args();

if (!empty($args)) {
$worker = function () use ($worker, $args) {
return call_user_func_array($worker, $args);
};
}

return create($worker, $loop);
return call_user_func_array(__NAMESPACE__ . '\create', array_merge([$loop, $worker], func_get_args()));
};
}

/**
* Calls the callable with the given arguments which must return a \Generator, which is then made into a coroutine
* and returned.
*
* @param \Icicle\Loop\LoopInterface $loop
* @param callable $worker
* @param \Icicle\Loop\LoopInterface|null $loop
*
* @return \Icicle\Coroutine\Coroutine
*
* @throws \Icicle\Coroutine\Exception\InvalidCallableError If the callable throws an exception or does not
* return a Generator.
*/
function create(callable $worker, LoopInterface $loop = null)
function create(LoopInterface $loop, callable $worker /* , ...$args */)
{
$args = array_slice(func_get_args(), 2);

try {
$generator = $worker();
$generator = call_user_func_array($worker, $args);
} catch (\Exception $exception) {
throw new InvalidCallableError('The callable threw an exception.', $worker, $exception);
}
Expand All @@ -70,7 +64,7 @@ function create(callable $worker, LoopInterface $loop = null)
throw new InvalidCallableError('The callable did not return a Generator.', $worker);
}

return new Coroutine($generator, $loop);
return new Coroutine($loop, $generator);
}

/**
Expand All @@ -86,7 +80,7 @@ function sleep($time)
{
$self = (yield); // Get Coroutine instance.

$start = (yield Promise\resolve(microtime(true), $self->getLoop())->delay($time));
$start = (yield Promise\resolve($self->getLoop(), microtime(true))->delay($time));

yield microtime(true) - $start;
}
Expand Down
17 changes: 10 additions & 7 deletions src/Promise/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ class Deferred implements PromisorInterface
private $reject;

/**
* @param callable|null $onCancelled
* @param \Icicle\Loop\LoopInterface $loop
* @param callable|null $onCancelled
*/
public function __construct(callable $onCancelled = null, LoopInterface $loop = null)
public function __construct(LoopInterface $loop, callable $onCancelled = null)
{
$this->promise = new Promise(function (callable $resolve, callable $reject) use ($onCancelled) {
$this->resolve = $resolve;
$this->reject = $reject;
return $onCancelled;
}, $loop);
$this->promise = new Promise(
$loop,
function (callable $resolve, callable $reject) use ($onCancelled) {
$this->resolve = $resolve;
$this->reject = $reject;
return $onCancelled;
}
);
}

/**
Expand Down
29 changes: 14 additions & 15 deletions src/Promise/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Icicle\Promise;

use Exception;
use Icicle\Loop;
use Icicle\Loop\LoopInterface;
use Icicle\Promise\Exception\CircularResolutionError;
use Icicle\Promise\Exception\InvalidResolverError;
Expand Down Expand Up @@ -61,12 +60,12 @@ class Promise implements PromiseInterface
private $children = 0;

/**
* @param \Icicle\Loop\LoopInterface $loop
* @param callable<(callable $resolve, callable $reject, LoopInterface $loop): callable|null> $resolver
* @param \Icicle\Loop\LoopInterface|null $loop
*/
public function __construct(callable $resolver, LoopInterface $loop = null)
public function __construct(LoopInterface $loop, callable $resolver)
{
$this->loop = $loop ?: Loop\loop();
$this->loop = $loop;

/**
* Resolves the promise with the given promise or value. If another promise, this promise takes
Expand All @@ -79,12 +78,12 @@ public function __construct(callable $resolver, LoopInterface $loop = null)
$value = $value->unwrap();
if ($this === $value) {
$value = new RejectedPromise(
new CircularResolutionError('Circular reference in promise resolution chain.'),
$this->loop
$this->loop,
new CircularResolutionError('Circular reference in promise resolution chain.')
);
}
} else {
$value = new FulfilledPromise($value, $this->loop);
$value = new FulfilledPromise($this->loop, $value);
}

$this->resolve($value);
Expand All @@ -96,7 +95,7 @@ public function __construct(callable $resolver, LoopInterface $loop = null)
* @param mixed $reason
*/
$reject = function ($reason = null) {
$this->resolve(new RejectedPromise($reason, $this->loop));
$this->resolve(new RejectedPromise($this->loop, $reason));
};

try {
Expand Down Expand Up @@ -179,6 +178,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
++$this->children;

return new self(
$this->loop,
function (callable $resolve, callable $reject) use ($onFulfilled, $onRejected) {
if (null !== $onFulfilled) {
$this->onFulfilled(function ($value) use ($resolve, $reject, $onFulfilled) {
Expand Down Expand Up @@ -213,8 +213,7 @@ function (callable $resolve, callable $reject) use ($onFulfilled, $onRejected) {
$this->cancel($exception);
}
};
},
$this->loop
}
);
}

Expand Down Expand Up @@ -247,7 +246,7 @@ public function cancel($reason = null)
return;
}

$this->resolve(new CancelledPromise($reason, $this->onCancelled, $this->loop));
$this->resolve(new CancelledPromise($this->loop, $reason, $this->onCancelled));
}

/**
Expand All @@ -262,6 +261,7 @@ public function timeout($timeout, $reason = null)
++$this->children;

return new self(
$this->loop,
function (callable $resolve) use ($timeout, $reason) {
$timer = $this->loop->timer($timeout, function () use ($reason) {
if (!$reason instanceof Exception) {
Expand All @@ -285,8 +285,7 @@ function (callable $resolve) use ($timeout, $reason) {
$this->cancel($exception);
}
};
},
$this->loop
}
);
}

Expand All @@ -302,6 +301,7 @@ public function delay($time)
++$this->children;

return new self(
$this->loop,
function (callable $resolve) use ($time) {
$this->onFulfilled(function () use (&$timer, $time, $resolve) {
$timer = $this->loop->timer($time, function () use ($resolve) {
Expand All @@ -322,8 +322,7 @@ function (callable $resolve) use ($time) {
$this->cancel($exception);
}
};
},
$this->loop
}
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Promise/Structures/CancelledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ class CancelledPromise extends ResolvedPromise
private $result;

/**
* @param \Icicle\Loop\LoopInterface $loop
* @param mixed $reason
* @param callable|null $onCancelled
* @param \Icicle\Loop\LoopInterface $loop
*/
public function __construct($reason, callable $onCancelled = null, LoopInterface $loop = null)
public function __construct(LoopInterface $loop, $reason, callable $onCancelled = null)
{
if (!$reason instanceof Exception) {
$reason = new CancelledException($reason);
}

$this->result = new RejectedPromise($reason, $loop);
$this->result = new RejectedPromise($loop, $reason);

if (null !== $onCancelled) {
$this->result = $this->result->cleanup(function () use ($onCancelled, $reason) {
Expand Down
15 changes: 7 additions & 8 deletions src/Promise/Structures/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Icicle\Promise\Structures;

use Icicle\Loop;
use Icicle\Loop\LoopInterface;
use Icicle\Promise\Exception\InvalidArgumentError;
use Icicle\Promise\Promise;
Expand All @@ -28,18 +27,18 @@ class FulfilledPromise extends ResolvedPromise
private $value;

/**
* @param LoopInterface $loop
* @param mixed $value Anything other than a PromiseInterface object.
* @param LoopInterface|null $loop
*
* @throws \Icicle\Promise\Exception\InvalidArgumentError If a PromiseInterface is given as the value.
*/
public function __construct($value, LoopInterface $loop = null)
public function __construct(LoopInterface $loop, $value)
{
if ($value instanceof PromiseInterface) {
throw new InvalidArgumentError('Cannot use a PromiseInterface as a fulfilled promise value.');
}

$this->loop = $loop ?: Loop\loop();
$this->loop = $loop;
$this->value = $value;
}

Expand All @@ -52,15 +51,15 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
return $this;
}

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

/**
Expand All @@ -79,6 +78,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
public function delay($time)
{
return new Promise(
$this->loop,
function (callable $resolve) use ($time) {
$timer = $this->loop->timer($time, function () use ($resolve) {
$resolve($this);
Expand All @@ -87,8 +87,7 @@ function (callable $resolve) use ($time) {
return function () use ($timer) {
$timer->stop();
};
},
$this->loop
}
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Promise/Structures/LazyPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class LazyPromise implements PromiseInterface
private $promisor;

/**
* @param callable $promisor
* @param LoopInterface $loop
* @param callable $promisor
*/
public function __construct(callable $promisor, LoopInterface $loop = null)
public function __construct(LoopInterface $loop, callable $promisor)
{
$this->loop = $loop ?: Loop\loop();
$this->loop = $loop;
$this->promisor = $promisor;
}

Expand All @@ -54,9 +54,9 @@ protected function getPromise()
$this->promisor = null;

try {
$this->promise = Promise\resolve($promisor(), $this->loop);
$this->promise = Promise\resolve($this->loop, $promisor());
} catch (\Exception $exception) {
$this->promise = Promise\reject($exception, $this->loop);
$this->promise = Promise\reject($this->loop, $exception);
}
}

Expand Down

0 comments on commit d3a15e9

Please sign in to comment.