-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
405 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace M6Web\Tornado\Adapter\Swoole\Internal; | ||
|
||
use M6Web\Tornado\Promise; | ||
use M6Web\Tornado\Deferred; | ||
use Throwable; | ||
|
||
final class DummyPromise implements Promise, Deferred | ||
{ | ||
private bool $isPending; | ||
private mixed $value; | ||
private ?Throwable $exception; | ||
private $callback; | ||
|
||
public function __construct(?callable $callback = null) | ||
{ | ||
$this->isPending = true; | ||
$this->exception = null; | ||
$this->callback = $callback ?? static function() {}; | ||
} | ||
|
||
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(true === $this->isPending, new \Error('Promise is already resolved.')); | ||
|
||
$this->isPending = false; | ||
$this->value = $value; | ||
($this->callback)(); | ||
} | ||
|
||
public function reject(Throwable $throwable): void | ||
{ | ||
assert(true === $this->isPending, new \Error('Promise is already resolved.')); | ||
|
||
$this->isPending = false; | ||
$this->exception = $throwable; | ||
($this->callback)(); | ||
} | ||
|
||
public function isPending(): bool | ||
{ | ||
if($this->isPending) { | ||
return $this->isPending; | ||
} | ||
|
||
if($this->exception === null) { | ||
if ($this->value instanceof self) { | ||
return $this->value->isPending(); | ||
} | ||
|
||
if(is_array($this->value)) { | ||
foreach ($this->value as $value) { | ||
if($value instanceof self && $value->isPending()) { | ||
return $value->isPending(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $this->isPending; | ||
} | ||
|
||
public function getValue(): mixed | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function getException(): ?Throwable | ||
{ | ||
return $this->exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.