-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathdebounce.ts
More file actions
29 lines (24 loc) · 767 Bytes
/
debounce.ts
File metadata and controls
29 lines (24 loc) · 767 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
import { Disposable } from 'vscode';
export interface SimpleDebounce extends Disposable {
trigger(): void;
}
class SimpleDebounceImpl implements SimpleDebounce {
private timeout: NodeJS.Timeout | undefined;
constructor(private readonly ms: number, private readonly callback: () => void) {}
public trigger(): void {
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
this.callback();
}, this.ms);
}
public dispose(): void {
if (this.timeout) {
clearTimeout(this.timeout);
}
}
}
export function createSimpleDebounce(ms: number, callback: () => void): SimpleDebounce {
return new SimpleDebounceImpl(ms, callback);
}