1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 00:49:48 +01:00
amp/lib/NullCancellationToken.php

54 lines
925 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.
*/
2018-06-18 20:00:01 +02:00
final class NullCancellationToken implements CancellationToken
{
2017-05-24 10:34:54 +02:00
/** @inheritdoc */
2018-06-18 20:00:01 +02:00
public function subscribe(callable $callback): string
{
2017-05-24 10:34:54 +02:00
return "null-token";
}
/** @inheritdoc */
public function unsubscribe(string $id): void
2018-06-18 20:00:01 +02:00
{
2017-05-24 10:34:54 +02:00
// nothing to do
}
/** @inheritdoc */
2018-06-18 20:00:01 +02:00
public function isRequested(): bool
{
2017-05-24 10:34:54 +02:00
return false;
}
/** @inheritdoc */
public function throwIfRequested(): void
2018-06-18 20:00:01 +02:00
{
2017-05-24 10:34:54 +02:00
// nothing to do
}
}