/
IdlePromise.ts
47 lines (45 loc) · 1.73 KB
/
IdlePromise.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 { IdleCallback, IdleCallbackOptions, IdleDeadline, IdleHandle } from "./implementation";
import { cancelIdleCallback, requestIdleCallback } from "./polyfill";
export interface IdlePromiseCancel extends Error {
readonly reason: string | undefined;
}
export const IdlePromiseCancel = (reason?: string): IdlePromiseCancel => {
const error: IdlePromiseCancel = new Error("Idle Promise Cancel") as IdlePromiseCancel;
Object.defineProperty(error, "reason", {
value: reason,
writable: false,
configurable: false,
enumerable: false,
});
return error;
};
export interface IdlePromise extends Promise<IdleDeadline> {
cancel(reason?: string): void;
}
export const IdlePromise = (options?: IdleCallbackOptions): IdlePromise => {
let handleCancellation: ((reason?: string) => void) | undefined = undefined;
const promise: IdlePromise = new Promise<IdleDeadline>(
(resolve, reject) => {
let handle: IdleHandle;
handleCancellation = (reason?: string) => {
handle = undefined;
cancelIdleCallback(handle);
handleCancellation = undefined;
reject(IdlePromiseCancel(reason))
};
const idleCallback: IdleCallback = (deadLine => {
handle = undefined;
handleCancellation = undefined;
resolve(deadLine);
});
requestIdleCallback(idleCallback, options);
}
) as IdlePromise;
Object.defineProperty(promise, "cancel", {
value: (reason: string) => handleCancellation && handleCancellation(reason),
writable: false,
configurable: false,
enumerable: false,
});
return promise;
};