Skip to content

Commit 5e00fd2

Browse files
committed
debounced fixed
1 parent 922dfdf commit 5e00fd2

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,49 @@
1-
import { FileSystemWatcher, RelativePattern, Uri, workspace } from "vscode";
1+
import {
2+
FileSystemWatcher,
3+
RelativePattern,
4+
Uri,
5+
workspace,
6+
} from "vscode";
27
import { arrayEquals, debounce } from "../../utils";
38
import { dbtProjectContainer } from "../dbtProjectContainer";
4-
import { OnProjectConfigChanged, ProjectConfigChangedEvent } from "../event/projectConfigChangedEvent";
9+
import {
10+
OnProjectConfigChanged,
11+
ProjectConfigChangedEvent,
12+
} from "../event/projectConfigChangedEvent";
513
import { SourceFileChangedEvent } from "../event/sourceFileChangedEvent";
614

715
export class SourceFileWatchers implements OnProjectConfigChanged {
816
private currentSourcePaths?: string[];
917
private sourceFolderWatchers: FileSystemWatcher[] = [];
1018

11-
public onProjectConfigChanged(event: ProjectConfigChangedEvent) {
19+
onProjectConfigChanged(event: ProjectConfigChangedEvent) {
1220
const { sourcePaths, projectRoot } = event;
1321
if (
1422
this.currentSourcePaths === undefined ||
1523
!arrayEquals(this.currentSourcePaths, sourcePaths)
1624
) {
17-
sourcePaths.forEach(sourcePath => {
25+
sourcePaths.forEach((sourcePath) => {
1826
const parsedSourcePath = Uri.parse(sourcePath);
19-
const globPattern = Uri.joinPath(parsedSourcePath, '**/*.sql').path.substring(1);
27+
const globPattern = Uri.joinPath(
28+
parsedSourcePath,
29+
"**/*.sql"
30+
).path.substring(1);
2031
const sourceFolderWatcher = workspace.createFileSystemWatcher(
2132
new RelativePattern(projectRoot, globPattern)
2233
);
2334
const event = new SourceFileChangedEvent(projectRoot);
24-
sourceFolderWatcher.onDidChange(() => debounce(() => dbtProjectContainer.raiseSourceFileChangedEvent(event), 2000)());
35+
36+
const debouncedSourceFileChangedEvent = debounce(
37+
() => dbtProjectContainer.raiseSourceFileChangedEvent(event),
38+
2000
39+
);
40+
41+
sourceFolderWatcher.onDidChange(() =>
42+
debouncedSourceFileChangedEvent()
43+
);
2544
this.sourceFolderWatchers.push(sourceFolderWatcher);
2645
});
2746
this.currentSourcePaths = sourcePaths;
2847
}
29-
};
48+
}
3049
}

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ export const arrayEquals = <T>(a: Array<T>, b: Array<T>): boolean => {
6666
};
6767

6868
export const debounce = (fn: Function, wait: number) => {
69-
let timeout: NodeJS.Timeout;
70-
71-
return function () {
69+
let timeout: number;
70+
return () => {
7271
clearTimeout(timeout);
73-
timeout = setTimeout(fn(), wait);
72+
timeout = setTimeout(fn, wait);
7473
};
7574
};
7675

76+
// TODO: potentially needs disposables
7777
export const setupWatcherHandler = (
7878
watcher: FileSystemWatcher,
7979
handler: Function

0 commit comments

Comments
 (0)