1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 09:27:46 +01:00

Use \Closure instead of callable

This commit is contained in:
Niklas Keller 2021-12-02 18:40:51 +01:00
parent fee62e5b5e
commit 37db787ba6
10 changed files with 26 additions and 38 deletions

View File

@ -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.

View File

@ -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++;

View File

@ -37,7 +37,7 @@ final class CancellableToken implements CancellationToken
}
}
public function subscribe(callable $callback): string
public function subscribe(\Closure $callback): string
{
$id = $this->nextId++;

View File

@ -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);
}

View File

@ -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()`.
*

View File

@ -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";
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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)));
}
}

View File

@ -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<T>
*/
private function delay(float $seconds, mixed $value): Future
{
return launch(
/**
* @return T
*/
/**
* @return T
*/
static function () use ($seconds, $value): mixed {
delay($seconds);
return $value;