Repository navigation
Expand file tree
/
Copy pathwait.ts
More file actions
52 lines (46 loc) · 1.43 KB
/
Copy pathwait.ts
File metadata and controls
52 lines (46 loc) · 1.43 KB
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
import { View } from '@nativescript/core';
/**
* Resolves on the next run-loop turn. On the main thread this gives the
* platform a chance to process layout, paint, and queued native callbacks.
*/
export function nextRenderPass(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 0));
}
export interface WaitUntilOptions {
timeout?: number;
interval?: number;
message?: string;
}
export async function waitUntil(
condition: () => boolean,
options: WaitUntilOptions = {},
): Promise<void> {
const timeout = options.timeout ?? 5_000;
const interval = options.interval ?? 50;
const deadline = Date.now() + timeout;
while (!condition()) {
if (Date.now() > deadline) {
throw new Error(
options.message ?? `waitUntil: condition not met within ${timeout}ms`,
);
}
await new Promise((resolve) => setTimeout(resolve, interval));
}
}
export function waitForLayout(view: View, timeout = 5_000): Promise<void> {
if (view.isLayoutValid) return Promise.resolve();
return new Promise((resolve, reject) => {
const handler = (): void => {
clearTimeout(timer);
view.off(View.layoutChangedEvent, handler);
resolve();
};
const timer = setTimeout(() => {
view.off(View.layoutChangedEvent, handler);
reject(
new Error(`waitForLayout: no layout pass within ${timeout}ms`),
);
}, timeout);
view.on(View.layoutChangedEvent, handler);
});
}