1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Executor\Promise\Adapter; |
6
|
|
|
|
7
|
|
|
use GraphQL\Executor\Promise\Promise; |
8
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter; |
9
|
|
|
use GraphQL\Utils\Utils; |
10
|
|
|
use React\Promise\Promise as ReactPromise; |
11
|
|
|
use React\Promise\PromiseInterface as ReactPromiseInterface; |
12
|
|
|
use function React\Promise\all; |
13
|
|
|
use function React\Promise\reject; |
14
|
|
|
use function React\Promise\resolve; |
15
|
|
|
|
16
|
|
|
class ReactPromiseAdapter implements PromiseAdapter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
|
|
public function isThenable($value) |
22
|
|
|
{ |
23
|
|
|
return $value instanceof ReactPromiseInterface; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function convertThenable($thenable) |
30
|
|
|
{ |
31
|
|
|
return new Promise($thenable, $this); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null) |
38
|
|
|
{ |
39
|
|
|
/** @var ReactPromiseInterface $adoptedPromise */ |
40
|
|
|
$adoptedPromise = $promise->adoptedPromise; |
41
|
|
|
|
42
|
|
|
return new Promise($adoptedPromise->then($onFulfilled, $onRejected), $this); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function create(callable $resolver) |
49
|
|
|
{ |
50
|
|
|
$promise = new ReactPromise($resolver); |
51
|
|
|
|
52
|
|
|
return new Promise($promise, $this); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
public function createFulfilled($value = null) |
59
|
|
|
{ |
60
|
|
|
$promise = resolve($value); |
61
|
|
|
|
62
|
|
|
return new Promise($promise, $this); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function createRejected($reason) |
69
|
|
|
{ |
70
|
|
|
$promise = reject($reason); |
71
|
|
|
|
72
|
|
|
return new Promise($promise, $this); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function all(array $promisesOrValues) |
79
|
|
|
{ |
80
|
|
|
// TODO: rework with generators when PHP minimum required version is changed to 5.5+ |
81
|
|
|
$promisesOrValues = Utils::map( |
82
|
|
|
$promisesOrValues, |
83
|
|
|
static function ($item) { |
84
|
|
|
return $item instanceof Promise ? $item->adoptedPromise : $item; |
85
|
|
|
} |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$promise = all($promisesOrValues)->then(static function ($values) use ($promisesOrValues) { |
89
|
|
|
$orderedResults = []; |
90
|
|
|
|
91
|
|
|
foreach ($promisesOrValues as $key => $value) { |
92
|
|
|
$orderedResults[$key] = $values[$key]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $orderedResults; |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
return new Promise($promise, $this); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|