Skip to content

Commit a34b79b

Browse files
FractalBoyJohnstonCode
authored andcommitted
fix: Debounce repo watcher (#742)
1 parent 84ca646 commit a34b79b

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/watchers/repositoryFilesWatcher.ts

+27-19
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import { Event, Uri, workspace, EventEmitter } from "vscode";
22
import { watch } from "fs";
33
import { exists } from "../fs";
44
import { join } from "path";
5+
import { debounce } from "../decorators";
56
import { anyEvent, filterEvent, IDisposable, isDescendant } from "../util";
67

78
export class RepositoryFilesWatcher implements IDisposable {
89
private disposables: IDisposable[] = [];
910

11+
private _onRepoChange: EventEmitter<Uri>;
12+
private _onRepoCreate: EventEmitter<Uri>;
13+
private _onRepoDelete: EventEmitter<Uri>;
14+
1015
public onDidChange: Event<Uri>;
1116
public onDidCreate: Event<Uri>;
1217
public onDidDelete: Event<Uri>;
@@ -24,9 +29,9 @@ export class RepositoryFilesWatcher implements IDisposable {
2429

2530
constructor(readonly root: string) {
2631
const fsWatcher = workspace.createFileSystemWatcher("**");
27-
const _onRepoChange = new EventEmitter<Uri>();
28-
const _onRepoCreate = new EventEmitter<Uri>();
29-
const _onRepoDelete = new EventEmitter<Uri>();
32+
this._onRepoChange = new EventEmitter<Uri>();
33+
this._onRepoCreate = new EventEmitter<Uri>();
34+
this._onRepoDelete = new EventEmitter<Uri>();
3035
let onRepoChange: Event<Uri> | undefined;
3136
let onRepoCreate: Event<Uri> | undefined;
3237
let onRepoDelete: Event<Uri> | undefined;
@@ -36,27 +41,15 @@ export class RepositoryFilesWatcher implements IDisposable {
3641
!workspace.workspaceFolders.filter(w => isDescendant(w.uri.fsPath, root))
3742
.length
3843
) {
39-
const repoWatcher = watch(join(root, ".svn"), (event, filename) => {
40-
if (event === "change") {
41-
_onRepoChange.fire(Uri.parse(filename));
42-
} else if (event === "rename") {
43-
exists(filename).then(doesExist => {
44-
if (doesExist) {
45-
_onRepoCreate.fire(Uri.parse(filename));
46-
} else {
47-
_onRepoDelete.fire(Uri.parse(filename));
48-
}
49-
});
50-
}
51-
});
44+
const repoWatcher = watch(join(root, ".svn"), this.repoWatch);
5245

5346
repoWatcher.on("error", error => {
5447
throw error;
5548
});
5649

57-
onRepoChange = _onRepoChange.event;
58-
onRepoCreate = _onRepoCreate.event;
59-
onRepoDelete = _onRepoDelete.event;
50+
onRepoChange = this._onRepoChange.event;
51+
onRepoCreate = this._onRepoCreate.event;
52+
onRepoDelete = this._onRepoDelete.event;
6053
}
6154

6255
this.disposables.push(fsWatcher);
@@ -108,6 +101,21 @@ export class RepositoryFilesWatcher implements IDisposable {
108101
);
109102
}
110103

104+
@debounce(1000)
105+
private repoWatch(event: string, filename: string): void {
106+
if (event === "change") {
107+
this._onRepoChange.fire(Uri.parse(filename));
108+
} else if (event === "rename") {
109+
exists(filename).then(doesExist => {
110+
if (doesExist) {
111+
this._onRepoCreate.fire(Uri.parse(filename));
112+
} else {
113+
this._onRepoDelete.fire(Uri.parse(filename));
114+
}
115+
});
116+
}
117+
}
118+
111119
public dispose(): void {
112120
this.disposables.forEach(d => d.dispose());
113121
}

0 commit comments

Comments
 (0)