mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Rename wrap to createRunnable and coroutine to createCallable
This commit is contained in:
parent
4577f92625
commit
dfa40f5b00
@ -4,35 +4,38 @@ namespace Amp {
|
||||
use React\Promise\PromiseInterface as ReactPromise;
|
||||
|
||||
/**
|
||||
* Wraps the callback in a promise/coroutine-aware function that automatically upgrades Generators to coroutines and
|
||||
* calls `rethrow()` on the returned promises (or the coroutine created).
|
||||
* Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
|
||||
* Generators as coroutines. The returned function always returns void when invoked. Errors are forwarded to the
|
||||
* loop error handler using `Amp\Promise\rethrow()`.
|
||||
*
|
||||
* Use this function to create a coroutine-aware callable for a non-promise-aware callback caller. Errors are
|
||||
* automatically handled by `rethrow()`.
|
||||
* Use this function to create a coroutine-aware callable for a non-promise-aware callback caller.
|
||||
*
|
||||
* @param callable(...$args): \Generator|\Amp\Promise|mixed $callback
|
||||
*
|
||||
* @return callable(...$args): void
|
||||
*
|
||||
* @see createCallable()
|
||||
*/
|
||||
function wrap(callable $callback): callable {
|
||||
function createRunnable(callable $callback): callable {
|
||||
return function (...$args) use ($callback) {
|
||||
Promise\rethrow(call($callback, ...$args));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new function that wraps $worker in a promise/coroutine-aware function that automatically upgrades
|
||||
* Generators to coroutines. The returned function always returns a promise when invoked. If $callback throws,
|
||||
* a failed promise is returned.
|
||||
* Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
|
||||
* Generators as coroutines. The returned function always returns a promise when invoked. Errors have to be handled
|
||||
* by the callback caller or they will go unnoticed.
|
||||
*
|
||||
* Use this function to create a coroutine-aware callable for a promise-aware callback caller. Errors have to be
|
||||
* handled by the caller.
|
||||
* Use this function to create a coroutine-aware callable for a promise-aware callback caller.
|
||||
*
|
||||
* @param callable(mixed ...$args): mixed $callback
|
||||
*
|
||||
* @return callable(mixed ...$args): \Amp\Promise
|
||||
*
|
||||
* @see createRunnable()
|
||||
*/
|
||||
function coroutine(callable $callback): callable {
|
||||
function createCallable(callable $callback): callable {
|
||||
return function (...$args) use ($callback): Promise {
|
||||
return call($callback, ...$args);
|
||||
};
|
||||
|
@ -449,8 +449,8 @@ class CoroutineTest extends TestCase {
|
||||
$this->assertTrue($invoked);
|
||||
}
|
||||
|
||||
public function testCoroutineFunction() {
|
||||
$callable = \Amp\coroutine(function () {
|
||||
public function testCreateCallableFunction() {
|
||||
$callable = \Amp\createCallable(function () {
|
||||
yield;
|
||||
});
|
||||
|
||||
@ -458,12 +458,12 @@ class CoroutineTest extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCoroutineFunction
|
||||
* @depends testCreateCallableFunction
|
||||
*/
|
||||
public function testCoroutineFunctionWithCallbackReturningPromise() {
|
||||
public function testCreateCallableFunctionWithCallbackReturningPromise() {
|
||||
$value = 1;
|
||||
$promise = new Success($value);
|
||||
$callable = \Amp\coroutine(function ($value) {
|
||||
$callable = \Amp\createCallable(function ($value) {
|
||||
return $value;
|
||||
});
|
||||
|
||||
@ -482,11 +482,11 @@ class CoroutineTest extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCoroutineFunction
|
||||
* @depends testCreateCallableFunction
|
||||
*/
|
||||
public function testCoroutineFunctionWithNonGeneratorCallback() {
|
||||
public function testCreateCallableFunctionWithNonGeneratorCallback() {
|
||||
$value = 1;
|
||||
$callable = \Amp\coroutine(function ($value) {
|
||||
$callable = \Amp\createCallable(function ($value) {
|
||||
return $value;
|
||||
});
|
||||
|
||||
@ -505,11 +505,11 @@ class CoroutineTest extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCoroutineFunction
|
||||
* @depends testCreateCallableFunction
|
||||
*/
|
||||
public function testCoroutineFunctionWithThrowingCallback() {
|
||||
public function testCreateCallableFunctionWithThrowingCallback() {
|
||||
$exception = new \Exception;
|
||||
$callable = \Amp\coroutine(function () use ($exception) {
|
||||
$callable = \Amp\createCallable(function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
|
||||
@ -528,10 +528,10 @@ class CoroutineTest extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCoroutineFunction
|
||||
* @depends testCreateCallableFunction
|
||||
*/
|
||||
public function testCoroutineFunctionWithSuccessReturnCallback() {
|
||||
$callable = \Amp\coroutine(function () {
|
||||
public function testCreateCallableFunctionWithSuccessReturnCallback() {
|
||||
$callable = \Amp\createCallable(function () {
|
||||
return new Success(42);
|
||||
});
|
||||
|
||||
@ -549,8 +549,8 @@ class CoroutineTest extends TestCase {
|
||||
$this->assertSame(42, $result);
|
||||
}
|
||||
|
||||
public function testCoroutineFunctionWithReactPromise() {
|
||||
$callable = \Amp\coroutine(function () {
|
||||
public function testCreateCallableFunctionWithReactPromise() {
|
||||
$callable = \Amp\createCallable(function () {
|
||||
return new FulfilledReactPromise(42);
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user