Documentation

NullCancellation
in package
implements Cancellation

FinalYes

A NullCancellation can be used to avoid conditionals to check whether a cancellation has been provided.

Instead of writing

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

potentially multiple times, it allows writing

$cancellation = $cancellation ?? new NullCancellation;

// ...

$cancellation->throwIfRequested();

instead.

Table of Contents

Interfaces

Cancellation
Cancellations are simple objects that allow registering handlers to subscribe to cancellation requests.

Methods

isRequested()  : bool
Returns whether cancellation has been requested yet.
subscribe()  : string
Subscribes a new handler to be invoked on a cancellation request.
throwIfRequested()  : void
Throws the `CancelledException` if cancellation has been requested, otherwise does nothing.
unsubscribe()  : void
Unsubscribes a previously registered handler.

Methods

isRequested()

Returns whether cancellation has been requested yet.

public isRequested() : bool
Return values
bool

subscribe()

Subscribes a new handler to be invoked on a cancellation request.

public subscribe(Closure $callback) : string

This handler might be invoked immediately in case the cancellation has already been requested. Any unhandled exceptions will be thrown into the event loop.

Parameters
$callback : Closure

Callback to be invoked on a cancellation request. Will receive a CancelledException as first argument that may be used to fail the operation.

Return values
string

Identifier that can be used to cancel the subscription.

throwIfRequested()

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

public throwIfRequested() : void

unsubscribe()

Unsubscribes a previously registered handler.

public unsubscribe(string $id) : void

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

Parameters
$id : string

        
On this page

Search results