AmpFutureAdapter.php
2 months ago
AmpPromiseAdapter.php
2 months ago
ReactPromiseAdapter.php
2 months ago
SyncPromise.php
2 months ago
SyncPromiseAdapter.php
2 months ago
SyncPromiseQueue.php
2 months ago
SyncPromise.php
213 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\Adapter; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\InvariantViolation; |
| 6 | |
| 7 | /** |
| 8 | * Synchronous promise implementation following Promises A+ spec. |
| 9 | * |
| 10 | * Uses a hybrid approach for optimal memory and performance: |
| 11 | * - Lightweight closures in queue (fast execution) |
| 12 | * - Heavy payload (callbacks) stored on promise objects and cleared after use |
| 13 | * |
| 14 | * Library users should use @see \Automattic\WooCommerce\Vendor\GraphQL\Deferred to create promises. |
| 15 | * |
| 16 | * @phpstan-type Executor callable(): mixed |
| 17 | */ |
| 18 | class SyncPromise |
| 19 | { |
| 20 | /** |
| 21 | * TODO remove in next major version. |
| 22 | * |
| 23 | * @deprecated Use SyncPromiseQueue::run() instead |
| 24 | */ |
| 25 | public static function runQueue(): void |
| 26 | { |
| 27 | SyncPromiseQueue::run(); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * TODO remove in next major version. |
| 32 | * |
| 33 | * @deprecated Use SyncPromiseQueue methods instead |
| 34 | * |
| 35 | * @return \SplQueue<callable(): void> |
| 36 | */ |
| 37 | public static function getQueue(): \SplQueue |
| 38 | { |
| 39 | return SyncPromiseQueue::queue(); |
| 40 | } |
| 41 | |
| 42 | public const PENDING = 0; |
| 43 | public const FULFILLED = 1; |
| 44 | public const REJECTED = 2; |
| 45 | |
| 46 | /** |
| 47 | * Current promise state. |
| 48 | * |
| 49 | * @var 0|1|2 |
| 50 | */ |
| 51 | public int $state = self::PENDING; |
| 52 | |
| 53 | /** |
| 54 | * Resolved value or rejection reason. |
| 55 | * |
| 56 | * @var mixed |
| 57 | */ |
| 58 | public $result; |
| 59 | |
| 60 | /** |
| 61 | * Promises created in `then` method awaiting resolution. |
| 62 | * |
| 63 | * @var array< |
| 64 | * int, |
| 65 | * array{ |
| 66 | * self, |
| 67 | * (callable(mixed): mixed)|null, |
| 68 | * (callable(\Throwable): mixed)|null, |
| 69 | * }, |
| 70 | * > |
| 71 | */ |
| 72 | protected array $waiting = []; |
| 73 | |
| 74 | /** |
| 75 | * @param mixed $value |
| 76 | * |
| 77 | * @throws \Exception |
| 78 | */ |
| 79 | public function resolve($value): self |
| 80 | { |
| 81 | switch ($this->state) { |
| 82 | case self::PENDING: |
| 83 | if ($value === $this) { |
| 84 | throw new \Exception('Cannot resolve promise with self.'); |
| 85 | } |
| 86 | |
| 87 | if (is_object($value) && method_exists($value, 'then')) { |
| 88 | $value->then( |
| 89 | function ($resolvedValue): void { |
| 90 | $this->resolve($resolvedValue); |
| 91 | }, |
| 92 | function (\Throwable $reason): void { |
| 93 | $this->reject($reason); |
| 94 | } |
| 95 | ); |
| 96 | |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | $this->state = self::FULFILLED; |
| 101 | $this->result = $value; |
| 102 | $this->enqueueWaitingPromises(); |
| 103 | break; |
| 104 | case self::FULFILLED: |
| 105 | if ($this->result !== $value) { |
| 106 | throw new \Exception('Cannot change value of fulfilled promise.'); |
| 107 | } |
| 108 | |
| 109 | break; |
| 110 | case self::REJECTED: |
| 111 | throw new \Exception('Cannot resolve rejected promise.'); |
| 112 | } |
| 113 | |
| 114 | return $this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @throws \Exception |
| 119 | * |
| 120 | * @return $this |
| 121 | */ |
| 122 | public function reject(\Throwable $reason): self |
| 123 | { |
| 124 | switch ($this->state) { |
| 125 | case self::PENDING: |
| 126 | $this->state = self::REJECTED; |
| 127 | $this->result = $reason; |
| 128 | $this->enqueueWaitingPromises(); |
| 129 | break; |
| 130 | case self::REJECTED: |
| 131 | if ($reason !== $this->result) { |
| 132 | throw new \Exception('Cannot change rejection reason.'); |
| 133 | } |
| 134 | |
| 135 | break; |
| 136 | case self::FULFILLED: |
| 137 | throw new \Exception('Cannot reject fulfilled promise.'); |
| 138 | } |
| 139 | |
| 140 | return $this; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param (callable(mixed): mixed)|null $onFulfilled |
| 145 | * @param (callable(\Throwable): mixed)|null $onRejected |
| 146 | * |
| 147 | * @throws InvariantViolation |
| 148 | */ |
| 149 | public function then(?callable $onFulfilled = null, ?callable $onRejected = null): self |
| 150 | { |
| 151 | if ($this->state === self::REJECTED |
| 152 | && $onRejected === null |
| 153 | ) { |
| 154 | return $this; |
| 155 | } |
| 156 | |
| 157 | if ($this->state === self::FULFILLED |
| 158 | && $onFulfilled === null |
| 159 | ) { |
| 160 | return $this; |
| 161 | } |
| 162 | |
| 163 | $child = new self(); |
| 164 | |
| 165 | $this->waiting[] = [$child, $onFulfilled, $onRejected]; |
| 166 | |
| 167 | if ($this->state !== self::PENDING) { |
| 168 | $this->enqueueWaitingPromises(); |
| 169 | } |
| 170 | |
| 171 | return $child; |
| 172 | } |
| 173 | |
| 174 | /** @throws InvariantViolation */ |
| 175 | private function enqueueWaitingPromises(): void |
| 176 | { |
| 177 | if ($this->state === self::PENDING) { |
| 178 | throw new InvariantViolation('Cannot enqueue derived promises when parent is still pending.'); |
| 179 | } |
| 180 | |
| 181 | $waiting = $this->waiting; |
| 182 | if ($waiting === []) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | $this->waiting = []; |
| 187 | |
| 188 | $result = $this->result; |
| 189 | |
| 190 | SyncPromiseQueue::enqueue(static function () use ($waiting, $result): void { |
| 191 | foreach ($waiting as [$child, $onFulfilled, $onRejected]) { |
| 192 | try { |
| 193 | if ($result instanceof \Throwable) { |
| 194 | if ($onRejected === null) { |
| 195 | $child->reject($result); |
| 196 | } else { |
| 197 | $child->resolve($onRejected($result)); |
| 198 | } |
| 199 | } else { |
| 200 | $child->resolve( |
| 201 | $onFulfilled === null |
| 202 | ? $result |
| 203 | : $onFulfilled($result) |
| 204 | ); |
| 205 | } |
| 206 | } catch (\Throwable $e) { |
| 207 | $child->reject($e); |
| 208 | } |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | } |
| 213 |