Repository navigation
Expand file tree
/
Copy pathReactPromiseAdapter.php
More file actions
104 lines (88 loc) · 2.86 KB
/
Copy pathReactPromiseAdapter.php
File metadata and controls
104 lines (88 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php declare(strict_types=1);
namespace GraphQL\Executor\Promise\Adapter;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use React\Promise\Promise as ReactPromise;
use React\Promise\PromiseInterface as ReactPromiseInterface;
use function React\Promise\all;
use function React\Promise\reject;
use function React\Promise\resolve;
/**
* @implements PromiseAdapter<ReactPromiseInterface<mixed>>
*/
class ReactPromiseAdapter implements PromiseAdapter
{
public function isThenable($value): bool
{
return $value instanceof ReactPromiseInterface;
}
/** @throws InvariantViolation */
public function convertThenable($thenable): Promise
{
return new Promise($thenable, $this);
}
/**
* @phpstan-param Promise<covariant ReactPromiseInterface<mixed>> $promise
*
* @throws InvariantViolation
*
* @phpstan-return Promise<ReactPromiseInterface<mixed>>
*/
public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null): Promise
{
$reactPromise = $promise->adoptedPromise;
return new Promise($reactPromise->then($onFulfilled, $onRejected), $this);
}
/**
* @throws InvariantViolation
*
* @phpstan-return Promise<ReactPromiseInterface<mixed>>
*/
public function create(callable $resolver): Promise
{
$reactPromise = new ReactPromise($resolver);
return new Promise($reactPromise, $this);
}
/**
* @throws InvariantViolation
*
* @phpstan-return Promise<ReactPromiseInterface<mixed>>
*/
public function createFulfilled($value = null): Promise
{
$reactPromise = resolve($value);
return new Promise($reactPromise, $this);
}
/**
* @throws InvariantViolation
*
* @phpstan-return Promise<ReactPromiseInterface<mixed>>
*/
public function createRejected(\Throwable $reason): Promise
{
$reactPromise = reject($reason);
return new Promise($reactPromise, $this);
}
/**
* @throws InvariantViolation
*
* @phpstan-return Promise<ReactPromiseInterface<mixed>>
*/
public function all(iterable $promisesOrValues): Promise
{
foreach ($promisesOrValues as &$promiseOrValue) {
if ($promiseOrValue instanceof Promise) {
$promiseOrValue = $promiseOrValue->adoptedPromise;
}
}
$promisesOrValuesArray = is_array($promisesOrValues)
? $promisesOrValues
: iterator_to_array($promisesOrValues);
$reactPromise = all($promisesOrValuesArray)->then(static fn (array $values): array => array_map(
static fn ($key) => $values[$key],
array_keys($promisesOrValuesArray),
));
return new Promise($reactPromise, $this);
}
}