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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a note about stop garbage collection #364

Merged
merged 1 commit into from Nov 26, 2021

Conversation

arcanis
Copy link
Contributor

@arcanis arcanis commented Aug 19, 2021

I spent a good amount of time trying to figure out why the watch would randomly stop working (or not work at all within workers), until I stumbled upon this comment by @pipobscure in an unrelated thread.

Given that JS API don't generally have visible side effects on garbage collection, I think it's worth adding a very visible note 馃槄

@jakub-g
Copy link
Contributor

jakub-g commented Nov 26, 2021

This note is pretty important (spent a good amount of time wondering the same), could someone please review and merge it?

> **Important note:** The API behaviour is slightly different from typical JS APIs. The `stop` function **must** be
> retrieved and stored somewhere, even if you don't plan to stop the watcher. If you forget it, the garbage collector
> will eventually kick in, the watcher will be unregistered, and your callbacks won't be called anymore.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering, maybe this note could be reflected in the sample code?
Something like:

const watchers = {};

const logAndWatch = (path) => {
    console.log('Watching ' + path)
    const stop = fsevents.watch(__dirname, (path, flags, id) => {
        const info = fsevents.getInfo(path, flags, id);
        console.dir(info);
    });
    watchers[path] = stop;
}

// Start observation and store the watcher
let myPath = '/path/to/somefile.txt';
logAndWatch(myPath);

// End observation, and get rid of the watcher
watchers[myPath]();
watchers[myPath] = null;

It's not as sweet as the original 4-liner, but can prevent some head scratching.

It seems inevitable to have to use an array or object to store the stop reference, otherwise in a local function the ref will go out of scope.

@pipobscure pipobscure merged commit 2f2a858 into fsevents:master Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants