From f3b189f33f9d594f8392522a21c90dd7442ccc69 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Thu, 5 Nov 2020 11:29:31 -0600 Subject: [PATCH] Update for Continuation --- lib/Internal/EmitSource.php | 17 ++++++++------ lib/functions.php | 6 ++--- stubs/Continuation.php | 32 +++++++++++++++++++++++++ stubs/Fiber.php | 47 +++++++------------------------------ 4 files changed, 54 insertions(+), 48 deletions(-) create mode 100644 stubs/Continuation.php diff --git a/lib/Internal/EmitSource.php b/lib/Internal/EmitSource.php index d5d52d6..cf37580 100644 --- a/lib/Internal/EmitSource.php +++ b/lib/Internal/EmitSource.php @@ -35,7 +35,7 @@ final class EmitSource /** @var Deferred[] */ private array $backPressure = []; - /** @var \Fiber[] */ + /** @var \Continuation[] */ private array $waiting = []; private int $consumePosition = 0; @@ -116,7 +116,10 @@ final class EmitSource return null; } - return \Fiber::suspend(fn(\Fiber $fiber) => $this->waiting[$position] = $fiber, Loop::get()); + return \Fiber::suspend( + fn(\Continuation $continuation) => $this->waiting[$position] = $continuation, + Loop::get() + ); } public function pipe(): Pipeline @@ -210,9 +213,9 @@ final class EmitSource $position = $this->emitPosition++; if (isset($this->waiting[$position])) { - $fiber = $this->waiting[$position]; + $continuation = $this->waiting[$position]; unset($this->waiting[$position]); - Loop::defer(fn() => $fiber->resume($value)); + Loop::defer(static fn() => $continuation->resume($value)); // Send-values are indexed as $this->consumePosition - 1, so use $position for the next value. if (isset($this->sendValues[$position])) { @@ -356,11 +359,11 @@ final class EmitSource } } - foreach ($waiting as $fiber) { + foreach ($waiting as $continuation) { if (isset($this->exception)) { - $fiber->throw($this->exception); + $continuation->throw($this->exception); } else { - $fiber->resume(); + $continuation->resume(); } } } diff --git a/lib/functions.php b/lib/functions.php index 41325c4..e69f494 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -25,10 +25,10 @@ namespace Amp $promise = Promise\all($promise); } - return \Fiber::suspend(static fn(\Fiber $fiber) => $promise->onResolve( + return \Fiber::suspend(static fn(\Continuation $continuation) => $promise->onResolve( static fn(?\Throwable $exception, mixed $value) => $exception - ? $fiber->throw($exception) - : $fiber->resume($value) + ? $continuation->throw($exception) + : $continuation->resume($value) ), Loop::get()); } diff --git a/stubs/Continuation.php b/stubs/Continuation.php new file mode 100644 index 0000000..ffca828 --- /dev/null +++ b/stubs/Continuation.php @@ -0,0 +1,32 @@ +