Skip to content

Commit

Permalink
🚧 introduce Yield promise
Browse files Browse the repository at this point in the history
  • Loading branch information
matyo91 committed Apr 21, 2021
1 parent 1894ee6 commit b19c9b2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
1 change: 0 additions & 1 deletion src/Adapter/Swoole/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function promiseRace(Promise ...$promises): Promise
*/
public function promiseFulfilled($value): Promise
{
new Pro
}

/**
Expand Down
26 changes: 0 additions & 26 deletions src/Adapter/Swoole/Internal/Promise.php

This file was deleted.

51 changes: 51 additions & 0 deletions src/Adapter/Swoole/Internal/YieldPromise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace M6Web\Tornado\Adapter\Swoole\Internal;

use M6Web\Tornado\Promise;
use M6Web\Tornado\Deferred;
use Swoole\Coroutine;

final class YieldPromise implements Promise, Deferred
{
private $cids = [];
private $isSettled = false;
private $value;

public function yield(): void
{
if($this->isSettled) {
return;
}

$this->cids[Coroutine::getCid()] = true;
Coroutine::yield();
}

public function value()
{
assert($this->isSettled, new \Error('Promise is not resolved.'));
return $this->value;
}

public function getPromise(): YieldPromise
{
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) {
Coroutine::resume($cid);
}
$this->cids = [];
}

public function reject(\Throwable $throwable): void
{

}
}

0 comments on commit b19c9b2

Please sign in to comment.