mirror of
https://github.com/danog/amp.git
synced 2024-12-02 17:37:50 +01:00
Add CombinedCancellationToken
Implementation has been used in http-client before.
This commit is contained in:
parent
f824f0df1d
commit
d4fc8ce7b3
79
lib/CombinedCancellationToken.php
Normal file
79
lib/CombinedCancellationToken.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Amp;
|
||||
|
||||
final class CombinedCancellationToken implements CancellationToken
|
||||
{
|
||||
private $tokens = [];
|
||||
|
||||
private $nextId = "a";
|
||||
private $callbacks = [];
|
||||
private $exception;
|
||||
|
||||
public function __construct(CancellationToken ...$tokens)
|
||||
{
|
||||
foreach ($tokens as $token) {
|
||||
$id = $token->subscribe(function ($exception) {
|
||||
$this->exception = $exception;
|
||||
|
||||
$callbacks = $this->callbacks;
|
||||
$this->callbacks = [];
|
||||
|
||||
foreach ($callbacks as $callback) {
|
||||
asyncCall($callback, $this->exception);
|
||||
}
|
||||
});
|
||||
|
||||
$this->tokens[] = [$token, $id];
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
foreach ($this->tokens as [$token, $id]) {
|
||||
/** @var CancellationToken $token */
|
||||
$token->unsubscribe($id);
|
||||
}
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
$id = $this->nextId++;
|
||||
|
||||
if ($this->exception) {
|
||||
asyncCall($callback, $this->exception);
|
||||
} else {
|
||||
$this->callbacks[$id] = $callback;
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function unsubscribe(string $id)
|
||||
{
|
||||
unset($this->callbacks[$id]);
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function isRequested(): bool
|
||||
{
|
||||
foreach ($this->tokens as [$token]) {
|
||||
if ($token->isRequested()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function throwIfRequested()
|
||||
{
|
||||
foreach ($this->tokens as [$token]) {
|
||||
$token->throwIfRequested();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user