forked from webonyx/graphql-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncPromise.php
More file actions
224 lines (194 loc) · 6.34 KB
/
SyncPromise.php
File metadata and controls
224 lines (194 loc) · 6.34 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php declare(strict_types=1);
namespace GraphQL\Executor\Promise\Adapter;
use GraphQL\Error\InvariantViolation;
/**
* Simplistic (yet full-featured) implementation of Promises A+ spec for regular PHP `sync` mode
* (using queue to defer promises execution).
*
* Library users are not supposed to use SyncPromise class in their resolvers.
* Instead, they should use @see \GraphQL\Deferred which enforces `$executor` callback in the constructor.
*
* Root SyncPromise without explicit `$executor` will never resolve (actually throw while trying).
* The whole point of Deferred is to ensure it never happens and that any resolver creates
* at least one $executor to start the promise chain.
*
* @template V
*/
class SyncPromise
{
public const PENDING = 'pending';
public const FULFILLED = 'fulfilled';
public const REJECTED = 'rejected';
public string $state = self::PENDING;
/** @var mixed */
public $result;
/**
* Promises created in `then` method of this promise and awaiting resolution of this promise.
*
* @var array<
* int,
* array{
* self<V>,
* (callable(mixed): mixed)|null,
* (callable(\Throwable): mixed)|null
* }
* >
*/
protected array $waiting = [];
public static function runQueue(): void
{
$q = self::getQueue();
while (! $q->isEmpty()) {
$task = $q->dequeue();
$task();
}
}
/**
* @param (callable(): V)|null $executor
*/
public function __construct(?callable $executor = null)
{
if ($executor === null) {
return;
}
self::getQueue()->enqueue(function () use ($executor): void {
try {
$this->resolve($executor());
} catch (\Throwable $e) {
$this->reject($e);
}
});
}
/**
* @param mixed $value
*
* @return self<V>
*/
public function resolve($value): self
{
switch ($this->state) {
case self::PENDING:
if ($value === $this) {
throw new \Exception('Cannot resolve promise with self');
}
if (\is_object($value) && \method_exists($value, 'then')) {
$value->then(
function ($resolvedValue): void {
$this->resolve($resolvedValue);
},
function ($reason): void {
$this->reject($reason);
}
);
return $this;
}
$this->state = self::FULFILLED;
$this->result = $value;
$this->enqueueWaitingPromises();
break;
case self::FULFILLED:
if ($this->result !== $value) {
throw new \Exception('Cannot change value of fulfilled promise');
}
break;
case self::REJECTED:
throw new \Exception('Cannot resolve rejected promise');
}
return $this;
}
/**
* @return self<null>
*/
public function reject(\Throwable $reason): self
{
switch ($this->state) {
case self::PENDING:
$this->state = self::REJECTED;
$this->result = $reason;
$this->enqueueWaitingPromises();
break;
case self::REJECTED:
if ($reason !== $this->result) {
throw new \Exception('Cannot change rejection reason');
}
break;
case self::FULFILLED:
throw new \Exception('Cannot reject fulfilled promise');
}
return $this;
}
private function enqueueWaitingPromises(): void
{
if ($this->state === self::PENDING) {
throw new InvariantViolation('Cannot enqueue derived promises when parent is still pending');
}
foreach ($this->waiting as $descriptor) {
self::getQueue()->enqueue(function () use ($descriptor): void {
[$promise, $onFulfilled, $onRejected] = $descriptor;
if ($this->state === self::FULFILLED) {
try {
$promise->resolve($onFulfilled === null ? $this->result : $onFulfilled($this->result));
} catch (\Throwable $e) {
$promise->reject($e);
}
} elseif ($this->state === self::REJECTED) {
try {
if ($onRejected === null) {
$promise->reject($this->result);
} else {
$promise->resolve($onRejected($this->result));
}
} catch (\Throwable $e) {
$promise->reject($e);
}
}
});
}
$this->waiting = [];
}
/**
* @return \SplQueue<callable(): void>
*/
public static function getQueue(): \SplQueue
{
static $queue;
return $queue ??= new \SplQueue();
}
/**
* @template VFulfilled
* @template VRejected
*
* @param (callable(V): VFulfilled)|null $onFulfilled
* @param (callable(\Throwable): VRejected)|null $onRejected
*
* @return self<(
* $onFulfilled is not null
* ? ($onRejected is not null ? VFulfilled|VRejected : VFulfilled)
* : ($onRejected is not null ? VRejected : V)
* )>
*/
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): self
{
if ($this->state === self::REJECTED && $onRejected === null) {
return $this;
}
if ($this->state === self::FULFILLED && $onFulfilled === null) {
return $this;
}
$tmp = new self();
$this->waiting[] = [$tmp, $onFulfilled, $onRejected];
if ($this->state !== self::PENDING) {
$this->enqueueWaitingPromises();
}
return $tmp;
}
/**
* @param callable(\Throwable): mixed $onRejected
*
* @return self<null>
*/
public function catch(callable $onRejected): self
{
return $this->then(null, $onRejected);
}
}