forked from frodeborli/moebius-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Promise.php
187 lines (172 loc) · 7.03 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
<?php
namespace Moebius;
use Closure;
use Moebius\Promise\ProtoPromise;
use Moebius\PromiseInterface;
class Promise extends ProtoPromise {
public function __construct(Closure $resolveFunction) {
$resolveFunction($this->fulfill(...), $this->reject(...));
}
/**
* Check carefully that the provided value is a promise-like
* object.
*/
public static final function isPromise($promise): bool {
if (!\is_object($promise)) {
return false;
}
if (!\method_exists($promise, 'then')) {
return false;
}
if (
$promise instanceof PromiseInterface ||
(class_exists(\GuzzleHttp\Promise\PromiseInterface::class, false) && $promise instanceof \GuzzleHttp\Promise\PromiseInterface) ||
(class_exists(\React\Promise\PromiseInterface::class, false) && $promise instanceof \React\Promise\PromiseInterface) ||
(class_exists(\Http\Promise\Promise::class, false) && $promise instanceof \Http\Promise\Promise)
) {
return true;
}
$rm = new \ReflectionMethod($promise, 'then');
if ($rm->isStatic()) {
return false;
}
foreach ($rm->getParameters() as $index => $rp) {
if ($rp->hasType()) {
$rt = $rp->getType();
if ($rt instanceof \ReflectionNamedType) {
if (
$rt->getName() !== 'mixed' &&
$rt->getName() !== 'callable' &&
$rt->getName() !== \Closure::class
) {
return false;
}
} else {
}
}
if ($rp->isVariadic()) {
return true;
}
if ($index === 1) {
return true;
}
}
return false;
}
/**
* Cast a promise-like object to a Moebius\Promise.
*/
public static final function cast(object $promise): PromiseInterface {
if ($promise instanceof PromiseInterface) {
return $promise;
}
if (!self::isPromise($promise)) {
throw new \TypeError("Expected a promise-like object");
}
$result = ProtoPromise::getInstance();
$promise->then($result->fulfill(...), $result->reject(...));
return $result;
}
/**
* The Promise.all() method takes an iterable of promises as an input, and returns a single
* Promise that resolves to an array of the results of the input promises. This returned promise
* will resolve when all of the input's promises have resolved, or if the input iterable contains
* no promises. It rejects immediately upon any of the input promises rejecting or non-promises
* throwing an error, and will reject with this first rejection message / error.
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
*/
public static final function all(iterable $promises): PromiseInterface {
$promise = ProtoPromise::getInstance();
$results = [];
$offset = 0;
$counter = count($promises);
foreach ($promises as $theirPromise) {
$results[$offset] = null;
static::cast($theirPromise)->then(function(mixed $result) use (&$results, $offset, &$counter, $promise) {
$results[$offset] = $result;
if (--$counter === 0) {
$promise->fulfill($results);
}
}, function(mixed $reason) use ($promise) {
$promise->reject($reason);
});
$offset++;
}
if ($results === []) {
$promise->fulfill([]);
}
return $promise;
}
/**
* The Promise::allSettled() method returns a promise that resolves after all of the given
* promises have either fulfilled or rejected, with an array of objects that each describes
* the outcome of each promise.
*
* It is typically used when you have multiple asynchronous tasks that are not dependent on
* one another to complete successfully, or you'd always like to know the result of each
* promise.
*
* In comparison, the Promise returned by Promise::all() may be more appropriate if the
* tasks are dependent on each other / if you'd like to immediately reject upon any of them
* rejecting.
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
*/
public static final function allSettled(iterable $promises): PromiseInterface {
$promise = ProtoPromise::getInstance();
$results = [];
$counter = count($promises);
foreach ($promises as $theirPromise) {
$results[] = $theirPromise = static::cast($theirPromise);
$theirPromise->then(function(mixed $result) use (&$counter, $promise, &$results) {
if (--$counter === 0) {
$promise->fulfill($results);
}
});
}
return $promise;
}
/**
* Promise.any() takes an iterable of Promise objects. It returns a single promise that resolves
* as soon as any of the promises in the iterable fulfills, with the value of the fulfilled
* promise. If no promises in the iterable fulfill (if all of the given promises are rejected),
* then the returned promise is rejected with an AggregateException, a subclass of \Exception that
* groups together individual errors.
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
*/
public static final function any(iterable $promises): PromiseInterface {
$promise = ProtoPromise::getInstance();
$errors = [];
$counter = count($promises);
foreach ($promises as $offset => $theirPromise) {
static::cast($theirPromise)->then(function($result) use ($promise) {
$promise->fulfill($result);
}, function($reason) use ($promise, &$counter, $offset, &$errors) {
$errors[$offset] = $reason;
if (--$counter === 0) {
$promise->reject($errors);
}
});
}
return $promise;
}
/**
* The Promise::race() method returns a promise that fulfills or rejects as soon as one of the promises
* in an iterable fulfills or rejects, with the value or reason from that promise.
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race
*/
public static final function race(iterable $promises): PromiseInterface {
$promise = ProtoPromise::getInstance();
foreach ($promises as $theirPromise) {
static::cast($theirPromise)->then(function($result) use ($promise) {
$promise->fulfill($result);
}, function($reason) use ($promise) {
$promise->reject($reason);
});
}
return $promise;
}
}