Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |