Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-3413: Made the FS watcher start non-blocking. #5111

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can it be undefined here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot be, but based on the original code, we set the reference to undefined, so this is the only way to trick the compiler. By the way, see the original code, it was undefined there as well. I think this change is correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see, it seems before tsc was smart enough to figure out that it cannot be undefined after calling nsfw

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(() => {
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