diff --git a/src/CancellationToken.php b/src/CancellationToken.php index 453062b..1039484 100644 --- a/src/CancellationToken.php +++ b/src/CancellationToken.php @@ -11,14 +11,14 @@ interface CancellationToken * Subscribes a new handler to be invoked on a cancellation request. * * This handler might be invoked immediately in case the token has already been cancelled. Returned generators will - * automatically be run as coroutines. Any unhandled exceptions will be throw into the event loop. + * automatically be run as coroutines. Any unhandled exceptions will be thrown into the event loop. * - * @param callable(CancelledException) $callback Callback to be invoked on a cancellation request. Will receive a + * @param \Closure(CancelledException) $callback Callback to be invoked on a cancellation request. Will receive a * `CancelledException` as first argument that may be used to fail the operation's promise. * * @return string Identifier that can be used to cancel the subscription. */ - public function subscribe(callable $callback): string; + public function subscribe(\Closure $callback): string; /** * Unsubscribes a previously registered handler. diff --git a/src/CombinedCancellationToken.php b/src/CombinedCancellationToken.php index b1e276c..1f0f4cd 100644 --- a/src/CombinedCancellationToken.php +++ b/src/CombinedCancellationToken.php @@ -42,8 +42,7 @@ final class CombinedCancellationToken implements CancellationToken } } - /** @inheritdoc */ - public function subscribe(callable $callback): string + public function subscribe(\Closure $callback): string { $id = $this->nextId++; diff --git a/src/Internal/CancellableToken.php b/src/Internal/CancellableToken.php index 2cc31fa..8a38996 100644 --- a/src/Internal/CancellableToken.php +++ b/src/Internal/CancellableToken.php @@ -37,7 +37,7 @@ final class CancellableToken implements CancellationToken } } - public function subscribe(callable $callback): string + public function subscribe(\Closure $callback): string { $id = $this->nextId++; diff --git a/src/Internal/WrappedCancellationToken.php b/src/Internal/WrappedCancellationToken.php index a7aa8a2..66f9a99 100644 --- a/src/Internal/WrappedCancellationToken.php +++ b/src/Internal/WrappedCancellationToken.php @@ -14,7 +14,7 @@ final class WrappedCancellationToken implements CancellationToken ) { } - public function subscribe(callable $callback): string + public function subscribe(\Closure $callback): string { return $this->token->subscribe($callback); } diff --git a/src/Internal/functions.php b/src/Internal/functions.php index b08e005..3678fa8 100644 --- a/src/Internal/functions.php +++ b/src/Internal/functions.php @@ -2,21 +2,6 @@ namespace Amp\Internal; -/** - * @param FutureState $state - * @param callable $callback - * - * @internal - */ -function run(FutureState $state, callable $callback): void -{ - try { - $state->complete($callback()); - } catch (\Throwable $exception) { - $state->error($exception); - } -} - /** * Formats a stacktrace obtained via `debug_backtrace()`. * diff --git a/src/NullCancellationToken.php b/src/NullCancellationToken.php index af263c3..94114c7 100644 --- a/src/NullCancellationToken.php +++ b/src/NullCancellationToken.php @@ -27,8 +27,7 @@ namespace Amp; */ final class NullCancellationToken implements CancellationToken { - /** @inheritdoc */ - public function subscribe(callable $callback): string + public function subscribe(\Closure $callback): string { return "null-token"; } diff --git a/src/TimeoutCancellationToken.php b/src/TimeoutCancellationToken.php index 2126548..44df5a6 100644 --- a/src/TimeoutCancellationToken.php +++ b/src/TimeoutCancellationToken.php @@ -45,10 +45,7 @@ final class TimeoutCancellationToken implements CancellationToken EventLoop::cancel($this->watcher); } - /** - * {@inheritdoc} - */ - public function subscribe(callable $callback): string + public function subscribe(\Closure $callback): string { return $this->token->subscribe($callback); } diff --git a/src/functions.php b/src/functions.php index f1f49ba..470e410 100644 --- a/src/functions.php +++ b/src/functions.php @@ -18,7 +18,15 @@ use Revolt\EventLoop\UnsupportedFeatureException; function launch(callable $callback): Future { $state = new Internal\FutureState; - EventLoop::queue('Amp\\Internal\\run', $state, $callback); + + EventLoop::queue(static function () use ($state, $callback) { + try { + $state->complete($callback()); + } catch (\Throwable $exception) { + $state->error($exception); + } + }); + return new Future($state); } diff --git a/test/CancellationToken/CancellationTest.php b/test/CancellationToken/CancellationTest.php index 400897a..4a2ff31 100644 --- a/test/CancellationToken/CancellationTest.php +++ b/test/CancellationToken/CancellationTest.php @@ -48,7 +48,7 @@ class CancellationTest extends AsyncTestCase public function testDoubleCancelOnlyInvokesOnce(): void { $cancellationSource = new CancellationTokenSource; - $cancellationSource->getToken()->subscribe($this->createCallback(1)); + $cancellationSource->getToken()->subscribe(\Closure::fromCallable($this->createCallback(1))); $cancellationSource->cancel(); $cancellationSource->cancel(); @@ -58,6 +58,6 @@ class CancellationTest extends AsyncTestCase { $cancellationSource = new CancellationTokenSource; $cancellationSource->cancel(); - $cancellationSource->getToken()->subscribe($this->createCallback(1)); + $cancellationSource->getToken()->subscribe(\Closure::fromCallable($this->createCallback(1))); } } diff --git a/test/Future/FutureTest.php b/test/Future/FutureTest.php index 97a1658..1b5699a 100644 --- a/test/Future/FutureTest.php +++ b/test/Future/FutureTest.php @@ -157,7 +157,6 @@ class FutureTest extends AsyncTestCase $source->cancel(); } - public function testUnhandledError(): void { $deferred = new Deferred; @@ -182,7 +181,7 @@ class FutureTest extends AsyncTestCase $deferred->error(new TestException); unset($deferred); - EventLoop::setErrorHandler($this->createCallback(0)); + EventLoop::setErrorHandler(\Closure::fromCallable($this->createCallback(0))); } public function testIgnoreUnhandledErrorFromFutureError(): void @@ -191,7 +190,7 @@ class FutureTest extends AsyncTestCase $future->ignore(); unset($future); - EventLoop::setErrorHandler($this->createCallback(0)); + EventLoop::setErrorHandler(\Closure::fromCallable($this->createCallback(0))); } public function testMapWithCompleteFuture(): void @@ -294,6 +293,7 @@ class FutureTest extends AsyncTestCase $future = $future->finally(static fn () => Future::complete()->await()); self::assertSame(1, $future->await()); } + public function testFinallyWithPendingFuture(): void { $deferred = new Deferred; @@ -319,16 +319,16 @@ class FutureTest extends AsyncTestCase * @template T * * @param float $seconds - * @param T $value + * @param T $value * * @return Future */ private function delay(float $seconds, mixed $value): Future { return launch( - /** - * @return T - */ + /** + * @return T + */ static function () use ($seconds, $value): mixed { delay($seconds); return $value;