1 | import { __assign } from './_virtual/_tslib.js';
|
2 |
|
3 | var defaultWaitForOptions = {
|
4 | timeout: 10000
|
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 | function waitFor(actorRef, predicate, options) {
|
30 | var resolvedOptions = __assign(__assign({}, defaultWaitForOptions), options);
|
31 |
|
32 | return new Promise(function (res, rej) {
|
33 | var done = false;
|
34 |
|
35 | if (process.env.NODE_ENV !== 'production' && resolvedOptions.timeout < 0) {
|
36 | console.error('`timeout` passed to `waitFor` is negative and it will reject its internal promise immediately.');
|
37 | }
|
38 |
|
39 | var handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(function () {
|
40 | sub.unsubscribe();
|
41 | rej(new Error("Timeout of ".concat(resolvedOptions.timeout, " ms exceeded")));
|
42 | }, resolvedOptions.timeout);
|
43 |
|
44 | var dispose = function () {
|
45 | clearTimeout(handle);
|
46 | done = true;
|
47 | sub === null || sub === void 0 ? void 0 : sub.unsubscribe();
|
48 | };
|
49 |
|
50 | var sub = actorRef.subscribe({
|
51 | next: function (emitted) {
|
52 | if (predicate(emitted)) {
|
53 | dispose();
|
54 | res(emitted);
|
55 | }
|
56 | },
|
57 | error: function (err) {
|
58 | dispose();
|
59 | rej(err);
|
60 | },
|
61 | complete: function () {
|
62 | dispose();
|
63 | rej(new Error("Actor terminated without satisfying predicate"));
|
64 | }
|
65 | });
|
66 |
|
67 | if (done) {
|
68 | sub.unsubscribe();
|
69 | }
|
70 | });
|
71 | }
|
72 |
|
73 | export { waitFor };
|