Error
2 months ago
Executor
2 months ago
Language
2 months ago
Server
2 months ago
Type
2 months ago
Utils
2 months ago
Validator
2 months ago
Deferred.php
2 months ago
GraphQL.php
2 months ago
Deferred.php
58 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\Adapter\SyncPromise; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\Adapter\SyncPromiseQueue; |
| 7 | |
| 8 | /** |
| 9 | * User-facing promise class for deferred field resolution. |
| 10 | * |
| 11 | * @phpstan-type Executor callable(): mixed |
| 12 | */ |
| 13 | class Deferred extends SyncPromise |
| 14 | { |
| 15 | /** |
| 16 | * Executor for deferred promises. |
| 17 | * |
| 18 | * @var (callable(): mixed)|null |
| 19 | */ |
| 20 | protected $executor; |
| 21 | |
| 22 | /** |
| 23 | * Create a new Deferred promise and enqueue its execution. |
| 24 | * |
| 25 | * @api |
| 26 | * |
| 27 | * @param Executor $executor |
| 28 | */ |
| 29 | public function __construct(callable $executor) |
| 30 | { |
| 31 | $this->executor = $executor; |
| 32 | |
| 33 | SyncPromiseQueue::enqueue(function (): void { |
| 34 | $executor = $this->executor; |
| 35 | assert($executor !== null, 'Always set in constructor, this callback runs only once.'); |
| 36 | $this->executor = null; |
| 37 | |
| 38 | try { |
| 39 | $this->resolve($executor()); |
| 40 | } catch (\Throwable $e) { |
| 41 | $this->reject($e); |
| 42 | } |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Alias for __construct. |
| 48 | * |
| 49 | * @param Executor $executor |
| 50 | * |
| 51 | * @deprecated TODO remove in next major version, use new Deferred() instead |
| 52 | */ |
| 53 | public static function create(callable $executor): self |
| 54 | { |
| 55 | return new self($executor); |
| 56 | } |
| 57 | } |
| 58 |