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

54 lines
913 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 */
2018-06-18 20:00:01 +02:00
public function unsubscribe(string $id)
{
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 */
2018-06-18 20:00:01 +02:00
public function throwIfRequested()
{
2017-05-24 10:34:54 +02:00
// nothing to do
}
}