mirror of
https://github.com/danog/amp.git
synced 2025-01-22 05:11:42 +01:00
Reactor is now optional for combinator functions
This commit is contained in:
parent
59578f7e8c
commit
c75a8514a1
@ -63,7 +63,7 @@ class LibeventReactor implements SignalReactor {
|
||||
foreach ($immediates as $watcherId => $callback) {
|
||||
$result = $callback($this, $watcherId);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
unset(
|
||||
$this->immediates[$watcherId],
|
||||
@ -195,7 +195,7 @@ class LibeventReactor implements SignalReactor {
|
||||
$watcherId = $watcher->id;
|
||||
$result = $callback($this, $watcherId);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
$this->cancel($watcherId);
|
||||
} catch (\Exception $e) {
|
||||
@ -243,7 +243,7 @@ class LibeventReactor implements SignalReactor {
|
||||
try {
|
||||
$result = $callback($this, $watcherId);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
event_add($eventResource, $msDelay);
|
||||
} catch (\Exception $e) {
|
||||
@ -310,7 +310,7 @@ class LibeventReactor implements SignalReactor {
|
||||
try {
|
||||
$result = $callback($this, $watcherId, $stream);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->stopException = $e;
|
||||
@ -356,7 +356,7 @@ class LibeventReactor implements SignalReactor {
|
||||
try {
|
||||
$result = $callback($this, $watcherId, $signo);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->stopException = $e;
|
||||
|
@ -96,7 +96,7 @@ class NativeReactor implements Reactor {
|
||||
foreach ($immediates as $watcherId => $callback) {
|
||||
$result = $callback($this, $watcherId);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -141,7 +141,7 @@ class NativeReactor implements Reactor {
|
||||
foreach ($this->readCallbacks[$streamId] as $watcherId => $callback) {
|
||||
$result = $callback($this, $watcherId, $readableStream);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -150,7 +150,7 @@ class NativeReactor implements Reactor {
|
||||
foreach ($this->writeCallbacks[$streamId] as $watcherId => $callback) {
|
||||
$result = $callback($this, $watcherId, $writableStream);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -181,7 +181,7 @@ class NativeReactor implements Reactor {
|
||||
|
||||
$result = $callback($this, $watcherId);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ class UvReactor implements SignalReactor {
|
||||
foreach ($immediates as $watcherId => $callback) {
|
||||
$result = $callback($this, $watcherId);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
unset(
|
||||
$this->immediates[$watcherId],
|
||||
@ -201,7 +201,7 @@ class UvReactor implements SignalReactor {
|
||||
try {
|
||||
$result = $callback($this, $watcher->id);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
if ($watcher->mode === self::$MODE_ONCE) {
|
||||
$this->clearWatcher($watcher->id);
|
||||
@ -343,7 +343,7 @@ class UvReactor implements SignalReactor {
|
||||
$callback = $watcher->callback;
|
||||
$result = $callback($this, $watcher->id, $watcher->stream);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->stopException = $e;
|
||||
@ -379,7 +379,7 @@ class UvReactor implements SignalReactor {
|
||||
try {
|
||||
$result = $callback($this, $watcher->id, $watcher->signo);
|
||||
if ($result instanceof \Generator) {
|
||||
resolve($this, $result)->when($this->onGeneratorError);
|
||||
resolve($result, $this)->when($this->onGeneratorError);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->stopException = $e;
|
||||
|
@ -20,18 +20,18 @@ function reactor(callable $factory = null) {
|
||||
* the resulting Promise succeeds with an array matching keys from the input array
|
||||
* to their resolved values.
|
||||
*
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @param array[\Amp\Promise] $promises
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function all(Reactor $reactor, array $promises) {
|
||||
function all(array $promises, Reactor $reactor = null) {
|
||||
if (empty($promises)) {
|
||||
return new Success([]);
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$remaining = count($promises);
|
||||
$promisor = new Future($reactor);
|
||||
$promisor = new Future($reactor ?: reactor());
|
||||
$isResolved = false;
|
||||
|
||||
foreach ($promises as $key => $resolvable) {
|
||||
@ -78,11 +78,11 @@ function all(Reactor $reactor, array $promises) {
|
||||
* The individual keys in the resulting arrays are preserved from the initial Promise array
|
||||
* passed to the function for evaluation.
|
||||
*
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @param array[\Amp\Promise] $promises
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function some(Reactor $reactor, array $promises) {
|
||||
function some(array $promises, Reactor $reactor = null) {
|
||||
if (empty($promises)) {
|
||||
return new Failure(new \LogicException(
|
||||
'No promises or values provided for resolution'
|
||||
@ -91,7 +91,7 @@ function some(Reactor $reactor, array $promises) {
|
||||
|
||||
$results = $errors = [];
|
||||
$remaining = count($promises);
|
||||
$promisor = new Future($reactor);
|
||||
$promisor = new Future($reactor ?: reactor());
|
||||
|
||||
foreach ($promises as $key => $resolvable) {
|
||||
if (!$resolvable instanceof Promise) {
|
||||
@ -130,11 +130,11 @@ function some(Reactor $reactor, array $promises) {
|
||||
* This function is the same as some() with the notable exception that it will never fail even
|
||||
* if all promises in the array resolve unsuccessfully.
|
||||
*
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @param array $promises
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function any(Reactor $reactor, array $promises) {
|
||||
function any(array $promises, Reactor $reactor = null) {
|
||||
if (empty($promises)) {
|
||||
return new Success([], []);
|
||||
}
|
||||
@ -142,7 +142,7 @@ function any(Reactor $reactor, array $promises) {
|
||||
$results = [];
|
||||
$errors = [];
|
||||
$remaining = count($promises);
|
||||
$promisor = new Future($reactor);
|
||||
$promisor = new Future($reactor ?: reactor());
|
||||
|
||||
foreach ($promises as $key => $resolvable) {
|
||||
if (!$resolvable instanceof Promise) {
|
||||
@ -173,11 +173,11 @@ function any(Reactor $reactor, array $promises) {
|
||||
* Resolves with the first successful Promise value. The resulting Promise will only fail if all
|
||||
* Promise values in the group fail or if the initial Promise array is empty.
|
||||
*
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @param array[\Amp\Promise] $promises
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function first(Reactor $reactor, array $promises) {
|
||||
function first(array $promises, Reactor $reactor = null) {
|
||||
if (empty($promises)) {
|
||||
return new Failure(new \LogicException(
|
||||
'No promises or values provided for resolution'
|
||||
@ -186,7 +186,7 @@ function first(Reactor $reactor, array $promises) {
|
||||
|
||||
$remaining = count($promises);
|
||||
$isComplete = false;
|
||||
$promisor = new Future($reactor);
|
||||
$promisor = new Future($reactor ?: reactor());
|
||||
|
||||
foreach ($promises as $resolvable) {
|
||||
if (!$resolvable instanceof Promise) {
|
||||
@ -217,19 +217,19 @@ function first(Reactor $reactor, array $promises) {
|
||||
/**
|
||||
* Map promised future values using the specified functor
|
||||
*
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @param array $promises
|
||||
* @param callable $functor
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function map(Reactor $reactor, array $promises, callable $functor) {
|
||||
function map(array $promises, callable $functor, Reactor $reactor = null) {
|
||||
if (empty($promises)) {
|
||||
return new Success([]);
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$remaining = count($promises);
|
||||
$promisor = new Future($reactor);
|
||||
$promisor = new Future($reactor ?: reactor());
|
||||
|
||||
foreach ($promises as $key => $resolvable) {
|
||||
$promise = ($resolvable instanceof Promise) ? $resolvable : new Success($resolvable);
|
||||
@ -267,19 +267,19 @@ function map(Reactor $reactor, array $promises, callable $functor) {
|
||||
* If the functor returns a truthy value the resolved promise result is retained, otherwise it is
|
||||
* discarded. Array keys are retained for any results not filtered out by the functor.
|
||||
*
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @param array $promises
|
||||
* @param callable $functor
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function filter(Reactor $reactor, array $promises, callable $functor) {
|
||||
function filter(array $promises, callable $functor, Reactor $reactor = null) {
|
||||
if (empty($promises)) {
|
||||
return new Success([]);
|
||||
}
|
||||
|
||||
$results = [];
|
||||
$remaining = count($promises);
|
||||
$promisor = new Future($reactor);
|
||||
$promisor = new Future($reactor ?: reactor());
|
||||
|
||||
foreach ($promises as $key => $resolvable) {
|
||||
$promise = ($resolvable instanceof Promise) ? $resolvable : new Success($resolvable);
|
||||
@ -314,12 +314,14 @@ function filter(Reactor $reactor, array $promises, callable $functor) {
|
||||
/**
|
||||
* A co-routine to resolve Generators
|
||||
*
|
||||
* Returns a promise that will resolve when the generator completes. The final value yielded by
|
||||
* the generator is used to resolve the returned Promise on success.
|
||||
* Returns a promise that resolves when the generator completes. The final value
|
||||
* yielded by the generator is used to resolve the returned Promise on success.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* function anotherGenerator() {
|
||||
* // wait 100 milliseconds before proceeding
|
||||
* yield 'wait' => 100;
|
||||
* yield 1;
|
||||
* }
|
||||
*
|
||||
@ -330,16 +332,15 @@ function filter(Reactor $reactor, array $promises, callable $functor) {
|
||||
* yield $a * $b * $c;
|
||||
* };
|
||||
*
|
||||
* $reactor = new Amp\NativeReactor;
|
||||
* $result = resolve($reactor, $generator())->wait();
|
||||
* $result = resolve($generator())->wait();
|
||||
* var_dump($result); // int(42)
|
||||
*
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @param \Generator $gen
|
||||
* @param \Amp\Reactor $reactor
|
||||
* @return \Amp\Promise
|
||||
*/
|
||||
function resolve(Reactor $reactor, \Generator $gen) {
|
||||
$promisor = new Future($reactor);
|
||||
function resolve(\Generator $gen, Reactor $reactor = null) {
|
||||
$promisor = new Future($reactor ?: reactor());
|
||||
__advanceGenerator($reactor, $gen, $promisor);
|
||||
|
||||
return $promisor;
|
||||
@ -451,7 +452,7 @@ function __promisifyGeneratorYield(Reactor $reactor, $key, $current) {
|
||||
if ($element instanceof Promise) {
|
||||
$promise = $element;
|
||||
} elseif ($element instanceof \Generator) {
|
||||
$promise = resolve($reactor, $element);
|
||||
$promise = resolve($element, $reactor);
|
||||
} else {
|
||||
$promise = new Success($element);
|
||||
}
|
||||
@ -460,7 +461,7 @@ function __promisifyGeneratorYield(Reactor $reactor, $key, $current) {
|
||||
}
|
||||
|
||||
$combinatorFunction = __NAMESPACE__ . "\\{$key}";
|
||||
$promise = $combinatorFunction($reactor, $promises);
|
||||
$promise = $combinatorFunction($promises, $reactor);
|
||||
|
||||
goto return_struct;
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$expected = ['r1' => 42, 'r2' => 41];
|
||||
$results = \Amp\all($reactor, $promises)->wait();
|
||||
$results = \Amp\all($promises, $reactor)->wait();
|
||||
$this->assertSame($expected, $results);
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
];
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$results = \Amp\all($reactor, $promises)->wait();
|
||||
$results = \Amp\all($promises, $reactor)->wait();
|
||||
}
|
||||
|
||||
public function testSomeReturnsArrayOfErrorsAndResults() {
|
||||
@ -44,7 +44,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
];
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
list($errors, $results) = \Amp\some($reactor, $promises)->wait();
|
||||
list($errors, $results) = \Amp\some($promises, $reactor)->wait();
|
||||
|
||||
$this->assertSame(['r2' => $exception], $errors);
|
||||
$this->assertSame(['r1' => 42, 'r3' => 40], $results);
|
||||
@ -60,7 +60,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
'r2' => new Failure(new \RuntimeException),
|
||||
];
|
||||
$reactor = new NativeReactor;
|
||||
list($errors, $results) = \Amp\some($reactor, $promises)->wait();
|
||||
list($errors, $results) = \Amp\some($promises, $reactor)->wait();
|
||||
}
|
||||
|
||||
public function testResolveResolvesGeneratorResult() {
|
||||
@ -71,7 +71,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$promise = \Amp\resolve($reactor, $gen());
|
||||
$promise = \Amp\resolve($gen(), $reactor);
|
||||
$expected = 42;
|
||||
$actual = $promise->wait();
|
||||
$this->assertSame($expected, $actual);
|
||||
@ -98,7 +98,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
$gen = function() { yield 42; };
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$result = \Amp\resolve($reactor, $gen())->wait();
|
||||
$result = \Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame(42, $result);
|
||||
}
|
||||
|
||||
@ -111,7 +111,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$expected = 42;
|
||||
$reactor = new NativeReactor;
|
||||
$actual = \Amp\resolve($reactor, $gen())->wait();
|
||||
$actual = \Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame($expected, $actual);
|
||||
}
|
||||
|
||||
@ -132,7 +132,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$expected = 42;
|
||||
$reactor = new NativeReactor;
|
||||
$actual = \Amp\resolve($reactor, $gen())->wait();
|
||||
$actual = \Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame($expected, $actual);
|
||||
}
|
||||
|
||||
@ -148,7 +148,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
\Amp\resolve($reactor, $gen())->wait();
|
||||
\Amp\resolve($gen(), $reactor)->wait();
|
||||
}
|
||||
|
||||
public function testImplicitAllCombinatorResolution() {
|
||||
@ -158,7 +158,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$result = \Amp\resolve($reactor, $gen())->wait();
|
||||
$result = \Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame(42, $result);
|
||||
}
|
||||
|
||||
@ -169,7 +169,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$result = \Amp\resolve($reactor, $gen())->wait();
|
||||
$result = \Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame(420, $result);
|
||||
}
|
||||
|
||||
@ -186,7 +186,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
\Amp\resolve($reactor, $gen())->wait();
|
||||
\Amp\resolve($gen(), $reactor)->wait();
|
||||
}
|
||||
|
||||
public function testImplicitCombinatorResolvesGeneratorInArray() {
|
||||
@ -203,7 +203,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$result = \Amp\resolve($reactor, $gen2())->wait();
|
||||
$result = \Amp\resolve($gen2(), $reactor)->wait();
|
||||
$this->assertSame(42, $result);
|
||||
}
|
||||
|
||||
@ -218,7 +218,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
$result = \Amp\resolve($reactor, $gen())->wait();
|
||||
$result = \Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame(420, $result);
|
||||
}
|
||||
|
||||
@ -231,7 +231,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
list($errors, $results) = \Amp\resolve($reactor, $gen())->wait();
|
||||
list($errors, $results) = \Amp\resolve($gen(), $reactor)->wait();
|
||||
|
||||
$this->assertSame('test', $errors['b']->getMessage());
|
||||
$this->assertSame(21, $results['a']);
|
||||
@ -250,7 +250,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
\Amp\resolve($reactor, $gen())->wait();
|
||||
\Amp\resolve($gen(), $reactor)->wait();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -263,7 +263,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
\Amp\resolve($reactor, $gen())->wait();
|
||||
\Amp\resolve($gen(), $reactor)->wait();
|
||||
}
|
||||
|
||||
public function testExplicitImmediatelyYieldResolution() {
|
||||
@ -276,7 +276,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
\Amp\resolve($reactor, $gen())->wait();
|
||||
\Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame(42, $var);
|
||||
}
|
||||
|
||||
@ -288,7 +288,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
\Amp\resolve($reactor, $gen())->wait();
|
||||
\Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame(42, $var);
|
||||
}
|
||||
|
||||
@ -305,7 +305,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
};
|
||||
|
||||
$reactor = new NativeReactor;
|
||||
\Amp\resolve($reactor, $gen())->wait();
|
||||
\Amp\resolve($gen(), $reactor)->wait();
|
||||
$this->assertSame(2, $var);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user