Skip to content

Commit

Permalink
🚧 integrate EventLoop->{deferred,promiseFulfilled,promiseRejected}
Browse files Browse the repository at this point in the history
  • Loading branch information
matyo91 committed Apr 21, 2021
1 parent 3092714 commit 98c7e6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/Adapter/Swoole/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.'
);
}
}
Expand Down Expand Up @@ -84,13 +85,21 @@ public function promiseRace(Promise ...$promises): Promise
*/
public function promiseFulfilled($value): Promise
{
$promise = new YieldPromise();
$promise->resolve($value);

return $promise;
}

/**
* {@inheritdoc}
*/
public function promiseRejected(\Throwable $throwable): Promise
{
$promise = new YieldPromise();
$promise->reject($throwable);

return $promise;
}

/**
Expand Down Expand Up @@ -125,9 +134,9 @@ public function delay(int $milliseconds): Promise
/**
* {@inheritdoc}
*/
public function deferred(): Deferred
#[Pure] public function deferred(): Deferred
{

return new YieldPromise();
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Adapter/Swoole/Internal/YieldPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 98c7e6e

Please sign in to comment.