-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
Promise.php
216 lines (176 loc) · 5.9 KB
/
Promise.php
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
<?php
namespace React\Promise;
class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface
{
private $canceller;
private $result;
private $handlers = [];
private $progressHandlers = [];
private $requiredCancelRequests = 0;
private $cancelRequests = 0;
public function __construct(callable $resolver, callable $canceller = null)
{
$this->canceller = $canceller;
$this->call($resolver);
}
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null !== $this->result) {
return $this->result->then($onFulfilled, $onRejected, $onProgress);
}
if (null === $this->canceller) {
return new static($this->resolver($onFulfilled, $onRejected, $onProgress));
}
$this->requiredCancelRequests++;
return new static($this->resolver($onFulfilled, $onRejected, $onProgress), function () {
if (++$this->cancelRequests < $this->requiredCancelRequests) {
return;
}
$this->cancel();
});
}
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
if (null !== $this->result) {
return $this->result->done($onFulfilled, $onRejected, $onProgress);
}
$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) {
$promise
->done($onFulfilled, $onRejected);
};
if ($onProgress) {
$this->progressHandlers[] = $onProgress;
}
}
public function otherwise(callable $onRejected)
{
return $this->then(null, function ($reason) use ($onRejected) {
if (!_checkTypehint($onRejected, $reason)) {
return new RejectedPromise($reason);
}
return $onRejected($reason);
});
}
public function always(callable $onFulfilledOrRejected)
{
return $this->then(function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
return $value;
});
}, function ($reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return new RejectedPromise($reason);
});
});
}
public function progress(callable $onProgress)
{
return $this->then(null, null, $onProgress);
}
public function cancel()
{
if (null === $this->canceller || null !== $this->result) {
return;
}
$canceller = $this->canceller;
$this->canceller = null;
$this->call($canceller);
}
private function resolver(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
return function ($resolve, $reject, $notify) use ($onFulfilled, $onRejected, $onProgress) {
if ($onProgress) {
$progressHandler = function ($update) use ($notify, $onProgress) {
try {
$notify($onProgress($update));
} catch (\Throwable $e) {
$notify($e);
} catch (\Exception $e) {
$notify($e);
}
};
} else {
$progressHandler = $notify;
}
$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject, $progressHandler) {
$promise
->then($onFulfilled, $onRejected)
->done($resolve, $reject, $progressHandler);
};
$this->progressHandlers[] = $progressHandler;
};
}
private function resolve($value = null)
{
if (null !== $this->result) {
return;
}
$this->settle(resolve($value));
}
private function reject($reason = null)
{
if (null !== $this->result) {
return;
}
$this->settle(reject($reason));
}
private function notify($update = null)
{
if (null !== $this->result) {
return;
}
foreach ($this->progressHandlers as $handler) {
$handler($update);
}
}
private function settle(ExtendedPromiseInterface $promise)
{
$promise = $this->unwrap($promise);
$handlers = $this->handlers;
$this->progressHandlers = $this->handlers = [];
$this->result = $promise;
foreach ($handlers as $handler) {
$handler($promise);
}
}
private function unwrap($promise)
{
$promise = $this->extract($promise);
while ($promise instanceof self && null !== $promise->result) {
$promise = $this->extract($promise->result);
}
return $promise;
}
private function extract($promise)
{
if ($promise instanceof LazyPromise) {
$promise = $promise->promise();
}
if ($promise === $this) {
return new RejectedPromise(
new \LogicException('Cannot resolve a promise with itself.')
);
}
return $promise;
}
private function call(callable $callback)
{
try {
$callback(
function ($value = null) {
$this->resolve($value);
},
function ($reason = null) {
$this->reject($reason);
},
function ($update = null) {
$this->notify($update);
}
);
} catch (\Throwable $e) {
$this->reject($e);
} catch (\Exception $e) {
$this->reject($e);
}
}
}