Skip to content

Commit 395ae89

Browse files
edgardmessiasJohnstonCode
authored andcommitted
refactor: Removed fsWatcher from repository (#524)
1 parent 8471670 commit 395ae89

File tree

3 files changed

+85
-29
lines changed

3 files changed

+85
-29
lines changed

src/repository.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
toDisposable
4747
} from "./util";
4848
import { match, matchAll } from "./util/globMatch";
49+
import { RepositoryFilesWatcher } from "./watchers/repositoryFilesWatcher";
4950

5051
function shouldShowProgress(operation: Operation): boolean {
5152
switch (operation) {
@@ -78,6 +79,11 @@ export class Repository implements IRemoteRepository {
7879

7980
private lastPromptAuth?: Thenable<IAuth | undefined>;
8081

82+
private _fsWatcher: RepositoryFilesWatcher;
83+
public get fsWatcher() {
84+
return this._fsWatcher;
85+
}
86+
8187
private _onDidChangeRepository = new EventEmitter<Uri>();
8288
public readonly onDidChangeRepository: Event<Uri> = this
8389
._onDidChangeRepository.event;
@@ -171,32 +177,13 @@ export class Repository implements IRemoteRepository {
171177
}
172178

173179
constructor(public repository: BaseRepository) {
174-
const fsWatcher = workspace.createFileSystemWatcher("**");
175-
this.disposables.push(fsWatcher);
176-
177-
const onWorkspaceChange = anyEvent(
178-
fsWatcher.onDidChange,
179-
fsWatcher.onDidCreate,
180-
fsWatcher.onDidDelete
181-
);
182-
183-
const onRepositoryChange = filterEvent(
184-
onWorkspaceChange,
185-
uri => !/^\.\./.test(path.relative(repository.root, uri.fsPath))
186-
);
187-
const onRelevantRepositoryChange = filterEvent(
188-
onRepositoryChange,
189-
uri => !/[\\\/]\.svn[\\\/]tmp/.test(uri.path)
190-
);
191-
192-
onRelevantRepositoryChange(this.onFSChange, this, this.disposables);
180+
this._fsWatcher = new RepositoryFilesWatcher(repository.root);
181+
this.disposables.push(this._fsWatcher);
193182

194-
const onRelevantSvnChange = filterEvent(onRelevantRepositoryChange, uri =>
195-
/[\\\/]\.svn[\\\/]/.test(uri.path)
196-
);
183+
this._fsWatcher.onDidAny(this.onFSChange, this, this.disposables);
197184

198185
// TODO on svn switch event fired two times since two files were changed
199-
onRelevantSvnChange(
186+
this._fsWatcher.onDidSvnAny(
200187
async (e: Uri) => {
201188
await this.repository.updateInfo();
202189
this._onDidChangeRepository.fire(e);
@@ -263,13 +250,12 @@ export class Repository implements IRemoteRepository {
263250
);
264251

265252
// For each deleted file, append to list
266-
const onFsDelete = filterEvent(
267-
fsWatcher.onDidDelete,
268-
uri => !/[\\\/]\.svn[\\\/]/.test(uri.path)
253+
this._fsWatcher.onDidWorkspaceDelete(
254+
uri => this.deletedUris.push(uri),
255+
this,
256+
this.disposables
269257
);
270258

271-
onFsDelete(uri => this.deletedUris.push(uri), this, this.disposables);
272-
273259
// Only check deleted files after the status list is fully updated
274260
this.onDidChangeStatus(this.actionForDeletedFiles, this, this.disposables);
275261

src/test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const testRunner = IstanbulTestRunner;
1111
const mochaOpts: Mocha.MochaOptions = {
1212
ui: "tdd", // the TDD UI is being used in extension.test.ts (suite, test, etc.)
1313
useColors: true, // colored output from test results,
14-
timeout: 10000, // default timeout: 10 seconds
14+
timeout: 30000, // default timeout: 10 seconds
1515
retries: 1,
1616
reporter: "mocha-multi-reporters",
1717
reporterOptions: {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Event, EventEmitter, Uri, workspace } from "vscode";
2+
import { anyEvent, filterEvent, IDisposable, isDescendant } from "../util";
3+
4+
export class RepositoryFilesWatcher implements IDisposable {
5+
private disposables: IDisposable[] = [];
6+
7+
public onDidChange: Event<Uri>;
8+
public onDidCreate: Event<Uri>;
9+
public onDidDelete: Event<Uri>;
10+
public onDidAny: Event<Uri>;
11+
12+
public onDidWorkspaceChange: Event<Uri>;
13+
public onDidWorkspaceCreate: Event<Uri>;
14+
public onDidWorkspaceDelete: Event<Uri>;
15+
public onDidWorkspaceAny: Event<Uri>;
16+
17+
public onDidSvnChange: Event<Uri>;
18+
public onDidSvnCreate: Event<Uri>;
19+
public onDidSvnDelete: Event<Uri>;
20+
public onDidSvnAny: Event<Uri>;
21+
22+
constructor(readonly root: string) {
23+
const fsWatcher = workspace.createFileSystemWatcher("**");
24+
this.disposables.push(fsWatcher);
25+
26+
const ignoreTmp = (uri: Uri) => !/[\\\/]\.svn[\\\/]tmp/.test(uri.path);
27+
28+
const ignoreNonRelevants = (uri: Uri) =>
29+
ignoreTmp(uri) && !isDescendant(this.root, uri.fsPath);
30+
31+
this.onDidChange = filterEvent(fsWatcher.onDidChange, ignoreNonRelevants);
32+
this.onDidCreate = filterEvent(fsWatcher.onDidCreate, ignoreNonRelevants);
33+
this.onDidDelete = filterEvent(fsWatcher.onDidDelete, ignoreNonRelevants);
34+
35+
this.onDidAny = anyEvent(
36+
this.onDidChange,
37+
this.onDidCreate,
38+
this.onDidDelete
39+
);
40+
41+
const svnPattern = /[\\\/]\.svn[\\\/]/;
42+
43+
const ignoreSvn = (uri: Uri) => !svnPattern.test(uri.path);
44+
45+
this.onDidWorkspaceChange = filterEvent(this.onDidChange, ignoreSvn);
46+
this.onDidWorkspaceCreate = filterEvent(this.onDidCreate, ignoreSvn);
47+
this.onDidWorkspaceDelete = filterEvent(this.onDidDelete, ignoreSvn);
48+
49+
this.onDidWorkspaceAny = anyEvent(
50+
this.onDidWorkspaceChange,
51+
this.onDidWorkspaceCreate,
52+
this.onDidWorkspaceDelete
53+
);
54+
const ignoreWorkspace = (uri: Uri) => svnPattern.test(uri.path);
55+
56+
this.onDidSvnChange = filterEvent(this.onDidChange, ignoreWorkspace);
57+
this.onDidSvnCreate = filterEvent(this.onDidCreate, ignoreWorkspace);
58+
this.onDidSvnDelete = filterEvent(this.onDidDelete, ignoreWorkspace);
59+
60+
this.onDidSvnAny = anyEvent(
61+
this.onDidSvnChange,
62+
this.onDidSvnCreate,
63+
this.onDidSvnDelete
64+
);
65+
}
66+
67+
public dispose(): void {
68+
this.disposables.forEach(d => d.dispose());
69+
}
70+
}

0 commit comments

Comments
 (0)