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
ReactPromiseAdapter.php
81 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 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\Promise; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\PromiseAdapter; |
| 8 | use React\Promise\Promise as ReactPromise; |
| 9 | use React\Promise\PromiseInterface as ReactPromiseInterface; |
| 10 | |
| 11 | use function React\Promise\all; |
| 12 | use function React\Promise\reject; |
| 13 | use function React\Promise\resolve; |
| 14 | |
| 15 | class ReactPromiseAdapter implements PromiseAdapter |
| 16 | { |
| 17 | public function isThenable($value): bool |
| 18 | { |
| 19 | return $value instanceof ReactPromiseInterface; |
| 20 | } |
| 21 | |
| 22 | /** @throws InvariantViolation */ |
| 23 | public function convertThenable($thenable): Promise |
| 24 | { |
| 25 | return new Promise($thenable, $this); |
| 26 | } |
| 27 | |
| 28 | /** @throws InvariantViolation */ |
| 29 | public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null): Promise |
| 30 | { |
| 31 | $reactPromise = $promise->adoptedPromise; |
| 32 | assert($reactPromise instanceof ReactPromiseInterface); |
| 33 | |
| 34 | return new Promise($reactPromise->then($onFulfilled, $onRejected), $this); |
| 35 | } |
| 36 | |
| 37 | /** @throws InvariantViolation */ |
| 38 | public function create(callable $resolver): Promise |
| 39 | { |
| 40 | $reactPromise = new ReactPromise($resolver); |
| 41 | |
| 42 | return new Promise($reactPromise, $this); |
| 43 | } |
| 44 | |
| 45 | /** @throws InvariantViolation */ |
| 46 | public function createFulfilled($value = null): Promise |
| 47 | { |
| 48 | $reactPromise = resolve($value); |
| 49 | |
| 50 | return new Promise($reactPromise, $this); |
| 51 | } |
| 52 | |
| 53 | /** @throws InvariantViolation */ |
| 54 | public function createRejected(\Throwable $reason): Promise |
| 55 | { |
| 56 | $reactPromise = reject($reason); |
| 57 | |
| 58 | return new Promise($reactPromise, $this); |
| 59 | } |
| 60 | |
| 61 | /** @throws InvariantViolation */ |
| 62 | public function all(iterable $promisesOrValues): Promise |
| 63 | { |
| 64 | foreach ($promisesOrValues as &$promiseOrValue) { |
| 65 | if ($promiseOrValue instanceof Promise) { |
| 66 | $promiseOrValue = $promiseOrValue->adoptedPromise; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | $promisesOrValuesArray = is_array($promisesOrValues) |
| 71 | ? $promisesOrValues |
| 72 | : iterator_to_array($promisesOrValues); |
| 73 | $reactPromise = all($promisesOrValuesArray)->then(static fn (array $values): array => array_map( |
| 74 | static fn ($key) => $values[$key], |
| 75 | array_keys($promisesOrValuesArray), |
| 76 | )); |
| 77 | |
| 78 | return new Promise($reactPromise, $this); |
| 79 | } |
| 80 | } |
| 81 |