mirror of
https://github.com/danog/amp.git
synced 2025-01-22 13:21:16 +01:00
defer instead of Loop::defer
Callbacks should be run in a fiber in case they await.
This commit is contained in:
parent
eaa7c45049
commit
3ebd44ee09
@ -68,39 +68,17 @@ final class CancellationTokenSource
|
||||
$this->callbacks = [];
|
||||
|
||||
foreach ($callbacks as $callback) {
|
||||
$this->invokeCallback($callback);
|
||||
defer($callback, $this->exception);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function invokeCallback(callable $callback): void
|
||||
{
|
||||
// No type declaration to prevent exception outside the try!
|
||||
try {
|
||||
/** @var mixed $result */
|
||||
$result = $callback($this->exception);
|
||||
|
||||
if ($result instanceof Promise) {
|
||||
Promise\rethrow($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
Loop::defer(static function () use ($exception): void {
|
||||
throw $exception;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function subscribe(callable $callback): string
|
||||
{
|
||||
$id = $this->nextId++;
|
||||
|
||||
if ($this->exception) {
|
||||
$this->invokeCallback($callback);
|
||||
defer($callback, $this->exception);
|
||||
} else {
|
||||
$this->callbacks[$id] = $callback;
|
||||
}
|
||||
@ -143,6 +121,6 @@ final class CancellationTokenSource
|
||||
|
||||
$onCancel = $this->onCancel;
|
||||
$this->onCancel = null;
|
||||
Loop::defer(static fn () => $onCancel(new CancelledException($previous)));
|
||||
$onCancel(new CancelledException($previous));
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ final class CombinedCancellationToken implements CancellationToken
|
||||
$this->callbacks = [];
|
||||
|
||||
foreach ($callbacks as $callback) {
|
||||
Loop::defer(fn() => $callback($this->exception));
|
||||
defer($callback, $this->exception);
|
||||
}
|
||||
});
|
||||
|
||||
@ -46,7 +46,7 @@ final class CombinedCancellationToken implements CancellationToken
|
||||
$id = $this->nextId++;
|
||||
|
||||
if (isset($this->exception)) {
|
||||
Loop::defer(fn() => $callback($this->exception));
|
||||
defer($callback, $this->exception);
|
||||
} else {
|
||||
$this->callbacks[$id] = $callback;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user