-
Notifications
You must be signed in to change notification settings - Fork 14
/
wait.ts
47 lines (43 loc) · 1.32 KB
/
wait.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
import { BasicTool } from "../basic.js";
const basicTool = new BasicTool();
export function waitUntil(
condition: () => boolean,
callback: () => void,
interval = 100,
timeout = 10000,
) {
const start = Date.now();
const intervalId = basicTool.getGlobal("setInterval")(() => {
if (condition()) {
basicTool.getGlobal("clearInterval")(intervalId);
callback();
} else if (Date.now() - start > timeout) {
basicTool.getGlobal("clearInterval")(intervalId);
}
}, interval);
}
export function waitUtilAsync(
condition: () => boolean,
interval = 100,
timeout = 10000,
) {
return new Promise<void>((resolve, reject) => {
const start = Date.now();
const intervalId = basicTool.getGlobal("setInterval")(() => {
if (condition()) {
basicTool.getGlobal("clearInterval")(intervalId);
resolve();
} else if (Date.now() - start > timeout) {
basicTool.getGlobal("clearInterval")(intervalId);
reject(new Error("timeout"));
}
}, interval);
});
}
export async function waitForReader(reader: _ZoteroTypes.ReaderInstance) {
await reader._initPromise;
await reader._lastView.initializedPromise;
if (reader.type === "pdf")
await (reader as _ZoteroTypes.ReaderInstance<"pdf">)._lastView
._iframeWindow!.PDFViewerApplication.initializedPromise;
}