| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Promise; |
| 5 |
|
| 6 |
use Generator; |
| 7 |
use Throwable; |
| 8 |
/** |
| 9 |
* Creates a promise that is resolved using a generator that yields values or |
| 10 |
* promises (somewhat similar to C#'s async keyword). |
| 11 |
* |
| 12 |
* When called, the Coroutine::of method will start an instance of the generator |
| 13 |
* and returns a promise that is fulfilled with its final yielded value. |
| 14 |
* |
| 15 |
* Control is returned back to the generator when the yielded promise settles. |
| 16 |
* This can lead to less verbose code when doing lots of sequential async calls |
| 17 |
* with minimal processing in between. |
| 18 |
* |
| 19 |
* use GuzzleHttp\Promise; |
| 20 |
* |
| 21 |
* function createPromise($value) { |
| 22 |
* return new Promise\FulfilledPromise($value); |
| 23 |
* } |
| 24 |
* |
| 25 |
* $promise = Promise\Coroutine::of(function () { |
| 26 |
* $value = (yield createPromise('a')); |
| 27 |
* try { |
| 28 |
* $value = (yield createPromise($value . 'b')); |
| 29 |
* } catch (\Throwable $e) { |
| 30 |
* // The promise was rejected. |
| 31 |
* } |
| 32 |
* yield $value . 'c'; |
| 33 |
* }); |
| 34 |
* |
| 35 |
* // Outputs "abc" |
| 36 |
* $promise->then(function ($v) { echo $v; }); |
| 37 |
* |
| 38 |
* @param callable $generatorFn Generator function to wrap into a promise. |
| 39 |
* |
| 40 |
* @return Promise |
| 41 |
* |
| 42 |
* @see https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration |
| 43 |
*/ |
| 44 |
final class Coroutine implements PromiseInterface |
| 45 |
{ |
| 46 |
/** |
| 47 |
* @var PromiseInterface|null |
| 48 |
*/ |
| 49 |
private $currentPromise; |
| 50 |
/** |
| 51 |
* @var Generator |
| 52 |
*/ |
| 53 |
private $generator; |
| 54 |
/** |
| 55 |
* @var Promise |
| 56 |
*/ |
| 57 |
private $result; |
| 58 |
public function __construct(callable $generatorFn) |
| 59 |
{ |
| 60 |
$this->generator = $generatorFn(); |
| 61 |
$this->result = new Promise(function () : void { |
| 62 |
while (isset($this->currentPromise)) { |
| 63 |
$this->currentPromise->wait(); |
| 64 |
} |
| 65 |
}); |
| 66 |
try { |
| 67 |
$this->nextCoroutine($this->generator->current()); |
| 68 |
} catch (Throwable $throwable) { |
| 69 |
$this->result->reject($throwable); |
| 70 |
} |
| 71 |
} |
| 72 |
/** |
| 73 |
* Create a new coroutine. |
| 74 |
*/ |
| 75 |
public static function of(callable $generatorFn) : self |
| 76 |
{ |
| 77 |
return new self($generatorFn); |
| 78 |
} |
| 79 |
public function then(?callable $onFulfilled = null, ?callable $onRejected = null) : PromiseInterface |
| 80 |
{ |
| 81 |
return $this->result->then($onFulfilled, $onRejected); |
| 82 |
} |
| 83 |
public function otherwise(callable $onRejected) : PromiseInterface |
| 84 |
{ |
| 85 |
return $this->result->otherwise($onRejected); |
| 86 |
} |
| 87 |
public function wait(bool $unwrap = \true) |
| 88 |
{ |
| 89 |
return $this->result->wait($unwrap); |
| 90 |
} |
| 91 |
public function getState() : string |
| 92 |
{ |
| 93 |
return $this->result->getState(); |
| 94 |
} |
| 95 |
public function resolve($value) : void |
| 96 |
{ |
| 97 |
$this->result->resolve($value); |
| 98 |
} |
| 99 |
public function reject($reason) : void |
| 100 |
{ |
| 101 |
$this->result->reject($reason); |
| 102 |
} |
| 103 |
public function cancel() : void |
| 104 |
{ |
| 105 |
$this->currentPromise->cancel(); |
| 106 |
$this->result->cancel(); |
| 107 |
} |
| 108 |
private function nextCoroutine($yielded) : void |
| 109 |
{ |
| 110 |
$this->currentPromise = Create::promiseFor($yielded)->then([$this, '_handleSuccess'], [$this, '_handleFailure']); |
| 111 |
} |
| 112 |
/** |
| 113 |
* @internal |
| 114 |
*/ |
| 115 |
public function _handleSuccess($value) : void |
| 116 |
{ |
| 117 |
unset($this->currentPromise); |
| 118 |
try { |
| 119 |
$next = $this->generator->send($value); |
| 120 |
if ($this->generator->valid()) { |
| 121 |
$this->nextCoroutine($next); |
| 122 |
} else { |
| 123 |
$this->result->resolve($value); |
| 124 |
} |
| 125 |
} catch (Throwable $throwable) { |
| 126 |
$this->result->reject($throwable); |
| 127 |
} |
| 128 |
} |
| 129 |
/** |
| 130 |
* @internal |
| 131 |
*/ |
| 132 |
public function _handleFailure($reason) : void |
| 133 |
{ |
| 134 |
unset($this->currentPromise); |
| 135 |
try { |
| 136 |
$nextYield = $this->generator->throw(Create::exceptionFor($reason)); |
| 137 |
// The throw was caught, so keep iterating on the coroutine |
| 138 |
$this->nextCoroutine($nextYield); |
| 139 |
} catch (Throwable $throwable) { |
| 140 |
$this->result->reject($throwable); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|