/
wait-until.ts
79 lines (65 loc) · 2.23 KB
/
wait-until.ts
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
import { futureTick } from './-utils';
const TIMEOUTS = [0, 1, 2, 5, 7];
const MAX_TIMEOUT = 10;
type Falsy = false | 0 | '' | null | undefined;
export interface Options {
timeout?: number;
timeoutMessage?: string;
}
/**
Wait for the provided callback to return a truthy value.
This does not leverage `settled()`, and as such can be used to manage async
while _not_ settled (e.g. "loading" or "pending" states).
@public
@param {Function} callback the callback to use for testing when waiting should stop
@param {Object} [options] options used to override defaults
@param {number} [options.timeout=1000] the maximum amount of time to wait
@param {string} [options.timeoutMessage='waitUntil timed out'] the message to use in the reject on timeout
@returns {Promise} resolves with the callback value when it returns a truthy value
@example
<caption>
Waiting until a selected element displays text:
</caption>
await waitUntil(function() {
return find('.my-selector').textContent.includes('something')
}, { timeout: 2000 })
*/
export default function waitUntil<T>(
callback: () => T | void | Falsy,
options: Options = {}
): Promise<T> {
let timeout = 'timeout' in options ? (options.timeout as number) : 1000;
let timeoutMessage =
'timeoutMessage' in options
? options.timeoutMessage
: 'waitUntil timed out';
// creating this error eagerly so it has the proper invocation stack
let waitUntilTimedOut = new Error(timeoutMessage);
return new Promise(function (resolve, reject) {
let time = 0;
// eslint-disable-next-line require-jsdoc
function scheduleCheck(timeoutsIndex: number) {
let knownTimeout = TIMEOUTS[timeoutsIndex];
let interval = knownTimeout === undefined ? MAX_TIMEOUT : knownTimeout;
futureTick(function () {
time += interval;
let value: T | void | Falsy;
try {
value = callback();
} catch (error) {
reject(error);
return;
}
if (value) {
resolve(value);
} else if (time < timeout) {
scheduleCheck(timeoutsIndex + 1);
} else {
reject(waitUntilTimedOut);
return;
}
}, interval);
}
scheduleCheck(0);
});
}