Amp\CancellationTokenSource class CancellationTokenSource

A cancellation token source provides a mechanism to cancel operations.

Cancellation of operation works by creating a cancellation token source and passing the corresponding token when starting the operation. To cancel the operation, invoke CancellationTokenSource::cancel().

Any operation can decide what to do on a cancellation request, it has "don't care" semantics. An operation SHOULD be aborted, but MAY continue. Example: A DNS client might continue to receive and cache the response, as the query has been sent anyway. An HTTP client would usually close a connection, but might not do so in case a response is close to be fully received to reuse the connection.

Example

$tokenSource = new CancellationTokenSource;
$token = $tokenSource->getToken();

$response = yield $httpClient->request("https://example.com/stream", $token);
$responseBody = $response->getBody();

while (($chunk = yield $response->read()) !== null) {
    // consume $chunk

    if ($noLongerInterested) {
        $cancellationTokenSource->cancel();
        break;
    }
}

Summary

Properties

Methods

Properties

Methods

__construct — public function __construct(&$onCancel)
No documentation.
Type Parameter Description Default Value
mixed & $onCancel No documentation. none
subscribe — public function subscribe(callable $callback): string
No documentation.
Type Parameter Description Default Value
callable $callback No documentation. none
unsubscribe — public function unsubscribe(string $id)
No documentation.
Type Parameter Description Default Value
string $id No documentation. none
isRequested — public function isRequested(): bool
No documentation.
throwIfRequested — public function throwIfRequested()
No documentation.
getToken — public function getToken(): Amp\CancellationToken
No documentation.
cancel — public function cancel(Throwable|null $previous = null)
No documentation.
Type Parameter Description Default Value
Throwable|null $previous Exception to be used as the previous exception to CancelledException. null