2017-05-14 22:46:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancellation tokens are simple objects that allow registering handlers to subscribe to cancellation requests.
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
interface CancellationToken
|
|
|
|
{
|
2017-05-14 22:46:33 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param 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.
|
|
|
|
*
|
|
|
|
* @return string Identifier that can be used to cancel the subscription.
|
|
|
|
*/
|
|
|
|
public function subscribe(callable $callback): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsubscribes a previously registered handler.
|
|
|
|
*
|
|
|
|
* The handler will no longer be called as long as this method isn't invoked from a subscribed callback.
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function unsubscribe(string $id);
|
2017-05-22 19:26:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether cancellation has been requested yet.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRequested(): bool;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws the `CancelledException` if cancellation has been requested, otherwise does nothing.
|
|
|
|
*
|
|
|
|
* @return void
|
2019-05-30 15:13:28 +02:00
|
|
|
*
|
|
|
|
* @throws CancelledException
|
2017-05-22 19:26:09 +02:00
|
|
|
*/
|
|
|
|
public function throwIfRequested();
|
2017-05-14 22:46:33 +02:00
|
|
|
}
|