From 7bed8a1cabed2b20dcd244025532a91aa2cf401c Mon Sep 17 00:00:00 2001 From: Mathieu Ledru Date: Wed, 21 Apr 2021 21:47:13 +0200 Subject: [PATCH] :construction: integrate EventLoop->{deferred,promiseFulfilled,promiseRejected} --- .../{00--swoole.php => 00--swooleABCABA.php} | 0 ...--swoole1.php => 00--swooleSimpleWait.php} | 0 src/Adapter/Swoole/EventLoop.php | 24 ++++++++++--- src/Adapter/Swoole/Internal/YieldPromise.php | 35 ++++++++++++++++--- 4 files changed, 51 insertions(+), 8 deletions(-) rename examples/{00--swoole.php => 00--swooleABCABA.php} (100%) rename examples/{00--swoole1.php => 00--swooleSimpleWait.php} (100%) diff --git a/examples/00--swoole.php b/examples/00--swooleABCABA.php similarity index 100% rename from examples/00--swoole.php rename to examples/00--swooleABCABA.php diff --git a/examples/00--swoole1.php b/examples/00--swooleSimpleWait.php similarity index 100% rename from examples/00--swoole1.php rename to examples/00--swooleSimpleWait.php diff --git a/src/Adapter/Swoole/EventLoop.php b/src/Adapter/Swoole/EventLoop.php index ffa8e0e..fab6fed 100644 --- a/src/Adapter/Swoole/EventLoop.php +++ b/src/Adapter/Swoole/EventLoop.php @@ -2,10 +2,12 @@ namespace M6Web\Tornado\Adapter\Swoole; +use JetBrains\PhpStorm\Pure; use M6Web\Tornado\Adapter\Swoole\Internal\YieldPromise; use M6Web\Tornado\Deferred; use M6Web\Tornado\Promise; use Swoole\Coroutine; +use Swoole\Coroutine\WaitGroup; use Swoole\Event; use RuntimeException; use function extension_loaded; @@ -16,7 +18,7 @@ public function __construct() { if (!extension_loaded('swoole')) { throw new RuntimeException( - 'SwoolePromise MUST running only in CLI mode with swoole extension.' + 'EventLoop must running only with swoole extension.' ); } } @@ -60,7 +62,13 @@ public function async(\Generator $generator): Promise */ public function promiseAll(Promise ...$promises): Promise { + $promise = new YieldPromise(); + + foreach ($promises as $p) { + + } + return $promise; } /** @@ -84,6 +92,10 @@ public function promiseRace(Promise ...$promises): Promise */ public function promiseFulfilled($value): Promise { + $promise = new YieldPromise(); + $promise->resolve($value); + + return $promise; } /** @@ -91,6 +103,10 @@ public function promiseFulfilled($value): Promise */ public function promiseRejected(\Throwable $throwable): Promise { + $promise = new YieldPromise(); + $promise->reject($throwable); + + return $promise; } /** @@ -115,7 +131,7 @@ public function delay(int $milliseconds): Promise { $promise = new YieldPromise(); Coroutine::create(function() use($milliseconds, $promise) { - Coroutine::sleep($milliseconds / 1000 /* ms -> s */); + Coroutine::sleep($milliseconds / 1000); $promise->resolve(null); }); @@ -125,9 +141,9 @@ public function delay(int $milliseconds): Promise /** * {@inheritdoc} */ - public function deferred(): Deferred + #[Pure] public function deferred(): Deferred { - + return new YieldPromise(); } /** diff --git a/src/Adapter/Swoole/Internal/YieldPromise.php b/src/Adapter/Swoole/Internal/YieldPromise.php index 3b1eef0..60478eb 100644 --- a/src/Adapter/Swoole/Internal/YieldPromise.php +++ b/src/Adapter/Swoole/Internal/YieldPromise.php @@ -8,9 +8,16 @@ final class YieldPromise implements Promise, Deferred { - private $cids = []; - private $isSettled = false; + private $cids; + private $isSettled; private $value; + private $exception; + + public function __construct() + { + $this->cids = []; + $this->isSettled = false; + } public function yield(): void { @@ -25,10 +32,22 @@ public function yield(): void public function value() { assert($this->isSettled, new \Error('Promise is not resolved.')); + + if($this->exception) { + return $this->exception; + } + return $this->value; } - public function getPromise(): YieldPromise + public static function wrap(Promise $promise): self + { + assert($promise instanceof self, new \Error('Input promise was not created by this adapter.')); + + return $promise; + } + + public function getPromise(): Promise { return $this; } @@ -36,6 +55,7 @@ public function getPromise(): YieldPromise public function resolve($value): void { assert(false === $this->isSettled, new \Error('Promise is already resolved.')); + $this->isSettled = true; $this->value = $value; foreach ($this->cids as $cid => $dummy) { @@ -46,6 +66,13 @@ public function resolve($value): void public function reject(\Throwable $throwable): void { - + assert(false === $this->isSettled, new \Error('Promise is already resolved.')); + + $this->isSettled = true; + $this->exception = $throwable; + foreach ($this->cids as $cid => $dummy) { + Coroutine::resume($cid); + } + $this->cids = []; } }