forked from webonyx/graphql-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncPromiseAdapter.php
More file actions
165 lines (137 loc) · 4.45 KB
/
SyncPromiseAdapter.php
File metadata and controls
165 lines (137 loc) · 4.45 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php declare(strict_types=1);
namespace GraphQL\Executor\Promise\Adapter;
use GraphQL\Deferred;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Utils\Utils;
/**
* Allows changing order of field resolution even in sync environments
* (by leveraging queue of deferreds and promises).
*/
class SyncPromiseAdapter implements PromiseAdapter
{
public function isThenable($value): bool
{
return $value instanceof SyncPromise;
}
public function convertThenable($thenable): Promise
{
if (! $thenable instanceof SyncPromise) {
// End-users should always use Deferred (and don't use SyncPromise directly)
$deferred = Deferred::class;
$safeThenable = Utils::printSafe($thenable);
throw new InvariantViolation("Expected instance of {$deferred}, got {$safeThenable}");
}
return new Promise($thenable, $this);
}
public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null): Promise
{
$adoptedPromise = $promise->adoptedPromise;
\assert($adoptedPromise instanceof SyncPromise);
return new Promise($adoptedPromise->then($onFulfilled, $onRejected), $this);
}
public function create(callable $resolver): Promise
{
$promise = new SyncPromise();
try {
$resolver(
[$promise, 'resolve'],
[$promise, 'reject']
);
} catch (\Throwable $e) {
$promise->reject($e);
}
return new Promise($promise, $this);
}
public function createFulfilled($value = null): Promise
{
$promise = new SyncPromise();
return new Promise($promise->resolve($value), $this);
}
public function createRejected(\Throwable $reason): Promise
{
$promise = new SyncPromise();
return new Promise($promise->reject($reason), $this);
}
public function all(iterable $promisesOrValues): Promise
{
$all = new SyncPromise();
$total = \is_array($promisesOrValues)
? \count($promisesOrValues)
: \iterator_count($promisesOrValues);
$count = 0;
$result = [];
$resolveAllWhenFinished = function () use (&$count, &$total, $all, &$result): void {
if ($count === $total) {
$all->resolve($result);
}
};
foreach ($promisesOrValues as $index => $promiseOrValue) {
if ($promiseOrValue instanceof Promise) {
$result[$index] = null;
$promiseOrValue->then(
static function ($value) use (&$result, $index, &$count, &$resolveAllWhenFinished): void {
$result[$index] = $value;
++$count;
$resolveAllWhenFinished();
},
[$all, 'reject']
);
} else {
$result[$index] = $promiseOrValue;
++$count;
}
}
$resolveAllWhenFinished();
return new Promise($all, $this);
}
/**
* Synchronously wait when promise completes.
*
* @template V
*
* @param Promise<V> $promise
*
* @throws InvariantViolation
*
* @return V
*/
public function wait(Promise $promise)
{
$this->beforeWait($promise);
$taskQueue = SyncPromise::getQueue();
$syncPromise = $promise->adoptedPromise;
\assert($syncPromise instanceof SyncPromise);
while (
$syncPromise->state === SyncPromise::PENDING
&& ! $taskQueue->isEmpty()
) {
SyncPromise::runQueue();
$this->onWait($promise);
}
if ($syncPromise->state === SyncPromise::FULFILLED) {
return $syncPromise->result;
}
if ($syncPromise->state === SyncPromise::REJECTED) {
throw $syncPromise->result;
}
throw new InvariantViolation('Could not resolve promise');
}
/**
* Execute just before starting to run promise completion.
*
* @param Promise<mixed> $promise
*/
protected function beforeWait(Promise $promise): void
{
}
/**
* Execute while running promise completion.
*
* @param Promise<mixed> $promise
*/
protected function onWait(Promise $promise): void
{
}
}