mirror of
https://github.com/danog/amp.git
synced 2024-12-02 17:37:50 +01:00
d48e6bd5d2
More PHP 7.1 to 8 types added.
54 lines
925 B
PHP
54 lines
925 B
PHP
<?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.
|
|
*/
|
|
final class NullCancellationToken implements CancellationToken
|
|
{
|
|
/** @inheritdoc */
|
|
public function subscribe(callable $callback): string
|
|
{
|
|
return "null-token";
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
public function unsubscribe(string $id): void
|
|
{
|
|
// nothing to do
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
public function isRequested(): bool
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/** @inheritdoc */
|
|
public function throwIfRequested(): void
|
|
{
|
|
// nothing to do
|
|
}
|
|
}
|