1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 09:57:51 +01:00
amp/lib/NullCancellationToken.php

49 lines
897 B
PHP
Raw Normal View History

2017-05-24 10:34:54 +02:00
<?php
namespace Amp;
/**
* A NullCancellationToken can be used to avoid conditionals to check whether a token has been provided.
*
* Instead of writing
*
* ```php
* if ($token) {
* $token->throwIfRequested();
* }
* ```
*
* potentially multiple times, it allows writing
*
* ```php
* $token = $token ?? new NullCancellationToken;
*
* // ...
*
* $token->throwIfRequested();
* ```
*
* instead.
*/
2017-06-09 17:22:11 +02:00
final class NullCancellationToken implements CancellationToken {
2017-05-24 10:34:54 +02:00
/** @inheritdoc */
public function subscribe(callable $callback): string {
return "null-token";
}
/** @inheritdoc */
public function unsubscribe(string $id) {
// nothing to do
}
/** @inheritdoc */
public function isRequested(): bool {
return false;
}
/** @inheritdoc */
public function throwIfRequested() {
// nothing to do
}
}