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 7bed8a1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
24 changes: 20 additions & 4 deletions src/Adapter/Swoole/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.'
);
}
}
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -84,13 +92,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 All @@ -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);
});

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

return new YieldPromise();
}

/**
Expand Down
35 changes: 31 additions & 4 deletions src/Adapter/Swoole/Internal/YieldPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -25,17 +32,30 @@ 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;
}

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) {
Expand All @@ -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 = [];
}
}

0 comments on commit 7bed8a1

Please sign in to comment.