1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00

Rename wrap to createRunnable and coroutine to createCallable

This commit is contained in:
Niklas Keller 2017-04-23 14:09:16 +02:00
parent 4577f92625
commit dfa40f5b00
2 changed files with 30 additions and 27 deletions

View File

@ -4,35 +4,38 @@ namespace Amp {
use React\Promise\PromiseInterface as ReactPromise; use React\Promise\PromiseInterface as ReactPromise;
/** /**
* Wraps the callback in a promise/coroutine-aware function that automatically upgrades Generators to coroutines and * Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
* calls `rethrow()` on the returned promises (or the coroutine created). * 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 * Use this function to create a coroutine-aware callable for a non-promise-aware callback caller.
* automatically handled by `rethrow()`.
* *
* @param callable(...$args): \Generator|\Amp\Promise|mixed $callback * @param callable(...$args): \Generator|\Amp\Promise|mixed $callback
* *
* @return callable(...$args): void * @return callable(...$args): void
*
* @see createCallable()
*/ */
function wrap(callable $callback): callable { function createRunnable(callable $callback): callable {
return function (...$args) use ($callback) { return function (...$args) use ($callback) {
Promise\rethrow(call($callback, ...$args)); Promise\rethrow(call($callback, ...$args));
}; };
} }
/** /**
* Returns a new function that wraps $worker in a promise/coroutine-aware function that automatically upgrades * Returns a new function that wraps $callback in a promise/coroutine-aware function that automatically runs
* Generators to coroutines. The returned function always returns a promise when invoked. If $callback throws, * Generators as coroutines. The returned function always returns a promise when invoked. Errors have to be handled
* a failed promise is returned. * 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 * Use this function to create a coroutine-aware callable for a promise-aware callback caller.
* handled by the caller.
* *
* @param callable(mixed ...$args): mixed $callback * @param callable(mixed ...$args): mixed $callback
* *
* @return callable(mixed ...$args): \Amp\Promise * @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 function (...$args) use ($callback): Promise {
return call($callback, ...$args); return call($callback, ...$args);
}; };

View File

@ -449,8 +449,8 @@ class CoroutineTest extends TestCase {
$this->assertTrue($invoked); $this->assertTrue($invoked);
} }
public function testCoroutineFunction() { public function testCreateCallableFunction() {
$callable = \Amp\coroutine(function () { $callable = \Amp\createCallable(function () {
yield; yield;
}); });
@ -458,12 +458,12 @@ class CoroutineTest extends TestCase {
} }
/** /**
* @depends testCoroutineFunction * @depends testCreateCallableFunction
*/ */
public function testCoroutineFunctionWithCallbackReturningPromise() { public function testCreateCallableFunctionWithCallbackReturningPromise() {
$value = 1; $value = 1;
$promise = new Success($value); $promise = new Success($value);
$callable = \Amp\coroutine(function ($value) { $callable = \Amp\createCallable(function ($value) {
return $value; return $value;
}); });
@ -482,11 +482,11 @@ class CoroutineTest extends TestCase {
} }
/** /**
* @depends testCoroutineFunction * @depends testCreateCallableFunction
*/ */
public function testCoroutineFunctionWithNonGeneratorCallback() { public function testCreateCallableFunctionWithNonGeneratorCallback() {
$value = 1; $value = 1;
$callable = \Amp\coroutine(function ($value) { $callable = \Amp\createCallable(function ($value) {
return $value; return $value;
}); });
@ -505,11 +505,11 @@ class CoroutineTest extends TestCase {
} }
/** /**
* @depends testCoroutineFunction * @depends testCreateCallableFunction
*/ */
public function testCoroutineFunctionWithThrowingCallback() { public function testCreateCallableFunctionWithThrowingCallback() {
$exception = new \Exception; $exception = new \Exception;
$callable = \Amp\coroutine(function () use ($exception) { $callable = \Amp\createCallable(function () use ($exception) {
throw $exception; throw $exception;
}); });
@ -528,10 +528,10 @@ class CoroutineTest extends TestCase {
} }
/** /**
* @depends testCoroutineFunction * @depends testCreateCallableFunction
*/ */
public function testCoroutineFunctionWithSuccessReturnCallback() { public function testCreateCallableFunctionWithSuccessReturnCallback() {
$callable = \Amp\coroutine(function () { $callable = \Amp\createCallable(function () {
return new Success(42); return new Success(42);
}); });
@ -549,8 +549,8 @@ class CoroutineTest extends TestCase {
$this->assertSame(42, $result); $this->assertSame(42, $result);
} }
public function testCoroutineFunctionWithReactPromise() { public function testCreateCallableFunctionWithReactPromise() {
$callable = \Amp\coroutine(function () { $callable = \Amp\createCallable(function () {
return new FulfilledReactPromise(42); return new FulfilledReactPromise(42);
}); });