@@ -6,19 +6,19 @@ |
||
6 | 6 | |
7 | 7 | interface TaskQueueInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * Returns true if the queue is empty. |
|
11 | - */ |
|
12 | - public function isEmpty(): bool; |
|
9 | + /** |
|
10 | + * Returns true if the queue is empty. |
|
11 | + */ |
|
12 | + public function isEmpty(): bool; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Adds a task to the queue that will be executed the next time run is |
|
16 | - * called. |
|
17 | - */ |
|
18 | - public function add(callable $task): void; |
|
14 | + /** |
|
15 | + * Adds a task to the queue that will be executed the next time run is |
|
16 | + * called. |
|
17 | + */ |
|
18 | + public function add(callable $task): void; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Execute all of the pending task in the queue. |
|
22 | - */ |
|
23 | - public function run(): void; |
|
20 | + /** |
|
21 | + * Execute all of the pending task in the queue. |
|
22 | + */ |
|
23 | + public function run(): void; |
|
24 | 24 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
6 | 6 | |
7 | -interface TaskQueueInterface |
|
8 | -{ |
|
7 | +interface TaskQueueInterface { |
|
9 | 8 | /** |
10 | 9 | * Returns true if the queue is empty. |
11 | 10 | */ |
@@ -14,76 +14,76 @@ |
||
14 | 14 | */ |
15 | 15 | class FulfilledPromise implements PromiseInterface |
16 | 16 | { |
17 | - private $value; |
|
17 | + private $value; |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param mixed $value |
|
21 | - */ |
|
22 | - public function __construct($value) |
|
23 | - { |
|
24 | - if (is_object($value) && method_exists($value, 'then')) { |
|
25 | - throw new \InvalidArgumentException( |
|
26 | - 'You cannot create a FulfilledPromise with a promise.' |
|
27 | - ); |
|
28 | - } |
|
19 | + /** |
|
20 | + * @param mixed $value |
|
21 | + */ |
|
22 | + public function __construct($value) |
|
23 | + { |
|
24 | + if (is_object($value) && method_exists($value, 'then')) { |
|
25 | + throw new \InvalidArgumentException( |
|
26 | + 'You cannot create a FulfilledPromise with a promise.' |
|
27 | + ); |
|
28 | + } |
|
29 | 29 | |
30 | - $this->value = $value; |
|
31 | - } |
|
30 | + $this->value = $value; |
|
31 | + } |
|
32 | 32 | |
33 | - public function then( |
|
34 | - callable $onFulfilled = null, |
|
35 | - callable $onRejected = null |
|
36 | - ): PromiseInterface { |
|
37 | - // Return itself if there is no onFulfilled function. |
|
38 | - if (!$onFulfilled) { |
|
39 | - return $this; |
|
40 | - } |
|
33 | + public function then( |
|
34 | + callable $onFulfilled = null, |
|
35 | + callable $onRejected = null |
|
36 | + ): PromiseInterface { |
|
37 | + // Return itself if there is no onFulfilled function. |
|
38 | + if (!$onFulfilled) { |
|
39 | + return $this; |
|
40 | + } |
|
41 | 41 | |
42 | - $queue = Utils::queue(); |
|
43 | - $p = new Promise([$queue, 'run']); |
|
44 | - $value = $this->value; |
|
45 | - $queue->add(static function () use ($p, $value, $onFulfilled): void { |
|
46 | - if (Is::pending($p)) { |
|
47 | - try { |
|
48 | - $p->resolve($onFulfilled($value)); |
|
49 | - } catch (\Throwable $e) { |
|
50 | - $p->reject($e); |
|
51 | - } |
|
52 | - } |
|
53 | - }); |
|
42 | + $queue = Utils::queue(); |
|
43 | + $p = new Promise([$queue, 'run']); |
|
44 | + $value = $this->value; |
|
45 | + $queue->add(static function () use ($p, $value, $onFulfilled): void { |
|
46 | + if (Is::pending($p)) { |
|
47 | + try { |
|
48 | + $p->resolve($onFulfilled($value)); |
|
49 | + } catch (\Throwable $e) { |
|
50 | + $p->reject($e); |
|
51 | + } |
|
52 | + } |
|
53 | + }); |
|
54 | 54 | |
55 | - return $p; |
|
56 | - } |
|
55 | + return $p; |
|
56 | + } |
|
57 | 57 | |
58 | - public function otherwise(callable $onRejected): PromiseInterface |
|
59 | - { |
|
60 | - return $this->then(null, $onRejected); |
|
61 | - } |
|
58 | + public function otherwise(callable $onRejected): PromiseInterface |
|
59 | + { |
|
60 | + return $this->then(null, $onRejected); |
|
61 | + } |
|
62 | 62 | |
63 | - public function wait(bool $unwrap = true) |
|
64 | - { |
|
65 | - return $unwrap ? $this->value : null; |
|
66 | - } |
|
63 | + public function wait(bool $unwrap = true) |
|
64 | + { |
|
65 | + return $unwrap ? $this->value : null; |
|
66 | + } |
|
67 | 67 | |
68 | - public function getState(): string |
|
69 | - { |
|
70 | - return self::FULFILLED; |
|
71 | - } |
|
68 | + public function getState(): string |
|
69 | + { |
|
70 | + return self::FULFILLED; |
|
71 | + } |
|
72 | 72 | |
73 | - public function resolve($value): void |
|
74 | - { |
|
75 | - if ($value !== $this->value) { |
|
76 | - throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
77 | - } |
|
78 | - } |
|
73 | + public function resolve($value): void |
|
74 | + { |
|
75 | + if ($value !== $this->value) { |
|
76 | + throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
77 | + } |
|
78 | + } |
|
79 | 79 | |
80 | - public function reject($reason): void |
|
81 | - { |
|
82 | - throw new \LogicException('Cannot reject a fulfilled promise'); |
|
83 | - } |
|
80 | + public function reject($reason): void |
|
81 | + { |
|
82 | + throw new \LogicException('Cannot reject a fulfilled promise'); |
|
83 | + } |
|
84 | 84 | |
85 | - public function cancel(): void |
|
86 | - { |
|
87 | - // pass |
|
88 | - } |
|
85 | + public function cancel(): void |
|
86 | + { |
|
87 | + // pass |
|
88 | + } |
|
89 | 89 | } |
@@ -42,7 +42,7 @@ |
||
42 | 42 | $queue = Utils::queue(); |
43 | 43 | $p = new Promise([$queue, 'run']); |
44 | 44 | $value = $this->value; |
45 | - $queue->add(static function () use ($p, $value, $onFulfilled): void { |
|
45 | + $queue->add(static function() use ($p, $value, $onFulfilled): void { |
|
46 | 46 | if (Is::pending($p)) { |
47 | 47 | try { |
48 | 48 | $p->resolve($onFulfilled($value)); |
@@ -12,8 +12,7 @@ |
||
12 | 12 | * |
13 | 13 | * @final |
14 | 14 | */ |
15 | -class FulfilledPromise implements PromiseInterface |
|
16 | -{ |
|
15 | +class FulfilledPromise implements PromiseInterface { |
|
17 | 16 | private $value; |
18 | 17 | |
19 | 18 | /** |
@@ -6,74 +6,74 @@ |
||
6 | 6 | |
7 | 7 | final class Create |
8 | 8 | { |
9 | - /** |
|
10 | - * Creates a promise for a value if the value is not a promise. |
|
11 | - * |
|
12 | - * @param mixed $value Promise or value. |
|
13 | - */ |
|
14 | - public static function promiseFor($value): PromiseInterface |
|
15 | - { |
|
16 | - if ($value instanceof PromiseInterface) { |
|
17 | - return $value; |
|
18 | - } |
|
9 | + /** |
|
10 | + * Creates a promise for a value if the value is not a promise. |
|
11 | + * |
|
12 | + * @param mixed $value Promise or value. |
|
13 | + */ |
|
14 | + public static function promiseFor($value): PromiseInterface |
|
15 | + { |
|
16 | + if ($value instanceof PromiseInterface) { |
|
17 | + return $value; |
|
18 | + } |
|
19 | 19 | |
20 | - // Return a Guzzle promise that shadows the given promise. |
|
21 | - if (is_object($value) && method_exists($value, 'then')) { |
|
22 | - $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null; |
|
23 | - $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null; |
|
24 | - $promise = new Promise($wfn, $cfn); |
|
25 | - $value->then([$promise, 'resolve'], [$promise, 'reject']); |
|
20 | + // Return a Guzzle promise that shadows the given promise. |
|
21 | + if (is_object($value) && method_exists($value, 'then')) { |
|
22 | + $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null; |
|
23 | + $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null; |
|
24 | + $promise = new Promise($wfn, $cfn); |
|
25 | + $value->then([$promise, 'resolve'], [$promise, 'reject']); |
|
26 | 26 | |
27 | - return $promise; |
|
28 | - } |
|
27 | + return $promise; |
|
28 | + } |
|
29 | 29 | |
30 | - return new FulfilledPromise($value); |
|
31 | - } |
|
30 | + return new FulfilledPromise($value); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Creates a rejected promise for a reason if the reason is not a promise. |
|
35 | - * If the provided reason is a promise, then it is returned as-is. |
|
36 | - * |
|
37 | - * @param mixed $reason Promise or reason. |
|
38 | - */ |
|
39 | - public static function rejectionFor($reason): PromiseInterface |
|
40 | - { |
|
41 | - if ($reason instanceof PromiseInterface) { |
|
42 | - return $reason; |
|
43 | - } |
|
33 | + /** |
|
34 | + * Creates a rejected promise for a reason if the reason is not a promise. |
|
35 | + * If the provided reason is a promise, then it is returned as-is. |
|
36 | + * |
|
37 | + * @param mixed $reason Promise or reason. |
|
38 | + */ |
|
39 | + public static function rejectionFor($reason): PromiseInterface |
|
40 | + { |
|
41 | + if ($reason instanceof PromiseInterface) { |
|
42 | + return $reason; |
|
43 | + } |
|
44 | 44 | |
45 | - return new RejectedPromise($reason); |
|
46 | - } |
|
45 | + return new RejectedPromise($reason); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Create an exception for a rejected promise value. |
|
50 | - * |
|
51 | - * @param mixed $reason |
|
52 | - */ |
|
53 | - public static function exceptionFor($reason): \Throwable |
|
54 | - { |
|
55 | - if ($reason instanceof \Throwable) { |
|
56 | - return $reason; |
|
57 | - } |
|
48 | + /** |
|
49 | + * Create an exception for a rejected promise value. |
|
50 | + * |
|
51 | + * @param mixed $reason |
|
52 | + */ |
|
53 | + public static function exceptionFor($reason): \Throwable |
|
54 | + { |
|
55 | + if ($reason instanceof \Throwable) { |
|
56 | + return $reason; |
|
57 | + } |
|
58 | 58 | |
59 | - return new RejectionException($reason); |
|
60 | - } |
|
59 | + return new RejectionException($reason); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Returns an iterator for the given value. |
|
64 | - * |
|
65 | - * @param mixed $value |
|
66 | - */ |
|
67 | - public static function iterFor($value): \Iterator |
|
68 | - { |
|
69 | - if ($value instanceof \Iterator) { |
|
70 | - return $value; |
|
71 | - } |
|
62 | + /** |
|
63 | + * Returns an iterator for the given value. |
|
64 | + * |
|
65 | + * @param mixed $value |
|
66 | + */ |
|
67 | + public static function iterFor($value): \Iterator |
|
68 | + { |
|
69 | + if ($value instanceof \Iterator) { |
|
70 | + return $value; |
|
71 | + } |
|
72 | 72 | |
73 | - if (is_array($value)) { |
|
74 | - return new \ArrayIterator($value); |
|
75 | - } |
|
73 | + if (is_array($value)) { |
|
74 | + return new \ArrayIterator($value); |
|
75 | + } |
|
76 | 76 | |
77 | - return new \ArrayIterator([$value]); |
|
78 | - } |
|
77 | + return new \ArrayIterator([$value]); |
|
78 | + } |
|
79 | 79 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
6 | 6 | |
7 | -final class Create |
|
8 | -{ |
|
7 | +final class Create { |
|
9 | 8 | /** |
10 | 9 | * Creates a promise for a value if the value is not a promise. |
11 | 10 | * |
@@ -15,77 +15,77 @@ |
||
15 | 15 | */ |
16 | 16 | interface PromiseInterface |
17 | 17 | { |
18 | - public const PENDING = 'pending'; |
|
19 | - public const FULFILLED = 'fulfilled'; |
|
20 | - public const REJECTED = 'rejected'; |
|
18 | + public const PENDING = 'pending'; |
|
19 | + public const FULFILLED = 'fulfilled'; |
|
20 | + public const REJECTED = 'rejected'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Appends fulfillment and rejection handlers to the promise, and returns |
|
24 | - * a new promise resolving to the return value of the called handler. |
|
25 | - * |
|
26 | - * @param callable $onFulfilled Invoked when the promise fulfills. |
|
27 | - * @param callable $onRejected Invoked when the promise is rejected. |
|
28 | - */ |
|
29 | - public function then( |
|
30 | - callable $onFulfilled = null, |
|
31 | - callable $onRejected = null |
|
32 | - ): PromiseInterface; |
|
22 | + /** |
|
23 | + * Appends fulfillment and rejection handlers to the promise, and returns |
|
24 | + * a new promise resolving to the return value of the called handler. |
|
25 | + * |
|
26 | + * @param callable $onFulfilled Invoked when the promise fulfills. |
|
27 | + * @param callable $onRejected Invoked when the promise is rejected. |
|
28 | + */ |
|
29 | + public function then( |
|
30 | + callable $onFulfilled = null, |
|
31 | + callable $onRejected = null |
|
32 | + ): PromiseInterface; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Appends a rejection handler callback to the promise, and returns a new |
|
36 | - * promise resolving to the return value of the callback if it is called, |
|
37 | - * or to its original fulfillment value if the promise is instead |
|
38 | - * fulfilled. |
|
39 | - * |
|
40 | - * @param callable $onRejected Invoked when the promise is rejected. |
|
41 | - */ |
|
42 | - public function otherwise(callable $onRejected): PromiseInterface; |
|
34 | + /** |
|
35 | + * Appends a rejection handler callback to the promise, and returns a new |
|
36 | + * promise resolving to the return value of the callback if it is called, |
|
37 | + * or to its original fulfillment value if the promise is instead |
|
38 | + * fulfilled. |
|
39 | + * |
|
40 | + * @param callable $onRejected Invoked when the promise is rejected. |
|
41 | + */ |
|
42 | + public function otherwise(callable $onRejected): PromiseInterface; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get the state of the promise ("pending", "rejected", or "fulfilled"). |
|
46 | - * |
|
47 | - * The three states can be checked against the constants defined on |
|
48 | - * PromiseInterface: PENDING, FULFILLED, and REJECTED. |
|
49 | - */ |
|
50 | - public function getState(): string; |
|
44 | + /** |
|
45 | + * Get the state of the promise ("pending", "rejected", or "fulfilled"). |
|
46 | + * |
|
47 | + * The three states can be checked against the constants defined on |
|
48 | + * PromiseInterface: PENDING, FULFILLED, and REJECTED. |
|
49 | + */ |
|
50 | + public function getState(): string; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Resolve the promise with the given value. |
|
54 | - * |
|
55 | - * @param mixed $value |
|
56 | - * |
|
57 | - * @throws \RuntimeException if the promise is already resolved. |
|
58 | - */ |
|
59 | - public function resolve($value): void; |
|
52 | + /** |
|
53 | + * Resolve the promise with the given value. |
|
54 | + * |
|
55 | + * @param mixed $value |
|
56 | + * |
|
57 | + * @throws \RuntimeException if the promise is already resolved. |
|
58 | + */ |
|
59 | + public function resolve($value): void; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Reject the promise with the given reason. |
|
63 | - * |
|
64 | - * @param mixed $reason |
|
65 | - * |
|
66 | - * @throws \RuntimeException if the promise is already resolved. |
|
67 | - */ |
|
68 | - public function reject($reason): void; |
|
61 | + /** |
|
62 | + * Reject the promise with the given reason. |
|
63 | + * |
|
64 | + * @param mixed $reason |
|
65 | + * |
|
66 | + * @throws \RuntimeException if the promise is already resolved. |
|
67 | + */ |
|
68 | + public function reject($reason): void; |
|
69 | 69 | |
70 | - /** |
|
71 | - * Cancels the promise if possible. |
|
72 | - * |
|
73 | - * @see https://github.com/promises-aplus/cancellation-spec/issues/7 |
|
74 | - */ |
|
75 | - public function cancel(): void; |
|
70 | + /** |
|
71 | + * Cancels the promise if possible. |
|
72 | + * |
|
73 | + * @see https://github.com/promises-aplus/cancellation-spec/issues/7 |
|
74 | + */ |
|
75 | + public function cancel(): void; |
|
76 | 76 | |
77 | - /** |
|
78 | - * Waits until the promise completes if possible. |
|
79 | - * |
|
80 | - * Pass $unwrap as true to unwrap the result of the promise, either |
|
81 | - * returning the resolved value or throwing the rejected exception. |
|
82 | - * |
|
83 | - * If the promise cannot be waited on, then the promise will be rejected. |
|
84 | - * |
|
85 | - * @return mixed |
|
86 | - * |
|
87 | - * @throws \LogicException if the promise has no wait function or if the |
|
88 | - * promise does not settle after waiting. |
|
89 | - */ |
|
90 | - public function wait(bool $unwrap = true); |
|
77 | + /** |
|
78 | + * Waits until the promise completes if possible. |
|
79 | + * |
|
80 | + * Pass $unwrap as true to unwrap the result of the promise, either |
|
81 | + * returning the resolved value or throwing the rejected exception. |
|
82 | + * |
|
83 | + * If the promise cannot be waited on, then the promise will be rejected. |
|
84 | + * |
|
85 | + * @return mixed |
|
86 | + * |
|
87 | + * @throws \LogicException if the promise has no wait function or if the |
|
88 | + * promise does not settle after waiting. |
|
89 | + */ |
|
90 | + public function wait(bool $unwrap = true); |
|
91 | 91 | } |
@@ -13,8 +13,7 @@ |
||
13 | 13 | * |
14 | 14 | * @see https://promisesaplus.com/ |
15 | 15 | */ |
16 | -interface PromiseInterface |
|
17 | -{ |
|
16 | +interface PromiseInterface { |
|
18 | 17 | public const PENDING = 'pending'; |
19 | 18 | public const FULFILLED = 'fulfilled'; |
20 | 19 | public const REJECTED = 'rejected'; |
@@ -7,6 +7,5 @@ |
||
7 | 7 | /** |
8 | 8 | * Exception that is set as the reason for a promise that has been cancelled. |
9 | 9 | */ |
10 | -class CancellationException extends RejectionException |
|
11 | -{ |
|
10 | +class CancellationException extends RejectionException { |
|
12 | 11 | } |
@@ -12,237 +12,237 @@ |
||
12 | 12 | */ |
13 | 13 | class EachPromise implements PromisorInterface |
14 | 14 | { |
15 | - private $pending = []; |
|
16 | - |
|
17 | - private $nextPendingIndex = 0; |
|
18 | - |
|
19 | - /** @var \Iterator|null */ |
|
20 | - private $iterable; |
|
21 | - |
|
22 | - /** @var callable|int|null */ |
|
23 | - private $concurrency; |
|
24 | - |
|
25 | - /** @var callable|null */ |
|
26 | - private $onFulfilled; |
|
27 | - |
|
28 | - /** @var callable|null */ |
|
29 | - private $onRejected; |
|
30 | - |
|
31 | - /** @var Promise|null */ |
|
32 | - private $aggregate; |
|
33 | - |
|
34 | - /** @var bool|null */ |
|
35 | - private $mutex; |
|
36 | - |
|
37 | - /** |
|
38 | - * Configuration hash can include the following key value pairs: |
|
39 | - * |
|
40 | - * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
41 | - * is invoked with three arguments: the fulfillment value, the index |
|
42 | - * position from the iterable list of the promise, and the aggregate |
|
43 | - * promise that manages all of the promises. The aggregate promise may |
|
44 | - * be resolved from within the callback to short-circuit the promise. |
|
45 | - * - rejected: (callable) Invoked when a promise is rejected. The |
|
46 | - * function is invoked with three arguments: the rejection reason, the |
|
47 | - * index position from the iterable list of the promise, and the |
|
48 | - * aggregate promise that manages all of the promises. The aggregate |
|
49 | - * promise may be resolved from within the callback to short-circuit |
|
50 | - * the promise. |
|
51 | - * - concurrency: (integer) Pass this configuration option to limit the |
|
52 | - * allowed number of outstanding concurrently executing promises, |
|
53 | - * creating a capped pool of promises. There is no limit by default. |
|
54 | - * |
|
55 | - * @param mixed $iterable Promises or values to iterate. |
|
56 | - * @param array $config Configuration options |
|
57 | - */ |
|
58 | - public function __construct($iterable, array $config = []) |
|
59 | - { |
|
60 | - $this->iterable = Create::iterFor($iterable); |
|
61 | - |
|
62 | - if (isset($config['concurrency'])) { |
|
63 | - $this->concurrency = $config['concurrency']; |
|
64 | - } |
|
65 | - |
|
66 | - if (isset($config['fulfilled'])) { |
|
67 | - $this->onFulfilled = $config['fulfilled']; |
|
68 | - } |
|
69 | - |
|
70 | - if (isset($config['rejected'])) { |
|
71 | - $this->onRejected = $config['rejected']; |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - /** @psalm-suppress InvalidNullableReturnType */ |
|
76 | - public function promise(): PromiseInterface |
|
77 | - { |
|
78 | - if ($this->aggregate) { |
|
79 | - return $this->aggregate; |
|
80 | - } |
|
81 | - |
|
82 | - try { |
|
83 | - $this->createPromise(); |
|
84 | - /** @psalm-assert Promise $this->aggregate */ |
|
85 | - $this->iterable->rewind(); |
|
86 | - $this->refillPending(); |
|
87 | - } catch (\Throwable $e) { |
|
88 | - $this->aggregate->reject($e); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @psalm-suppress NullableReturnStatement |
|
93 | - */ |
|
94 | - return $this->aggregate; |
|
95 | - } |
|
96 | - |
|
97 | - private function createPromise(): void |
|
98 | - { |
|
99 | - $this->mutex = false; |
|
100 | - $this->aggregate = new Promise(function (): void { |
|
101 | - if ($this->checkIfFinished()) { |
|
102 | - return; |
|
103 | - } |
|
104 | - reset($this->pending); |
|
105 | - // Consume a potentially fluctuating list of promises while |
|
106 | - // ensuring that indexes are maintained (precluding array_shift). |
|
107 | - while ($promise = current($this->pending)) { |
|
108 | - next($this->pending); |
|
109 | - $promise->wait(); |
|
110 | - if (Is::settled($this->aggregate)) { |
|
111 | - return; |
|
112 | - } |
|
113 | - } |
|
114 | - }); |
|
115 | - |
|
116 | - // Clear the references when the promise is resolved. |
|
117 | - $clearFn = function (): void { |
|
118 | - $this->iterable = $this->concurrency = $this->pending = null; |
|
119 | - $this->onFulfilled = $this->onRejected = null; |
|
120 | - $this->nextPendingIndex = 0; |
|
121 | - }; |
|
122 | - |
|
123 | - $this->aggregate->then($clearFn, $clearFn); |
|
124 | - } |
|
125 | - |
|
126 | - private function refillPending(): void |
|
127 | - { |
|
128 | - if (!$this->concurrency) { |
|
129 | - // Add all pending promises. |
|
130 | - while ($this->addPending() && $this->advanceIterator()) { |
|
131 | - } |
|
132 | - |
|
133 | - return; |
|
134 | - } |
|
135 | - |
|
136 | - // Add only up to N pending promises. |
|
137 | - $concurrency = is_callable($this->concurrency) |
|
138 | - ? ($this->concurrency)(count($this->pending)) |
|
139 | - : $this->concurrency; |
|
140 | - $concurrency = max($concurrency - count($this->pending), 0); |
|
141 | - // Concurrency may be set to 0 to disallow new promises. |
|
142 | - if (!$concurrency) { |
|
143 | - return; |
|
144 | - } |
|
145 | - // Add the first pending promise. |
|
146 | - $this->addPending(); |
|
147 | - // Note this is special handling for concurrency=1 so that we do |
|
148 | - // not advance the iterator after adding the first promise. This |
|
149 | - // helps work around issues with generators that might not have the |
|
150 | - // next value to yield until promise callbacks are called. |
|
151 | - while (--$concurrency |
|
152 | - && $this->advanceIterator() |
|
153 | - && $this->addPending()) { |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - private function addPending(): bool |
|
158 | - { |
|
159 | - if (!$this->iterable || !$this->iterable->valid()) { |
|
160 | - return false; |
|
161 | - } |
|
162 | - |
|
163 | - $promise = Create::promiseFor($this->iterable->current()); |
|
164 | - $key = $this->iterable->key(); |
|
165 | - |
|
166 | - // Iterable keys may not be unique, so we use a counter to |
|
167 | - // guarantee uniqueness |
|
168 | - $idx = $this->nextPendingIndex++; |
|
169 | - |
|
170 | - $this->pending[$idx] = $promise->then( |
|
171 | - function ($value) use ($idx, $key): void { |
|
172 | - if ($this->onFulfilled) { |
|
173 | - ($this->onFulfilled)( |
|
174 | - $value, |
|
175 | - $key, |
|
176 | - $this->aggregate |
|
177 | - ); |
|
178 | - } |
|
179 | - $this->step($idx); |
|
180 | - }, |
|
181 | - function ($reason) use ($idx, $key): void { |
|
182 | - if ($this->onRejected) { |
|
183 | - ($this->onRejected)( |
|
184 | - $reason, |
|
185 | - $key, |
|
186 | - $this->aggregate |
|
187 | - ); |
|
188 | - } |
|
189 | - $this->step($idx); |
|
190 | - } |
|
191 | - ); |
|
192 | - |
|
193 | - return true; |
|
194 | - } |
|
195 | - |
|
196 | - private function advanceIterator(): bool |
|
197 | - { |
|
198 | - // Place a lock on the iterator so that we ensure to not recurse, |
|
199 | - // preventing fatal generator errors. |
|
200 | - if ($this->mutex) { |
|
201 | - return false; |
|
202 | - } |
|
203 | - |
|
204 | - $this->mutex = true; |
|
205 | - |
|
206 | - try { |
|
207 | - $this->iterable->next(); |
|
208 | - $this->mutex = false; |
|
209 | - |
|
210 | - return true; |
|
211 | - } catch (\Throwable $e) { |
|
212 | - $this->aggregate->reject($e); |
|
213 | - $this->mutex = false; |
|
214 | - |
|
215 | - return false; |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - private function step(int $idx): void |
|
220 | - { |
|
221 | - // If the promise was already resolved, then ignore this step. |
|
222 | - if (Is::settled($this->aggregate)) { |
|
223 | - return; |
|
224 | - } |
|
225 | - |
|
226 | - unset($this->pending[$idx]); |
|
227 | - |
|
228 | - // Only refill pending promises if we are not locked, preventing the |
|
229 | - // EachPromise to recursively invoke the provided iterator, which |
|
230 | - // cause a fatal error: "Cannot resume an already running generator" |
|
231 | - if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
232 | - // Add more pending promises if possible. |
|
233 | - $this->refillPending(); |
|
234 | - } |
|
235 | - } |
|
236 | - |
|
237 | - private function checkIfFinished(): bool |
|
238 | - { |
|
239 | - if (!$this->pending && !$this->iterable->valid()) { |
|
240 | - // Resolve the promise if there's nothing left to do. |
|
241 | - $this->aggregate->resolve(null); |
|
242 | - |
|
243 | - return true; |
|
244 | - } |
|
245 | - |
|
246 | - return false; |
|
247 | - } |
|
15 | + private $pending = []; |
|
16 | + |
|
17 | + private $nextPendingIndex = 0; |
|
18 | + |
|
19 | + /** @var \Iterator|null */ |
|
20 | + private $iterable; |
|
21 | + |
|
22 | + /** @var callable|int|null */ |
|
23 | + private $concurrency; |
|
24 | + |
|
25 | + /** @var callable|null */ |
|
26 | + private $onFulfilled; |
|
27 | + |
|
28 | + /** @var callable|null */ |
|
29 | + private $onRejected; |
|
30 | + |
|
31 | + /** @var Promise|null */ |
|
32 | + private $aggregate; |
|
33 | + |
|
34 | + /** @var bool|null */ |
|
35 | + private $mutex; |
|
36 | + |
|
37 | + /** |
|
38 | + * Configuration hash can include the following key value pairs: |
|
39 | + * |
|
40 | + * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
41 | + * is invoked with three arguments: the fulfillment value, the index |
|
42 | + * position from the iterable list of the promise, and the aggregate |
|
43 | + * promise that manages all of the promises. The aggregate promise may |
|
44 | + * be resolved from within the callback to short-circuit the promise. |
|
45 | + * - rejected: (callable) Invoked when a promise is rejected. The |
|
46 | + * function is invoked with three arguments: the rejection reason, the |
|
47 | + * index position from the iterable list of the promise, and the |
|
48 | + * aggregate promise that manages all of the promises. The aggregate |
|
49 | + * promise may be resolved from within the callback to short-circuit |
|
50 | + * the promise. |
|
51 | + * - concurrency: (integer) Pass this configuration option to limit the |
|
52 | + * allowed number of outstanding concurrently executing promises, |
|
53 | + * creating a capped pool of promises. There is no limit by default. |
|
54 | + * |
|
55 | + * @param mixed $iterable Promises or values to iterate. |
|
56 | + * @param array $config Configuration options |
|
57 | + */ |
|
58 | + public function __construct($iterable, array $config = []) |
|
59 | + { |
|
60 | + $this->iterable = Create::iterFor($iterable); |
|
61 | + |
|
62 | + if (isset($config['concurrency'])) { |
|
63 | + $this->concurrency = $config['concurrency']; |
|
64 | + } |
|
65 | + |
|
66 | + if (isset($config['fulfilled'])) { |
|
67 | + $this->onFulfilled = $config['fulfilled']; |
|
68 | + } |
|
69 | + |
|
70 | + if (isset($config['rejected'])) { |
|
71 | + $this->onRejected = $config['rejected']; |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + /** @psalm-suppress InvalidNullableReturnType */ |
|
76 | + public function promise(): PromiseInterface |
|
77 | + { |
|
78 | + if ($this->aggregate) { |
|
79 | + return $this->aggregate; |
|
80 | + } |
|
81 | + |
|
82 | + try { |
|
83 | + $this->createPromise(); |
|
84 | + /** @psalm-assert Promise $this->aggregate */ |
|
85 | + $this->iterable->rewind(); |
|
86 | + $this->refillPending(); |
|
87 | + } catch (\Throwable $e) { |
|
88 | + $this->aggregate->reject($e); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @psalm-suppress NullableReturnStatement |
|
93 | + */ |
|
94 | + return $this->aggregate; |
|
95 | + } |
|
96 | + |
|
97 | + private function createPromise(): void |
|
98 | + { |
|
99 | + $this->mutex = false; |
|
100 | + $this->aggregate = new Promise(function (): void { |
|
101 | + if ($this->checkIfFinished()) { |
|
102 | + return; |
|
103 | + } |
|
104 | + reset($this->pending); |
|
105 | + // Consume a potentially fluctuating list of promises while |
|
106 | + // ensuring that indexes are maintained (precluding array_shift). |
|
107 | + while ($promise = current($this->pending)) { |
|
108 | + next($this->pending); |
|
109 | + $promise->wait(); |
|
110 | + if (Is::settled($this->aggregate)) { |
|
111 | + return; |
|
112 | + } |
|
113 | + } |
|
114 | + }); |
|
115 | + |
|
116 | + // Clear the references when the promise is resolved. |
|
117 | + $clearFn = function (): void { |
|
118 | + $this->iterable = $this->concurrency = $this->pending = null; |
|
119 | + $this->onFulfilled = $this->onRejected = null; |
|
120 | + $this->nextPendingIndex = 0; |
|
121 | + }; |
|
122 | + |
|
123 | + $this->aggregate->then($clearFn, $clearFn); |
|
124 | + } |
|
125 | + |
|
126 | + private function refillPending(): void |
|
127 | + { |
|
128 | + if (!$this->concurrency) { |
|
129 | + // Add all pending promises. |
|
130 | + while ($this->addPending() && $this->advanceIterator()) { |
|
131 | + } |
|
132 | + |
|
133 | + return; |
|
134 | + } |
|
135 | + |
|
136 | + // Add only up to N pending promises. |
|
137 | + $concurrency = is_callable($this->concurrency) |
|
138 | + ? ($this->concurrency)(count($this->pending)) |
|
139 | + : $this->concurrency; |
|
140 | + $concurrency = max($concurrency - count($this->pending), 0); |
|
141 | + // Concurrency may be set to 0 to disallow new promises. |
|
142 | + if (!$concurrency) { |
|
143 | + return; |
|
144 | + } |
|
145 | + // Add the first pending promise. |
|
146 | + $this->addPending(); |
|
147 | + // Note this is special handling for concurrency=1 so that we do |
|
148 | + // not advance the iterator after adding the first promise. This |
|
149 | + // helps work around issues with generators that might not have the |
|
150 | + // next value to yield until promise callbacks are called. |
|
151 | + while (--$concurrency |
|
152 | + && $this->advanceIterator() |
|
153 | + && $this->addPending()) { |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + private function addPending(): bool |
|
158 | + { |
|
159 | + if (!$this->iterable || !$this->iterable->valid()) { |
|
160 | + return false; |
|
161 | + } |
|
162 | + |
|
163 | + $promise = Create::promiseFor($this->iterable->current()); |
|
164 | + $key = $this->iterable->key(); |
|
165 | + |
|
166 | + // Iterable keys may not be unique, so we use a counter to |
|
167 | + // guarantee uniqueness |
|
168 | + $idx = $this->nextPendingIndex++; |
|
169 | + |
|
170 | + $this->pending[$idx] = $promise->then( |
|
171 | + function ($value) use ($idx, $key): void { |
|
172 | + if ($this->onFulfilled) { |
|
173 | + ($this->onFulfilled)( |
|
174 | + $value, |
|
175 | + $key, |
|
176 | + $this->aggregate |
|
177 | + ); |
|
178 | + } |
|
179 | + $this->step($idx); |
|
180 | + }, |
|
181 | + function ($reason) use ($idx, $key): void { |
|
182 | + if ($this->onRejected) { |
|
183 | + ($this->onRejected)( |
|
184 | + $reason, |
|
185 | + $key, |
|
186 | + $this->aggregate |
|
187 | + ); |
|
188 | + } |
|
189 | + $this->step($idx); |
|
190 | + } |
|
191 | + ); |
|
192 | + |
|
193 | + return true; |
|
194 | + } |
|
195 | + |
|
196 | + private function advanceIterator(): bool |
|
197 | + { |
|
198 | + // Place a lock on the iterator so that we ensure to not recurse, |
|
199 | + // preventing fatal generator errors. |
|
200 | + if ($this->mutex) { |
|
201 | + return false; |
|
202 | + } |
|
203 | + |
|
204 | + $this->mutex = true; |
|
205 | + |
|
206 | + try { |
|
207 | + $this->iterable->next(); |
|
208 | + $this->mutex = false; |
|
209 | + |
|
210 | + return true; |
|
211 | + } catch (\Throwable $e) { |
|
212 | + $this->aggregate->reject($e); |
|
213 | + $this->mutex = false; |
|
214 | + |
|
215 | + return false; |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + private function step(int $idx): void |
|
220 | + { |
|
221 | + // If the promise was already resolved, then ignore this step. |
|
222 | + if (Is::settled($this->aggregate)) { |
|
223 | + return; |
|
224 | + } |
|
225 | + |
|
226 | + unset($this->pending[$idx]); |
|
227 | + |
|
228 | + // Only refill pending promises if we are not locked, preventing the |
|
229 | + // EachPromise to recursively invoke the provided iterator, which |
|
230 | + // cause a fatal error: "Cannot resume an already running generator" |
|
231 | + if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
232 | + // Add more pending promises if possible. |
|
233 | + $this->refillPending(); |
|
234 | + } |
|
235 | + } |
|
236 | + |
|
237 | + private function checkIfFinished(): bool |
|
238 | + { |
|
239 | + if (!$this->pending && !$this->iterable->valid()) { |
|
240 | + // Resolve the promise if there's nothing left to do. |
|
241 | + $this->aggregate->resolve(null); |
|
242 | + |
|
243 | + return true; |
|
244 | + } |
|
245 | + |
|
246 | + return false; |
|
247 | + } |
|
248 | 248 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | private function createPromise(): void |
98 | 98 | { |
99 | 99 | $this->mutex = false; |
100 | - $this->aggregate = new Promise(function (): void { |
|
100 | + $this->aggregate = new Promise(function(): void { |
|
101 | 101 | if ($this->checkIfFinished()) { |
102 | 102 | return; |
103 | 103 | } |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | }); |
115 | 115 | |
116 | 116 | // Clear the references when the promise is resolved. |
117 | - $clearFn = function (): void { |
|
117 | + $clearFn = function(): void { |
|
118 | 118 | $this->iterable = $this->concurrency = $this->pending = null; |
119 | 119 | $this->onFulfilled = $this->onRejected = null; |
120 | 120 | $this->nextPendingIndex = 0; |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | $idx = $this->nextPendingIndex++; |
169 | 169 | |
170 | 170 | $this->pending[$idx] = $promise->then( |
171 | - function ($value) use ($idx, $key): void { |
|
171 | + function($value) use ($idx, $key): void { |
|
172 | 172 | if ($this->onFulfilled) { |
173 | 173 | ($this->onFulfilled)( |
174 | 174 | $value, |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | } |
179 | 179 | $this->step($idx); |
180 | 180 | }, |
181 | - function ($reason) use ($idx, $key): void { |
|
181 | + function($reason) use ($idx, $key): void { |
|
182 | 182 | if ($this->onRejected) { |
183 | 183 | ($this->onRejected)( |
184 | 184 | $reason, |
@@ -10,8 +10,7 @@ |
||
10 | 10 | * |
11 | 11 | * @final |
12 | 12 | */ |
13 | -class EachPromise implements PromisorInterface |
|
14 | -{ |
|
13 | +class EachPromise implements PromisorInterface { |
|
15 | 14 | private $pending = []; |
16 | 15 | |
17 | 16 | private $nextPendingIndex = 0; |
@@ -13,269 +13,269 @@ |
||
13 | 13 | */ |
14 | 14 | class Promise implements PromiseInterface |
15 | 15 | { |
16 | - private $state = self::PENDING; |
|
17 | - private $result; |
|
18 | - private $cancelFn; |
|
19 | - private $waitFn; |
|
20 | - private $waitList; |
|
21 | - private $handlers = []; |
|
22 | - |
|
23 | - /** |
|
24 | - * @param callable $waitFn Fn that when invoked resolves the promise. |
|
25 | - * @param callable $cancelFn Fn that when invoked cancels the promise. |
|
26 | - */ |
|
27 | - public function __construct( |
|
28 | - callable $waitFn = null, |
|
29 | - callable $cancelFn = null |
|
30 | - ) { |
|
31 | - $this->waitFn = $waitFn; |
|
32 | - $this->cancelFn = $cancelFn; |
|
33 | - } |
|
34 | - |
|
35 | - public function then( |
|
36 | - callable $onFulfilled = null, |
|
37 | - callable $onRejected = null |
|
38 | - ): PromiseInterface { |
|
39 | - if ($this->state === self::PENDING) { |
|
40 | - $p = new Promise(null, [$this, 'cancel']); |
|
41 | - $this->handlers[] = [$p, $onFulfilled, $onRejected]; |
|
42 | - $p->waitList = $this->waitList; |
|
43 | - $p->waitList[] = $this; |
|
44 | - |
|
45 | - return $p; |
|
46 | - } |
|
47 | - |
|
48 | - // Return a fulfilled promise and immediately invoke any callbacks. |
|
49 | - if ($this->state === self::FULFILLED) { |
|
50 | - $promise = Create::promiseFor($this->result); |
|
51 | - |
|
52 | - return $onFulfilled ? $promise->then($onFulfilled) : $promise; |
|
53 | - } |
|
54 | - |
|
55 | - // It's either cancelled or rejected, so return a rejected promise |
|
56 | - // and immediately invoke any callbacks. |
|
57 | - $rejection = Create::rejectionFor($this->result); |
|
58 | - |
|
59 | - return $onRejected ? $rejection->then(null, $onRejected) : $rejection; |
|
60 | - } |
|
61 | - |
|
62 | - public function otherwise(callable $onRejected): PromiseInterface |
|
63 | - { |
|
64 | - return $this->then(null, $onRejected); |
|
65 | - } |
|
66 | - |
|
67 | - public function wait(bool $unwrap = true) |
|
68 | - { |
|
69 | - $this->waitIfPending(); |
|
70 | - |
|
71 | - if ($this->result instanceof PromiseInterface) { |
|
72 | - return $this->result->wait($unwrap); |
|
73 | - } |
|
74 | - if ($unwrap) { |
|
75 | - if ($this->state === self::FULFILLED) { |
|
76 | - return $this->result; |
|
77 | - } |
|
78 | - // It's rejected so "unwrap" and throw an exception. |
|
79 | - throw Create::exceptionFor($this->result); |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - public function getState(): string |
|
84 | - { |
|
85 | - return $this->state; |
|
86 | - } |
|
87 | - |
|
88 | - public function cancel(): void |
|
89 | - { |
|
90 | - if ($this->state !== self::PENDING) { |
|
91 | - return; |
|
92 | - } |
|
93 | - |
|
94 | - $this->waitFn = $this->waitList = null; |
|
95 | - |
|
96 | - if ($this->cancelFn) { |
|
97 | - $fn = $this->cancelFn; |
|
98 | - $this->cancelFn = null; |
|
99 | - try { |
|
100 | - $fn(); |
|
101 | - } catch (\Throwable $e) { |
|
102 | - $this->reject($e); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - // Reject the promise only if it wasn't rejected in a then callback. |
|
107 | - /** @psalm-suppress RedundantCondition */ |
|
108 | - if ($this->state === self::PENDING) { |
|
109 | - $this->reject(new CancellationException('Promise has been cancelled')); |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - public function resolve($value): void |
|
114 | - { |
|
115 | - $this->settle(self::FULFILLED, $value); |
|
116 | - } |
|
117 | - |
|
118 | - public function reject($reason): void |
|
119 | - { |
|
120 | - $this->settle(self::REJECTED, $reason); |
|
121 | - } |
|
122 | - |
|
123 | - private function settle(string $state, $value): void |
|
124 | - { |
|
125 | - if ($this->state !== self::PENDING) { |
|
126 | - // Ignore calls with the same resolution. |
|
127 | - if ($state === $this->state && $value === $this->result) { |
|
128 | - return; |
|
129 | - } |
|
130 | - throw $this->state === $state |
|
131 | - ? new \LogicException("The promise is already {$state}.") |
|
132 | - : new \LogicException("Cannot change a {$this->state} promise to {$state}"); |
|
133 | - } |
|
134 | - |
|
135 | - if ($value === $this) { |
|
136 | - throw new \LogicException('Cannot fulfill or reject a promise with itself'); |
|
137 | - } |
|
138 | - |
|
139 | - // Clear out the state of the promise but stash the handlers. |
|
140 | - $this->state = $state; |
|
141 | - $this->result = $value; |
|
142 | - $handlers = $this->handlers; |
|
143 | - $this->handlers = null; |
|
144 | - $this->waitList = $this->waitFn = null; |
|
145 | - $this->cancelFn = null; |
|
146 | - |
|
147 | - if (!$handlers) { |
|
148 | - return; |
|
149 | - } |
|
150 | - |
|
151 | - // If the value was not a settled promise or a thenable, then resolve |
|
152 | - // it in the task queue using the correct ID. |
|
153 | - if (!is_object($value) || !method_exists($value, 'then')) { |
|
154 | - $id = $state === self::FULFILLED ? 1 : 2; |
|
155 | - // It's a success, so resolve the handlers in the queue. |
|
156 | - Utils::queue()->add(static function () use ($id, $value, $handlers): void { |
|
157 | - foreach ($handlers as $handler) { |
|
158 | - self::callHandler($id, $value, $handler); |
|
159 | - } |
|
160 | - }); |
|
161 | - } elseif ($value instanceof Promise && Is::pending($value)) { |
|
162 | - // We can just merge our handlers onto the next promise. |
|
163 | - $value->handlers = array_merge($value->handlers, $handlers); |
|
164 | - } else { |
|
165 | - // Resolve the handlers when the forwarded promise is resolved. |
|
166 | - $value->then( |
|
167 | - static function ($value) use ($handlers): void { |
|
168 | - foreach ($handlers as $handler) { |
|
169 | - self::callHandler(1, $value, $handler); |
|
170 | - } |
|
171 | - }, |
|
172 | - static function ($reason) use ($handlers): void { |
|
173 | - foreach ($handlers as $handler) { |
|
174 | - self::callHandler(2, $reason, $handler); |
|
175 | - } |
|
176 | - } |
|
177 | - ); |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Call a stack of handlers using a specific callback index and value. |
|
183 | - * |
|
184 | - * @param int $index 1 (resolve) or 2 (reject). |
|
185 | - * @param mixed $value Value to pass to the callback. |
|
186 | - * @param array $handler Array of handler data (promise and callbacks). |
|
187 | - */ |
|
188 | - private static function callHandler(int $index, $value, array $handler): void |
|
189 | - { |
|
190 | - /** @var PromiseInterface $promise */ |
|
191 | - $promise = $handler[0]; |
|
192 | - |
|
193 | - // The promise may have been cancelled or resolved before placing |
|
194 | - // this thunk in the queue. |
|
195 | - if (Is::settled($promise)) { |
|
196 | - return; |
|
197 | - } |
|
198 | - |
|
199 | - try { |
|
200 | - if (isset($handler[$index])) { |
|
201 | - /* |
|
16 | + private $state = self::PENDING; |
|
17 | + private $result; |
|
18 | + private $cancelFn; |
|
19 | + private $waitFn; |
|
20 | + private $waitList; |
|
21 | + private $handlers = []; |
|
22 | + |
|
23 | + /** |
|
24 | + * @param callable $waitFn Fn that when invoked resolves the promise. |
|
25 | + * @param callable $cancelFn Fn that when invoked cancels the promise. |
|
26 | + */ |
|
27 | + public function __construct( |
|
28 | + callable $waitFn = null, |
|
29 | + callable $cancelFn = null |
|
30 | + ) { |
|
31 | + $this->waitFn = $waitFn; |
|
32 | + $this->cancelFn = $cancelFn; |
|
33 | + } |
|
34 | + |
|
35 | + public function then( |
|
36 | + callable $onFulfilled = null, |
|
37 | + callable $onRejected = null |
|
38 | + ): PromiseInterface { |
|
39 | + if ($this->state === self::PENDING) { |
|
40 | + $p = new Promise(null, [$this, 'cancel']); |
|
41 | + $this->handlers[] = [$p, $onFulfilled, $onRejected]; |
|
42 | + $p->waitList = $this->waitList; |
|
43 | + $p->waitList[] = $this; |
|
44 | + |
|
45 | + return $p; |
|
46 | + } |
|
47 | + |
|
48 | + // Return a fulfilled promise and immediately invoke any callbacks. |
|
49 | + if ($this->state === self::FULFILLED) { |
|
50 | + $promise = Create::promiseFor($this->result); |
|
51 | + |
|
52 | + return $onFulfilled ? $promise->then($onFulfilled) : $promise; |
|
53 | + } |
|
54 | + |
|
55 | + // It's either cancelled or rejected, so return a rejected promise |
|
56 | + // and immediately invoke any callbacks. |
|
57 | + $rejection = Create::rejectionFor($this->result); |
|
58 | + |
|
59 | + return $onRejected ? $rejection->then(null, $onRejected) : $rejection; |
|
60 | + } |
|
61 | + |
|
62 | + public function otherwise(callable $onRejected): PromiseInterface |
|
63 | + { |
|
64 | + return $this->then(null, $onRejected); |
|
65 | + } |
|
66 | + |
|
67 | + public function wait(bool $unwrap = true) |
|
68 | + { |
|
69 | + $this->waitIfPending(); |
|
70 | + |
|
71 | + if ($this->result instanceof PromiseInterface) { |
|
72 | + return $this->result->wait($unwrap); |
|
73 | + } |
|
74 | + if ($unwrap) { |
|
75 | + if ($this->state === self::FULFILLED) { |
|
76 | + return $this->result; |
|
77 | + } |
|
78 | + // It's rejected so "unwrap" and throw an exception. |
|
79 | + throw Create::exceptionFor($this->result); |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + public function getState(): string |
|
84 | + { |
|
85 | + return $this->state; |
|
86 | + } |
|
87 | + |
|
88 | + public function cancel(): void |
|
89 | + { |
|
90 | + if ($this->state !== self::PENDING) { |
|
91 | + return; |
|
92 | + } |
|
93 | + |
|
94 | + $this->waitFn = $this->waitList = null; |
|
95 | + |
|
96 | + if ($this->cancelFn) { |
|
97 | + $fn = $this->cancelFn; |
|
98 | + $this->cancelFn = null; |
|
99 | + try { |
|
100 | + $fn(); |
|
101 | + } catch (\Throwable $e) { |
|
102 | + $this->reject($e); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + // Reject the promise only if it wasn't rejected in a then callback. |
|
107 | + /** @psalm-suppress RedundantCondition */ |
|
108 | + if ($this->state === self::PENDING) { |
|
109 | + $this->reject(new CancellationException('Promise has been cancelled')); |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + public function resolve($value): void |
|
114 | + { |
|
115 | + $this->settle(self::FULFILLED, $value); |
|
116 | + } |
|
117 | + |
|
118 | + public function reject($reason): void |
|
119 | + { |
|
120 | + $this->settle(self::REJECTED, $reason); |
|
121 | + } |
|
122 | + |
|
123 | + private function settle(string $state, $value): void |
|
124 | + { |
|
125 | + if ($this->state !== self::PENDING) { |
|
126 | + // Ignore calls with the same resolution. |
|
127 | + if ($state === $this->state && $value === $this->result) { |
|
128 | + return; |
|
129 | + } |
|
130 | + throw $this->state === $state |
|
131 | + ? new \LogicException("The promise is already {$state}.") |
|
132 | + : new \LogicException("Cannot change a {$this->state} promise to {$state}"); |
|
133 | + } |
|
134 | + |
|
135 | + if ($value === $this) { |
|
136 | + throw new \LogicException('Cannot fulfill or reject a promise with itself'); |
|
137 | + } |
|
138 | + |
|
139 | + // Clear out the state of the promise but stash the handlers. |
|
140 | + $this->state = $state; |
|
141 | + $this->result = $value; |
|
142 | + $handlers = $this->handlers; |
|
143 | + $this->handlers = null; |
|
144 | + $this->waitList = $this->waitFn = null; |
|
145 | + $this->cancelFn = null; |
|
146 | + |
|
147 | + if (!$handlers) { |
|
148 | + return; |
|
149 | + } |
|
150 | + |
|
151 | + // If the value was not a settled promise or a thenable, then resolve |
|
152 | + // it in the task queue using the correct ID. |
|
153 | + if (!is_object($value) || !method_exists($value, 'then')) { |
|
154 | + $id = $state === self::FULFILLED ? 1 : 2; |
|
155 | + // It's a success, so resolve the handlers in the queue. |
|
156 | + Utils::queue()->add(static function () use ($id, $value, $handlers): void { |
|
157 | + foreach ($handlers as $handler) { |
|
158 | + self::callHandler($id, $value, $handler); |
|
159 | + } |
|
160 | + }); |
|
161 | + } elseif ($value instanceof Promise && Is::pending($value)) { |
|
162 | + // We can just merge our handlers onto the next promise. |
|
163 | + $value->handlers = array_merge($value->handlers, $handlers); |
|
164 | + } else { |
|
165 | + // Resolve the handlers when the forwarded promise is resolved. |
|
166 | + $value->then( |
|
167 | + static function ($value) use ($handlers): void { |
|
168 | + foreach ($handlers as $handler) { |
|
169 | + self::callHandler(1, $value, $handler); |
|
170 | + } |
|
171 | + }, |
|
172 | + static function ($reason) use ($handlers): void { |
|
173 | + foreach ($handlers as $handler) { |
|
174 | + self::callHandler(2, $reason, $handler); |
|
175 | + } |
|
176 | + } |
|
177 | + ); |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Call a stack of handlers using a specific callback index and value. |
|
183 | + * |
|
184 | + * @param int $index 1 (resolve) or 2 (reject). |
|
185 | + * @param mixed $value Value to pass to the callback. |
|
186 | + * @param array $handler Array of handler data (promise and callbacks). |
|
187 | + */ |
|
188 | + private static function callHandler(int $index, $value, array $handler): void |
|
189 | + { |
|
190 | + /** @var PromiseInterface $promise */ |
|
191 | + $promise = $handler[0]; |
|
192 | + |
|
193 | + // The promise may have been cancelled or resolved before placing |
|
194 | + // this thunk in the queue. |
|
195 | + if (Is::settled($promise)) { |
|
196 | + return; |
|
197 | + } |
|
198 | + |
|
199 | + try { |
|
200 | + if (isset($handler[$index])) { |
|
201 | + /* |
|
202 | 202 | * If $f throws an exception, then $handler will be in the exception |
203 | 203 | * stack trace. Since $handler contains a reference to the callable |
204 | 204 | * itself we get a circular reference. We clear the $handler |
205 | 205 | * here to avoid that memory leak. |
206 | 206 | */ |
207 | - $f = $handler[$index]; |
|
208 | - unset($handler); |
|
209 | - $promise->resolve($f($value)); |
|
210 | - } elseif ($index === 1) { |
|
211 | - // Forward resolution values as-is. |
|
212 | - $promise->resolve($value); |
|
213 | - } else { |
|
214 | - // Forward rejections down the chain. |
|
215 | - $promise->reject($value); |
|
216 | - } |
|
217 | - } catch (\Throwable $reason) { |
|
218 | - $promise->reject($reason); |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - private function waitIfPending(): void |
|
223 | - { |
|
224 | - if ($this->state !== self::PENDING) { |
|
225 | - return; |
|
226 | - } elseif ($this->waitFn) { |
|
227 | - $this->invokeWaitFn(); |
|
228 | - } elseif ($this->waitList) { |
|
229 | - $this->invokeWaitList(); |
|
230 | - } else { |
|
231 | - // If there's no wait function, then reject the promise. |
|
232 | - $this->reject('Cannot wait on a promise that has ' |
|
233 | - .'no internal wait function. You must provide a wait ' |
|
234 | - .'function when constructing the promise to be able to ' |
|
235 | - .'wait on a promise.'); |
|
236 | - } |
|
237 | - |
|
238 | - Utils::queue()->run(); |
|
239 | - |
|
240 | - /** @psalm-suppress RedundantCondition */ |
|
241 | - if ($this->state === self::PENDING) { |
|
242 | - $this->reject('Invoking the wait callback did not resolve the promise'); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - private function invokeWaitFn(): void |
|
247 | - { |
|
248 | - try { |
|
249 | - $wfn = $this->waitFn; |
|
250 | - $this->waitFn = null; |
|
251 | - $wfn(true); |
|
252 | - } catch (\Throwable $reason) { |
|
253 | - if ($this->state === self::PENDING) { |
|
254 | - // The promise has not been resolved yet, so reject the promise |
|
255 | - // with the exception. |
|
256 | - $this->reject($reason); |
|
257 | - } else { |
|
258 | - // The promise was already resolved, so there's a problem in |
|
259 | - // the application. |
|
260 | - throw $reason; |
|
261 | - } |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - private function invokeWaitList(): void |
|
266 | - { |
|
267 | - $waitList = $this->waitList; |
|
268 | - $this->waitList = null; |
|
269 | - |
|
270 | - foreach ($waitList as $result) { |
|
271 | - do { |
|
272 | - $result->waitIfPending(); |
|
273 | - $result = $result->result; |
|
274 | - } while ($result instanceof Promise); |
|
275 | - |
|
276 | - if ($result instanceof PromiseInterface) { |
|
277 | - $result->wait(false); |
|
278 | - } |
|
279 | - } |
|
280 | - } |
|
207 | + $f = $handler[$index]; |
|
208 | + unset($handler); |
|
209 | + $promise->resolve($f($value)); |
|
210 | + } elseif ($index === 1) { |
|
211 | + // Forward resolution values as-is. |
|
212 | + $promise->resolve($value); |
|
213 | + } else { |
|
214 | + // Forward rejections down the chain. |
|
215 | + $promise->reject($value); |
|
216 | + } |
|
217 | + } catch (\Throwable $reason) { |
|
218 | + $promise->reject($reason); |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + private function waitIfPending(): void |
|
223 | + { |
|
224 | + if ($this->state !== self::PENDING) { |
|
225 | + return; |
|
226 | + } elseif ($this->waitFn) { |
|
227 | + $this->invokeWaitFn(); |
|
228 | + } elseif ($this->waitList) { |
|
229 | + $this->invokeWaitList(); |
|
230 | + } else { |
|
231 | + // If there's no wait function, then reject the promise. |
|
232 | + $this->reject('Cannot wait on a promise that has ' |
|
233 | + .'no internal wait function. You must provide a wait ' |
|
234 | + .'function when constructing the promise to be able to ' |
|
235 | + .'wait on a promise.'); |
|
236 | + } |
|
237 | + |
|
238 | + Utils::queue()->run(); |
|
239 | + |
|
240 | + /** @psalm-suppress RedundantCondition */ |
|
241 | + if ($this->state === self::PENDING) { |
|
242 | + $this->reject('Invoking the wait callback did not resolve the promise'); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + private function invokeWaitFn(): void |
|
247 | + { |
|
248 | + try { |
|
249 | + $wfn = $this->waitFn; |
|
250 | + $this->waitFn = null; |
|
251 | + $wfn(true); |
|
252 | + } catch (\Throwable $reason) { |
|
253 | + if ($this->state === self::PENDING) { |
|
254 | + // The promise has not been resolved yet, so reject the promise |
|
255 | + // with the exception. |
|
256 | + $this->reject($reason); |
|
257 | + } else { |
|
258 | + // The promise was already resolved, so there's a problem in |
|
259 | + // the application. |
|
260 | + throw $reason; |
|
261 | + } |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + private function invokeWaitList(): void |
|
266 | + { |
|
267 | + $waitList = $this->waitList; |
|
268 | + $this->waitList = null; |
|
269 | + |
|
270 | + foreach ($waitList as $result) { |
|
271 | + do { |
|
272 | + $result->waitIfPending(); |
|
273 | + $result = $result->result; |
|
274 | + } while ($result instanceof Promise); |
|
275 | + |
|
276 | + if ($result instanceof PromiseInterface) { |
|
277 | + $result->wait(false); |
|
278 | + } |
|
279 | + } |
|
280 | + } |
|
281 | 281 | } |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | if (!is_object($value) || !method_exists($value, 'then')) { |
154 | 154 | $id = $state === self::FULFILLED ? 1 : 2; |
155 | 155 | // It's a success, so resolve the handlers in the queue. |
156 | - Utils::queue()->add(static function () use ($id, $value, $handlers): void { |
|
156 | + Utils::queue()->add(static function() use ($id, $value, $handlers): void { |
|
157 | 157 | foreach ($handlers as $handler) { |
158 | 158 | self::callHandler($id, $value, $handler); |
159 | 159 | } |
@@ -164,12 +164,12 @@ discard block |
||
164 | 164 | } else { |
165 | 165 | // Resolve the handlers when the forwarded promise is resolved. |
166 | 166 | $value->then( |
167 | - static function ($value) use ($handlers): void { |
|
167 | + static function($value) use ($handlers): void { |
|
168 | 168 | foreach ($handlers as $handler) { |
169 | 169 | self::callHandler(1, $value, $handler); |
170 | 170 | } |
171 | 171 | }, |
172 | - static function ($reason) use ($handlers): void { |
|
172 | + static function($reason) use ($handlers): void { |
|
173 | 173 | foreach ($handlers as $handler) { |
174 | 174 | self::callHandler(2, $reason, $handler); |
175 | 175 | } |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @final |
13 | 13 | */ |
14 | -class Promise implements PromiseInterface |
|
15 | -{ |
|
14 | +class Promise implements PromiseInterface { |
|
16 | 15 | private $state = self::PENDING; |
17 | 16 | private $result; |
18 | 17 | private $cancelFn; |
@@ -9,8 +9,8 @@ |
||
9 | 9 | */ |
10 | 10 | interface PromisorInterface |
11 | 11 | { |
12 | - /** |
|
13 | - * Returns a promise. |
|
14 | - */ |
|
15 | - public function promise(): PromiseInterface; |
|
12 | + /** |
|
13 | + * Returns a promise. |
|
14 | + */ |
|
15 | + public function promise(): PromiseInterface; |
|
16 | 16 | } |
@@ -7,8 +7,7 @@ |
||
7 | 7 | /** |
8 | 8 | * Interface used with classes that return a promise. |
9 | 9 | */ |
10 | -interface PromisorInterface |
|
11 | -{ |
|
10 | +interface PromisorInterface { |
|
12 | 11 | /** |
13 | 12 | * Returns a promise. |
14 | 13 | */ |
@@ -9,11 +9,11 @@ |
||
9 | 9 | */ |
10 | 10 | class AggregateException extends RejectionException |
11 | 11 | { |
12 | - public function __construct(string $msg, array $reasons) |
|
13 | - { |
|
14 | - parent::__construct( |
|
15 | - $reasons, |
|
16 | - sprintf('%s; %d rejected promises', $msg, count($reasons)) |
|
17 | - ); |
|
18 | - } |
|
12 | + public function __construct(string $msg, array $reasons) |
|
13 | + { |
|
14 | + parent::__construct( |
|
15 | + $reasons, |
|
16 | + sprintf('%s; %d rejected promises', $msg, count($reasons)) |
|
17 | + ); |
|
18 | + } |
|
19 | 19 | } |
@@ -7,8 +7,7 @@ |
||
7 | 7 | /** |
8 | 8 | * Exception thrown when too many errors occur in the some() or any() methods. |
9 | 9 | */ |
10 | -class AggregateException extends RejectionException |
|
11 | -{ |
|
10 | +class AggregateException extends RejectionException { |
|
12 | 11 | public function __construct(string $msg, array $reasons) |
13 | 12 | { |
14 | 13 | parent::__construct( |