1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00

defer instead of Loop::defer

Callbacks should be run in a fiber in case they await.
This commit is contained in:
Aaron Piotrowski 2020-11-01 11:14:03 -06:00
parent eaa7c45049
commit 3ebd44ee09
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 5 additions and 27 deletions

View File

@ -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));
}
}

View File

@ -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;
}