Skip to content

Commit

Permalink
GH-3413: Made the FS watcher start non-blocking.
Browse files Browse the repository at this point in the history
Closes #3413

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta committed May 10, 2019
1 parent fe6ea99 commit 607795c
Showing 1 changed file with 33 additions and 25 deletions.
Expand Up @@ -107,8 +107,7 @@ export class NsfwFileSystemWatcherServer implements FileSystemWatcherServer {
if (options.ignored.length > 0) {
this.debug('Files ignored for watching', options.ignored);
}

let watcher: nsfw.NSFW | undefined = await nsfw(fs.realpathSync(basePath), (events: nsfw.ChangeEvent[]) => {
return nsfw(fs.realpathSync(basePath), (events: nsfw.ChangeEvent[]) => {
for (const event of events) {
if (event.action === nsfw.actions.CREATED) {
this.pushAdded(watcherId, this.resolvePath(event.directory, event.file!));
Expand All @@ -130,30 +129,39 @@ export class NsfwFileSystemWatcherServer implements FileSystemWatcherServer {
console.warn(`Failed to watch "${basePath}":`, error);
this.unwatchFileChanges(watcherId);
}
}).then((watcher: nsfw.NSFW | undefined) => {
if (!watcher) {
return;
}
watcher.start().then(() => {
this.options.info('Started watching:', basePath);
if (toDisposeWatcher.disposed) {
if (watcher) {
this.debug('Stopping watching:', basePath);
watcher.stop().then(() => {
// remove a reference to nsfw otherwise GC cannot collect it
watcher = undefined;
this.options.info('Stopped watching:', basePath);
return;
});
}
}
toDisposeWatcher.push(Disposable.create(async () => {
this.watcherOptions.delete(watcherId);
if (watcher) {
this.debug('Stopping watching:', basePath);
watcher.stop().then(() => {
// 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))
});
});
});
await watcher.start();
this.options.info('Started watching:', basePath);
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;
}
toDisposeWatcher.push(Disposable.create(async () => {
this.watcherOptions.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))
});
}

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

0 comments on commit 607795c

Please sign in to comment.