From 98c7e6e6e391d4dcc0bd594fb8f84cdb1a407a21 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} --- src/Adapter/Swoole/EventLoop.php | 15 ++++++++++++--- src/Adapter/Swoole/Internal/YieldPromise.php | 9 ++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Adapter/Swoole/EventLoop.php b/src/Adapter/Swoole/EventLoop.php index ffa8e0e..226ed16 100644 --- a/src/Adapter/Swoole/EventLoop.php +++ b/src/Adapter/Swoole/EventLoop.php @@ -2,6 +2,7 @@ namespace M6Web\Tornado\Adapter\Swoole; +use JetBrains\PhpStorm\Pure; use M6Web\Tornado\Adapter\Swoole\Internal\YieldPromise; use M6Web\Tornado\Deferred; use M6Web\Tornado\Promise; @@ -16,7 +17,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.' ); } } @@ -84,6 +85,10 @@ public function promiseRace(Promise ...$promises): Promise */ public function promiseFulfilled($value): Promise { + $promise = new YieldPromise(); + $promise->resolve($value); + + return $promise; } /** @@ -91,6 +96,10 @@ public function promiseFulfilled($value): Promise */ public function promiseRejected(\Throwable $throwable): Promise { + $promise = new YieldPromise(); + $promise->reject($throwable); + + return $promise; } /** @@ -125,9 +134,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..43a643c 100644 --- a/src/Adapter/Swoole/Internal/YieldPromise.php +++ b/src/Adapter/Swoole/Internal/YieldPromise.php @@ -28,7 +28,14 @@ public function value() 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; }