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