Skip to content

Commit

Permalink
ensure that the nsfw watcher is disposed
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov authored and caseyflynn-google committed Dec 4, 2018
1 parent 6628246 commit 3a3f1a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('nsfw-filesystem-watcher', function () {
root = FileUri.create(fs.realpathSync(temp.mkdirSync('node-fs-root')));
watcherServer = createNsfwFileSystemWatcherServer();
watcherId = await watcherServer.watchFileChanges(root.toString());
await sleep(2000);
});

afterEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,28 @@ export class NsfwFileSystemWatcherServer implements FileSystemWatcherServer {
const watcherId = this.watcherSequence++;
const basePath = FileUri.fsPath(uri);
this.debug('Starting watching:', basePath);
const toDisposeWatcher = new DisposableCollection();
this.watchers.set(watcherId, toDisposeWatcher);
toDisposeWatcher.push(Disposable.create(() => this.watchers.delete(watcherId)));
if (fs.existsSync(basePath)) {
this.start(watcherId, basePath, options);
this.start(watcherId, basePath, options, toDisposeWatcher);
} else {
const disposable = new DisposableCollection();
const toClearTimer = new DisposableCollection();
const timer = setInterval(() => {
if (fs.existsSync(basePath)) {
disposable.dispose();
toClearTimer.dispose();
this.pushAdded(watcherId, basePath);
this.start(watcherId, basePath, options);
this.start(watcherId, basePath, options, toDisposeWatcher);
}
}, 500);
disposable.push(Disposable.create(() => {
this.watchers.delete(watcherId);
clearInterval(timer);
}));
this.toDispose.push(disposable);
return watcherId;
toClearTimer.push(Disposable.create(() => clearInterval(timer)));
toDisposeWatcher.push(toClearTimer);
}
this.toDispose.push(toDisposeWatcher);
return watcherId;
}

protected async start(watcherId: number, basePath: string, rawOptions?: WatchOptions): Promise<void> {
protected async start(watcherId: number, basePath: string, rawOptions: WatchOptions | undefined, toDisposeWatcher: DisposableCollection): Promise<void> {
const options: WatchOptions = {
ignored: [],
...rawOptions
Expand Down Expand Up @@ -127,30 +127,27 @@ export class NsfwFileSystemWatcherServer implements FileSystemWatcherServer {
});
await watcher.start();
this.options.info('Started watching:', basePath);
if (this.toDispose.disposed) {
if (toDisposeWatcher.disposed) {
this.debug('Stopping watching:', basePath);
await watcher.stop();
// remove a reference to nsfw otherwise GC cannot collect it
watcher = undefined;
this.options.info('Stopped watching:', basePath);
return;
}
const disposable = Disposable.create(async () => {
toDisposeWatcher.push(Disposable.create(async () => {
this.watcherOptions.delete(watcherId);
this.watchers.delete(watcherId);
if (watcher) {
this.debug('Stopping watching:', basePath);
await watcher.stop();
// remove a reference to nsfw otherwise GC cannot collect it
watcher = undefined;
this.options.info('Stopped watching:', basePath);
}
});
}));
this.watcherOptions.set(watcherId, {
ignored: options.ignored.map(pattern => new Minimatch(pattern))
});
this.watchers.set(watcherId, disposable);
this.toDispose.push(disposable);
}

unwatchFileChanges(watcherId: number): Promise<void> {
Expand Down

0 comments on commit 3a3f1a3

Please sign in to comment.