-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathasyncUtils.ts
209 lines (188 loc) · 5.1 KB
/
asyncUtils.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
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
import { act } from "@testing-library/preact";
import { ResolverType } from "./_types";
export interface TimeoutOptions {
timeout?: number;
suppressErrors?: boolean;
}
class TimeoutError extends Error {
constructor(utilName: string, { timeout }: TimeoutOptions) {
super(`Timed out in ${utilName} after ${timeout}ms.`);
}
timeout = true;
}
function asyncUtils(addResolver: (resolver: ResolverType) => void) {
let nextUpdatePromise: Promise<any> | void;
async function waitForNextUpdate(options: TimeoutOptions = { timeout: 0 }) {
if (!nextUpdatePromise) {
nextUpdatePromise = new Promise((resolve, reject) => {
const timeoutId =
options.timeout! > 0
? setTimeout(() => {
reject(new TimeoutError("waitForNextUpdate", options));
}, options.timeout)
: null;
addResolver(() => {
if (timeoutId) {
clearTimeout(timeoutId);
}
nextUpdatePromise = undefined;
resolve();
});
});
}
await nextUpdatePromise;
}
async function wait(
callback: () => any,
options: TimeoutOptions = { timeout: 0, suppressErrors: true }
) {
const checkResult = () => {
try {
const callbackResult = callback();
return callbackResult || callbackResult === undefined;
} catch (err) {
if (!options.suppressErrors) {
throw err;
}
}
};
const waitForResult = async () => {
const initialTimeout = options.timeout;
while (true) {
const startTime = Date.now();
try {
await waitForNextUpdate({ timeout: options.timeout });
if (checkResult()) {
return;
}
} catch (err) {
if (err.timeout) {
throw new TimeoutError("wait", { timeout: initialTimeout });
}
throw err;
}
options.timeout! -= Date.now() - startTime;
}
};
if (!checkResult()) {
await waitForResult();
}
}
async function waitForValueToChange(
selector: () => any,
options: TimeoutOptions = { timeout: 0 }
) {
const initialValue = selector();
try {
await wait(() => selector() !== initialValue, {
suppressErrors: false,
...options,
});
} catch (err) {
if (err.timeout) {
throw new TimeoutError("waitForValueToChange", options);
}
throw err;
}
}
return {
waitForValueToChange,
waitForNextUpdate,
wait,
};
}
export default asyncUtils;
// function asyncUtils(addResolver: (r: ResolverType) => void) {
// let nextUpdatePromise: Promise<any> | null = null;
// const waitForNextUpdate = async (
// options: TimeoutOptions = { timeout: 0 }
// ) => {
// if (!nextUpdatePromise) {
// nextUpdatePromise = new Promise((resolve, reject) => {
// let timeoutId: NodeJS.Timeout;
// if (options.timeout && options.timeout > 0) {
// timeoutId = setTimeout(
// () => reject(new TimeoutError("waitForNextUpdate", options)),
// options.timeout
// );
// }
// addResolver(() => {
// clearTimeout(timeoutId);
// nextUpdatePromise = null;
// resolve();
// });
// });
// await act(() => {
// if (nextUpdatePromise) {
// return nextUpdatePromise;
// }
// return;
// });
// }
// await nextUpdatePromise;
// };
// const wait = async (
// callback: () => any,
// { timeout, suppressErrors }: WaitOptions = {
// timeout: 0,
// suppressErrors: true,
// }
// ) => {
// const checkResult = () => {
// try {
// const callbackResult = callback();
// return callbackResult || callbackResult === undefined;
// } catch (e) {
// if (!suppressErrors) {
// throw e;
// }
// }
// };
// const waitForResult = async () => {
// const initialTimeout = timeout;
// while (true) {
// const startTime = Date.now();
// try {
// await waitForNextUpdate({ timeout });
// if (checkResult()) {
// return;
// }
// } catch (e) {
// if (e.timeout) {
// throw new TimeoutError("wait", { timeout: initialTimeout });
// }
// throw e;
// }
// timeout -= Date.now() - startTime;
// }
// };
// if (!checkResult()) {
// await waitForResult();
// }
// };
// const waitForValueToChange = async (
// selector: () => any,
// options: TimeoutOptions = {
// timeout: 0,
// }
// ) => {
// const initialValue = selector();
// try {
// await wait(() => selector() !== initialValue, {
// suppressErrors: false,
// ...options,
// });
// } catch (e) {
// if (e.timeout) {
// throw new TimeoutError("waitForValueToChange", options);
// }
// throw e;
// }
// };
// return {
// wait,
// waitForNextUpdate,
// waitForValueToChange,
// };
// }
// export default asyncUtils;