mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Merge pull request #139 from amphp/null-cancel
Implement NullCancellationToken
This commit is contained in:
commit
550713c74b
48
lib/NullCancellationToken.php
Normal file
48
lib/NullCancellationToken.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?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.
|
||||
*/
|
||||
class NullCancellationToken implements CancellationToken {
|
||||
/** @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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user