-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
34 lines (29 loc) · 990 Bytes
/
index.ts
File metadata and controls
34 lines (29 loc) · 990 Bytes
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
export const VERSION = '1.4.0';
export function sleep(timeout: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, timeout));
}
export function queueFactory() {
let queueImpl: Promise<unknown> = Promise.resolve();
return <T,>(fx: () => Promise<T> | T): Promise<T> => {
queueImpl = queueImpl.then(fx, fx);
return queueImpl as Promise<T>;
};
}
export function throttlingQueueFactory({ delay }: { delay: number }) {
let queueImpl = queueFactory();
return (fx: () => unknown): Promise<unknown> => {
const res = queueImpl(fx);
queueImpl(() => sleep(delay));
return res;
};
}
export function singleInstance<T extends (...args: any[]) => Promise<unknown>>(fx: T) {
let instance: ReturnType<T> | null = null;
return (...args: Parameters<T>): ReturnType<T> => {
if (!instance) {
instance = fx(...args) as ReturnType<T>;
instance.then(() => instance = null, () => instance = null);
}
return instance;
};
}