subscribe(function (CancelledException $exception) { $this->exception = $exception; $callbacks = $this->callbacks; $this->callbacks = []; foreach ($callbacks as $callback) { Loop::defer(static fn (): Promise => call($callback, $this->exception)); } }); $this->tokens[] = [$token, $id]; } } public function __destruct() { foreach ($this->tokens as list($token, $id)) { /** @var CancellationToken $token */ $token->unsubscribe($id); } } /** @inheritdoc */ public function subscribe(callable $callback): string { $id = $this->nextId++; if ($this->exception) { Loop::defer(static fn (): Promise => call($callback)($this->exception)); } else { $this->callbacks[$id] = $callback; } return $id; } /** @inheritdoc */ public function unsubscribe(string $id): void { unset($this->callbacks[$id]); } /** @inheritdoc */ public function isRequested(): bool { foreach ($this->tokens as list($token)) { if ($token->isRequested()) { return true; } } return false; } /** @inheritdoc */ public function throwIfRequested(): void { foreach ($this->tokens as list($token)) { $token->throwIfRequested(); } } }