Skip to content

Commit 73f3f3c

Browse files
committed
Try to make SimpleDebounce disposable
1 parent 2b56b82 commit 73f3f3c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/common/utils/debounce.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
export interface SimpleDebounce {
1+
import { Disposable } from 'vscode';
2+
3+
export interface SimpleDebounce extends Disposable {
24
trigger(): void;
35
}
46

5-
class SimpleDebounceImpl {
7+
class SimpleDebounceImpl extends Disposable {
68
private timeout: NodeJS.Timeout | undefined;
79

8-
constructor(private readonly ms: number, private readonly callback: () => void) {}
10+
constructor(private readonly ms: number, private readonly callback: () => void) {
11+
super(() => {
12+
if (this.timeout) {
13+
clearTimeout(this.timeout);
14+
this.timeout = undefined;
15+
}
16+
});
17+
}
918

1019
public trigger() {
1120
if (this.timeout) {
@@ -15,6 +24,12 @@ class SimpleDebounceImpl {
1524
this.callback();
1625
}, this.ms);
1726
}
27+
28+
public dispose() {
29+
if (this.timeout) {
30+
clearTimeout(this.timeout);
31+
}
32+
}
1833
}
1934

2035
export function createSimpleDebounce(ms: number, callback: () => void): SimpleDebounce {

0 commit comments

Comments
 (0)