forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.ts
More file actions
48 lines (37 loc) · 1.21 KB
/
Copy pathasync.ts
File metadata and controls
48 lines (37 loc) · 1.21 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
function done<T>(promise: Promise<T>): Promise<void> {
return promise.then<void>(() => undefined);
}
export function throttle<T>(fn: () => Promise<T>): () => Promise<T> {
let current: Promise<T> | undefined;
let next: Promise<T> | undefined;
const trigger = (): Promise<T> => {
if (next) {
return next;
}
if (current) {
next = done(current).then(() => {
next = undefined;
return trigger();
});
return next;
}
current = fn();
const clear = () => (current = undefined);
done(current).then(clear, clear);
return current;
};
return trigger;
}
export function debounce<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void {
let timer: NodeJS.Timeout | undefined;
return (...args: Parameters<T>) => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => fn(...args), delay);
};
}