1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00

minor cleanup

This commit is contained in:
Daniel Lowrey 2015-03-23 11:07:40 -04:00
parent 6375cdd522
commit 654e82f277
6 changed files with 43 additions and 87 deletions

View File

@ -13,21 +13,14 @@ class Failure implements Promise {
}
/**
* Pass the resolved failure Exception to the specified callback
*
* NOTE: because this object represents a failed Promise it will *always* invoke the specified
* $func callback immediately.
*
* @return void
* {@inheritDoc}
*/
public function when(callable $func) {
$func($this->error, $result = null);
}
/**
* Does nothing -- a resolved promise has no progress updates
*
* @return void
* {@inheritDoc}
*/
public function watch(callable $func) {
return;

View File

@ -15,17 +15,15 @@ class Future implements Promisor, Promise {
* This implementation acts as both Promisor and Promise so we simply return the
* current instance. If users require a Promisor that can only be resolved by code
* holding a reference to the Promisor they may instead use Amp\PrivateFuture.
*
* @return \Amp\Promise
*/
public function promise(): Future {
public function promise(): Promise {
return $this;
}
/**
* Notify the $func callback when the promise resolves (whether successful or not)
*
* $func callbacks are invoked with parameters in error-first style.
*
* @return void
* {@inheritDoc}
*/
public function when(callable $func) {
if ($this->isResolved) {
@ -36,9 +34,7 @@ class Future implements Promisor, Promise {
}
/**
* Notify the $func callback when resolution progress events are emitted
*
* @return void
* {@inheritDoc}
*/
public function watch(callable $func) {
if (!$this->isResolved) {
@ -47,11 +43,8 @@ class Future implements Promisor, Promise {
}
/**
* Update watchers of resolution progress events
*
* @param mixed $progress
* {@inheritDoc}
* @throws \LogicException if the promise has already resolved
* @return void
*/
public function update(...$progress) {
if ($this->isResolved) {
@ -66,11 +59,8 @@ class Future implements Promisor, Promise {
}
/**
* Resolve the promised value as a success
*
* @param mixed $result
* {@inheritDoc}
* @throws \LogicException if the promise has already resolved or the result is the current instance
* @return void
*/
public function succeed($result = null) {
if ($this->isResolved) {
@ -101,10 +91,8 @@ class Future implements Promisor, Promise {
}
/**
* Resolve the promised value as a failure
*
* {@inheritDoc}
* @throws \LogicException if the promise has already resolved
* @return void
*/
public function fail(\Exception $error) {
if ($this->isResolved) {

View File

@ -5,10 +5,10 @@ namespace Amp;
interface SignalReactor extends Reactor {
/**
* React to process control signals
*
*
* @param int $signo The signal number for which to watch
* @param callable $func A callback to invoke when the specified signal is received
* @return string Returns unique (to the process) string watcher ID
*/
public function onSignal(int $signo, callable $onSignal): string;
public function onSignal(int $signo, callable $func): string;
}

View File

@ -14,7 +14,7 @@ class Unresolved implements Promise {
private $result;
/**
* Notify the $func callback when the promise resolves (whether successful or not)
* {@inheritDoc}
*/
public function when(callable $func): Unresolved {
if ($this->isResolved) {
@ -27,7 +27,7 @@ class Unresolved implements Promise {
}
/**
* Notify the $func callback when resolution progress events are emitted
* {@inheritDoc}
*/
public function watch(callable $func): Unresolved {
if (!$this->isResolved) {

View File

@ -110,12 +110,8 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
return ($a * $b);
};
$bar = function() use ($foo) {
return yield from $foo();
};
(new NativeReactor)->run(function($reactor) use ($bar) {
$result = yield resolve($bar(), $reactor);
(new NativeReactor)->run(function($reactor) use ($foo) {
$result = yield resolve($foo(), $reactor);
$this->assertSame(42, $result);
});
}
@ -143,27 +139,20 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
public function testAllCombinatorResolution() {
(new NativeReactor)->run(function($reactor) {
$gen = function() {
list($a, $b) = yield all([
list($a, $b) = yield all([
new Success(21),
new Success(2),
]);
return ($a * $b);
};
]);
$result = yield from $gen();
$result = ($a * $b);
$this->assertSame(42, $result);
});
}
public function testAllCombinatorResolutionWithNonPromises() {
(new NativeReactor)->run(function($reactor) {
$gen = function() {
list($a, $b, $c) = yield all([new Success(21), new Success(2), 10]);
return ($a * $b * $c);
};
$result = yield from $gen();
list($a, $b, $c) = yield all([new Success(21), new Success(2), 10]);
$result = ($a * $b * $c);
$this->assertSame(420, $result);
});
}
@ -174,42 +163,31 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
*/
public function testAllCombinatorResolutionThrowsIfAnyOnePromiseFails() {
(new NativeReactor)->run(function($reactor) {
$gen = function() {
list($a, $b) = yield all([
new Success(21),
new Failure(new \Exception('When in the chronicle of wasted time')),
]);
};
yield from $gen();
list($a, $b) = yield all([
new Success(21),
new Failure(new \Exception('When in the chronicle of wasted time')),
]);
});
}
public function testExplicitAllCombinatorResolution() {
(new NativeReactor)->run(function($reactor) {
$gen = function() {
list($a, $b, $c) = yield all([
new Success(21),
new Success(2),
10
]);
return ($a * $b * $c);
};
list($a, $b, $c) = yield all([
new Success(21),
new Success(2),
10
]);
$result = yield from $gen();
$this->assertSame(420, $result);
$this->assertSame(420, ($a * $b * $c));
});
}
public function testExplicitAnyCombinatorResolution() {
(new NativeReactor)->run(function($reactor) {
$gen = function() {
return yield any([
'a' => new Success(21),
'b' => new Failure(new \Exception('test')),
]);
};
list($errors, $results) = yield from $gen();
list($errors, $results) = yield any([
'a' => new Success(21),
'b' => new Failure(new \Exception('test')),
]);
$this->assertSame('test', $errors['b']->getMessage());
$this->assertSame(21, $results['a']);
});
@ -221,13 +199,10 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
*/
public function testExplicitSomeCombinatorResolutionFailsOnError() {
(new NativeReactor)->run(function($reactor) {
$gen = function() {
yield some([
'r1' => new Failure(new \RuntimeException),
'r2' => new Failure(new \RuntimeException),
]);
};
yield from $gen();
yield some([
'r1' => new Failure(new \RuntimeException),
'r2' => new Failure(new \RuntimeException),
]);
});
}

View File

@ -158,12 +158,12 @@ abstract class ReactorTest extends \PHPUnit_Framework_TestCase {
public function testOnceReturnsEventWatcher() {
$reactor = $this->getReactor();
$firstWatcherId = '1';
$firstWatcherId = 'a';
$watcherId = $reactor->once(function(){}, $delay = 0);
$this->assertSame($firstWatcherId, $watcherId);
$watcherId = $reactor->immediately(function(){});
$this->assertSame((string)($firstWatcherId + 1), $watcherId);
$this->assertSame(++$firstWatcherId, $watcherId);
}
/**
@ -199,12 +199,12 @@ abstract class ReactorTest extends \PHPUnit_Framework_TestCase {
public function testRepeatReturnsEventWatcher() {
$reactor = $this->getReactor();
$firstWatcherId = '1';
$firstWatcherId = 'a';
$watcherId = $reactor->repeat(function(){}, $msInterval = 1000);
$this->assertSame($firstWatcherId, $watcherId);
$watcherId = $reactor->repeat(function(){}, $msInterval = 1000);
$this->assertSame((string)($firstWatcherId + 1), $watcherId);
$this->assertSame(++$firstWatcherId, $watcherId);
}
public function testCancelRemovesWatcher() {