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:
parent
fee62e5b5e
commit
37db787ba6
@ -11,14 +11,14 @@ interface CancellationToken
|
|||||||
* Subscribes a new handler to be invoked on a cancellation request.
|
* 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
|
* 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.
|
* `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.
|
* @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.
|
* Unsubscribes a previously registered handler.
|
||||||
|
@ -42,8 +42,7 @@ final class CombinedCancellationToken implements CancellationToken
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritdoc */
|
public function subscribe(\Closure $callback): string
|
||||||
public function subscribe(callable $callback): string
|
|
||||||
{
|
{
|
||||||
$id = $this->nextId++;
|
$id = $this->nextId++;
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ final class CancellableToken implements CancellationToken
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function subscribe(callable $callback): string
|
public function subscribe(\Closure $callback): string
|
||||||
{
|
{
|
||||||
$id = $this->nextId++;
|
$id = $this->nextId++;
|
||||||
|
|
||||||
|
@ -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);
|
return $this->token->subscribe($callback);
|
||||||
}
|
}
|
||||||
|
@ -2,21 +2,6 @@
|
|||||||
|
|
||||||
namespace Amp\Internal;
|
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()`.
|
* Formats a stacktrace obtained via `debug_backtrace()`.
|
||||||
*
|
*
|
||||||
|
@ -27,8 +27,7 @@ namespace Amp;
|
|||||||
*/
|
*/
|
||||||
final class NullCancellationToken implements CancellationToken
|
final class NullCancellationToken implements CancellationToken
|
||||||
{
|
{
|
||||||
/** @inheritdoc */
|
public function subscribe(\Closure $callback): string
|
||||||
public function subscribe(callable $callback): string
|
|
||||||
{
|
{
|
||||||
return "null-token";
|
return "null-token";
|
||||||
}
|
}
|
||||||
|
@ -45,10 +45,7 @@ final class TimeoutCancellationToken implements CancellationToken
|
|||||||
EventLoop::cancel($this->watcher);
|
EventLoop::cancel($this->watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function subscribe(\Closure $callback): string
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function subscribe(callable $callback): string
|
|
||||||
{
|
{
|
||||||
return $this->token->subscribe($callback);
|
return $this->token->subscribe($callback);
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,15 @@ use Revolt\EventLoop\UnsupportedFeatureException;
|
|||||||
function launch(callable $callback): Future
|
function launch(callable $callback): Future
|
||||||
{
|
{
|
||||||
$state = new Internal\FutureState;
|
$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);
|
return new Future($state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ class CancellationTest extends AsyncTestCase
|
|||||||
public function testDoubleCancelOnlyInvokesOnce(): void
|
public function testDoubleCancelOnlyInvokesOnce(): void
|
||||||
{
|
{
|
||||||
$cancellationSource = new CancellationTokenSource;
|
$cancellationSource = new CancellationTokenSource;
|
||||||
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
$cancellationSource->getToken()->subscribe(\Closure::fromCallable($this->createCallback(1)));
|
||||||
|
|
||||||
$cancellationSource->cancel();
|
$cancellationSource->cancel();
|
||||||
$cancellationSource->cancel();
|
$cancellationSource->cancel();
|
||||||
@ -58,6 +58,6 @@ class CancellationTest extends AsyncTestCase
|
|||||||
{
|
{
|
||||||
$cancellationSource = new CancellationTokenSource;
|
$cancellationSource = new CancellationTokenSource;
|
||||||
$cancellationSource->cancel();
|
$cancellationSource->cancel();
|
||||||
$cancellationSource->getToken()->subscribe($this->createCallback(1));
|
$cancellationSource->getToken()->subscribe(\Closure::fromCallable($this->createCallback(1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,7 +157,6 @@ class FutureTest extends AsyncTestCase
|
|||||||
$source->cancel();
|
$source->cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testUnhandledError(): void
|
public function testUnhandledError(): void
|
||||||
{
|
{
|
||||||
$deferred = new Deferred;
|
$deferred = new Deferred;
|
||||||
@ -182,7 +181,7 @@ class FutureTest extends AsyncTestCase
|
|||||||
$deferred->error(new TestException);
|
$deferred->error(new TestException);
|
||||||
unset($deferred);
|
unset($deferred);
|
||||||
|
|
||||||
EventLoop::setErrorHandler($this->createCallback(0));
|
EventLoop::setErrorHandler(\Closure::fromCallable($this->createCallback(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIgnoreUnhandledErrorFromFutureError(): void
|
public function testIgnoreUnhandledErrorFromFutureError(): void
|
||||||
@ -191,7 +190,7 @@ class FutureTest extends AsyncTestCase
|
|||||||
$future->ignore();
|
$future->ignore();
|
||||||
unset($future);
|
unset($future);
|
||||||
|
|
||||||
EventLoop::setErrorHandler($this->createCallback(0));
|
EventLoop::setErrorHandler(\Closure::fromCallable($this->createCallback(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMapWithCompleteFuture(): void
|
public function testMapWithCompleteFuture(): void
|
||||||
@ -294,6 +293,7 @@ class FutureTest extends AsyncTestCase
|
|||||||
$future = $future->finally(static fn () => Future::complete()->await());
|
$future = $future->finally(static fn () => Future::complete()->await());
|
||||||
self::assertSame(1, $future->await());
|
self::assertSame(1, $future->await());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFinallyWithPendingFuture(): void
|
public function testFinallyWithPendingFuture(): void
|
||||||
{
|
{
|
||||||
$deferred = new Deferred;
|
$deferred = new Deferred;
|
||||||
@ -319,16 +319,16 @@ class FutureTest extends AsyncTestCase
|
|||||||
* @template T
|
* @template T
|
||||||
*
|
*
|
||||||
* @param float $seconds
|
* @param float $seconds
|
||||||
* @param T $value
|
* @param T $value
|
||||||
*
|
*
|
||||||
* @return Future<T>
|
* @return Future<T>
|
||||||
*/
|
*/
|
||||||
private function delay(float $seconds, mixed $value): Future
|
private function delay(float $seconds, mixed $value): Future
|
||||||
{
|
{
|
||||||
return launch(
|
return launch(
|
||||||
/**
|
/**
|
||||||
* @return T
|
* @return T
|
||||||
*/
|
*/
|
||||||
static function () use ($seconds, $value): mixed {
|
static function () use ($seconds, $value): mixed {
|
||||||
delay($seconds);
|
delay($seconds);
|
||||||
return $value;
|
return $value;
|
||||||
|
Loading…
Reference in New Issue
Block a user