-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpromise.re
414 lines (329 loc) · 9.19 KB
/
promise.re
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* This file is part of reason-promise, released under the MIT license. See
LICENSE.md for details, or visit
https://github.com/aantron/promise/blob/master/LICENSE.md. */
type rejectable(+'a, +'e);
type never;
type promise(+'a) = rejectable('a, never);
type t(+'a) = promise('a);
let onUnhandledException = ref(exn => {
prerr_endline("Unhandled exception in promise callback:");
Js.Console.error(exn);
});
[%%bs.raw {|
function PromiseBox(p) {
this.nested = p;
};
function unbox(value) {
if (value instanceof PromiseBox)
return value.nested;
else
return value;
}
function box(value) {
if (value != null && typeof value.then === 'function')
return new PromiseBox(value);
else
return value;
}
function make(executor) {
return new Promise(function (resolve, reject) {
var boxingResolve = function(value) {
resolve(box(value));
};
executor(boxingResolve, reject);
});
};
function resolved(value) {
return Promise.resolve(box(value));
};
function then(promise, callback) {
return promise.then(function (value) {
try {
return callback(unbox(value));
}
catch (exception) {
onUnhandledException.contents(exception);
}
});
};
function catch_(promise, callback) {
var safeCallback = function (error) {
try {
return callback(error);
}
catch (exception) {
onUnhandledException.contents(exception);
}
};
return promise.catch(safeCallback);
};
|}];
/* Compatibility with BukleScript < 6. */
type result('a, 'e) = Belt.Result.t('a, 'e) = Ok('a) | Error('e);
module Js_ = {
type t('a, 'e) = rejectable('a, 'e);
external relax: promise('a) => rejectable('a, _) = "%identity";
[@bs.val]
external jsNew:
(('a => unit) => ('e => unit) => unit) => rejectable('a, 'e) = "make";
let pending = () => {
let resolve = ref(ignore);
let reject = ref(ignore);
let p =
jsNew((resolve', reject') => {
resolve := resolve';
reject := reject';
});
(p, resolve^, reject^);
};
[@bs.val]
external resolved: 'a => rejectable('a, _) = "resolved";
[@bs.val]
external flatMap:
(rejectable('a, 'e), 'a => rejectable('b, 'e)) => rejectable('b, 'e) =
"then";
let map = (promise, callback) =>
flatMap(promise, v => resolved(callback(v)));
let get = (promise, callback) =>
ignore(map(promise, callback));
let tap = (promise, callback) => {
get(promise, callback);
promise;
};
[@bs.scope "Promise"]
[@bs.val]
external rejected: 'e => rejectable(_, 'e) = "reject";
[@bs.val]
external catch:
(rejectable('a, 'e), 'e => rejectable('a, 'e2)) => rejectable('a, 'e2) =
"catch_";
[@bs.val]
external unbox: 'a => 'a = "unbox";
[@bs.scope "Promise"]
[@bs.val]
external jsAll: 'a => 'b = "all";
let allArray = promises =>
map(jsAll(promises), promises => Belt.Array.map(promises, unbox));
let all = promises =>
map(allArray(Belt.List.toArray(promises)), Belt.List.fromArray);
let all2 = (p1, p2) =>
jsAll((p1, p2));
let all3 = (p1, p2, p3) =>
jsAll((p1, p2, p3));
let all4 = (p1, p2, p3, p4) =>
jsAll((p1, p2, p3, p4));
let all5 = (p1, p2, p3, p4, p5) =>
jsAll((p1, p2, p3, p4, p5));
let all6 = (p1, p2, p3, p4, p5, p6) =>
jsAll((p1, p2, p3, p4, p5, p6));
[@bs.scope "Promise"]
[@bs.val]
external jsRace: array(rejectable('a, 'e)) => rejectable('a, 'e) = "race";
let race = promises =>
if (promises == []) {
raise(Invalid_argument("Promise.race([]) would be pending forever"));
}
else {
jsRace(Belt.List.toArray(promises));
};
let toResult = promise =>
catch(map(promise, v => Ok(v)), e => resolved(Error(e)));
let fromResult = promise =>
flatMap(relax(promise), fun
| Ok(v) => resolved(v)
| Error(e) => rejected(e));
external fromBsPromise:
Js.Promise.t('a) => rejectable('a, Js.Promise.error) = "%identity";
external toBsPromise:
rejectable('a, _) => Js.Promise.t('a) = "%identity";
};
let pending = () => {
let (p, resolve, _) = Js_.pending();
(p, resolve);
};
let exec = executor => {
let (p, resolve) = pending();
executor(resolve);
p;
};
let resolved = Js_.resolved;
let flatMap = Js_.flatMap;
let map = Js_.map;
let get = Js_.get;
let tap = Js_.tap;
let all = Js_.all;
let all2 = Js_.all2;
let all3 = Js_.all3;
let all4 = Js_.all4;
let all5 = Js_.all5;
let all6 = Js_.all6;
let allArray = Js_.allArray;
let race = Js_.race;
let flatMapOk = (promise, callback) =>
flatMap(promise, result =>
switch (result) {
| Ok(v) => callback(v)
| Error(_) as error => resolved(error)
});
let flatMapError = (promise, callback) =>
flatMap(promise, result =>
switch (result) {
| Ok(_) as ok => resolved(ok)
| Error(e) => callback(e)
});
let mapOk = (promise, callback) =>
map(promise, result =>
switch (result) {
| Ok(v) => Ok(callback(v))
| Error(_) as error => error
});
let mapError = (promise, callback) =>
map(promise, result =>
switch (result) {
| Ok(_) as ok => ok
| Error(e) => Error(callback(e))
});
let getOk = (promise, callback) =>
get(promise, result =>
switch (result) {
| Ok(v) => callback(v)
| Error(_) => ()
});
let getError = (promise, callback) =>
get(promise, result =>
switch (result) {
| Ok(_) => ()
| Error(e) => callback(e)
});
let tapOk = (promise, callback) => {
getOk(promise, callback);
promise;
};
let tapError = (promise, callback) => {
getError(promise, callback);
promise;
};
let allOkArray = promises => {
let promiseCount = Array.length(promises);
let resultValues = Array.make(promiseCount, None);
let resultCount = ref(0);
let (resultPromise, resolve) = pending();
let (callbackRemover, removeCallbacks) = pending();
promises |> Array.iteri((index, promise) =>
/* Because callbacks are added to the user's promises through calls to the
JS runtime's Promise.race, this function leaks memory if and only if the
JS runtime's Promise functions leak memory. In particular, if one of the
promises resolves with Error(_), the callbacks on the other promises
should be removed. If not done, and long-pending promises are repeatedly
passed to allOk in a loop, they will gradually accumulate huge lists of
stale callbacks. This is also true of Promise.race, so we rely on the
quality of the runtime's Promise.race implementation to proactively
remove these callbacks. */
race([promise, callbackRemover])
|> wrapped => get(wrapped, result =>
switch (result) {
| Ok(v) =>
resultValues[index] = Some(v);
incr(resultCount);
if (resultCount^ >= promiseCount) {
resultValues
|> Array.map(v =>
switch (v) {
| Some(v) => v
| None => assert(false)
})
|> values => resolve(Ok(values))
};
| Error(e) =>
resolve(Error(e));
removeCallbacks(Error(e));
}));
resultPromise
};
let allOk = promises =>
mapOk(allOkArray(Array.of_list(promises)), Array.to_list);
let allOk2 = (p1, p2) => {
let promises = [|Obj.magic(p1), Obj.magic(p2)|];
mapOk(allOkArray(promises), fun
| [|v1, v2|] => (Obj.magic(v1), Obj.magic(v2))
| _ => assert(false))
};
let allOk3 = (p1, p2, p3) => {
let promises = [|Obj.magic(p1), Obj.magic(p2), Obj.magic(p3)|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3|] => (Obj.magic(v1), Obj.magic(v2), Obj.magic(v3))
| _ => assert(false))
};
let allOk4 = (p1, p2, p3, p4) => {
let promises = [|Obj.magic(p1), Obj.magic(p2), Obj.magic(p3), Obj.magic(p4)|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3, v4|] =>
(Obj.magic(v1), Obj.magic(v2), Obj.magic(v3), Obj.magic(v4))
| _ =>
assert(false))
};
let allOk5 = (p1, p2, p3, p4, p5) => {
let promises = [|
Obj.magic(p1),
Obj.magic(p2),
Obj.magic(p3),
Obj.magic(p4),
Obj.magic(p5)
|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3, v4, v5|] =>
(Obj.magic(v1), Obj.magic(v2), Obj.magic(v3), Obj.magic(v4), Obj.magic(v5))
| _ =>
assert(false))
};
let allOk6 = (p1, p2, p3, p4, p5, p6) => {
let promises = [|
Obj.magic(p1),
Obj.magic(p2),
Obj.magic(p3),
Obj.magic(p4),
Obj.magic(p5),
Obj.magic(p6)
|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3, v4, v5, v6|] =>
(
Obj.magic(v1),
Obj.magic(v2),
Obj.magic(v3),
Obj.magic(v4),
Obj.magic(v5),
Obj.magic(v6)
)
| _ =>
assert(false))
};
module Operators = {
let (>|=) = mapOk;
let (>>=) = flatMapOk;
};
let flatMapSome = (promise, callback) =>
flatMap(promise, option =>
switch (option) {
| Some(v) => callback(v)
| None => resolved(None)
});
let mapSome = (promise, callback) =>
map(promise, option =>
switch (option) {
| Some(v) => Some(callback(v))
| None => None
});
let getSome = (promise, callback) =>
get(promise, option =>
switch (option) {
| Some(v) => callback(v)
| None => ()
});
let tapSome = (promise, callback) => {
getSome(promise, callback);
promise;
};
module PipeFirst = {
};
module Js = Js_;