Amp\NullCancellationToken class NullCancellationToken implements Amp\CancellationToken

A NullCancellationToken can be used to avoid conditionals to check whether a token has been provided.

Instead of writing

if ($token) {
    $token->throwIfRequested();
}

potentially multiple times, it allows writing

$token = $token ?? new NullCancellationToken;

// ...

$token->throwIfRequested();

instead.

Summary

Properties

Methods

Methods

subscribe — public function subscribe(callable $callback): string

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.

Type Parameter Description Default Value
callable $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. none
unsubscribe — public function unsubscribe(string $id)

Unsubscribes a previously registered handler.

The handler will no longer be called as long as this method isn't invoked from a subscribed callback.

Type Parameter Description Default Value
string $id No documentation. none
isRequested — public function isRequested(): bool

Returns whether cancellation has been requested yet.

throwIfRequested — public function throwIfRequested()

Throws the CancelledException if cancellation has been requested, otherwise does nothing.