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

Add Watcher implementations #2

Merged
merged 54 commits into from
Mar 29, 2019
Merged

Add Watcher implementations #2

merged 54 commits into from
Mar 29, 2019

Conversation

devongovett
Copy link
Member

This adds subscribe and unsubscribe methods to watch for changes to a directory. There are several backends:

  • FSEvents on macOS
  • Inotify on Linux
  • ReadDirectoryChangesW on Windows
  • watchman if available

Unlike nsfw, which starts up a thread per watched directory, fschanges starts up a single thread per backend and adds multiple watchers to it. Once all watchers have been closed, the backend thread is also closed.

Events are also denounced. Rather than spamming the JS thread when file changes come in, we queue up changes and emit them when things settle down. This is very useful for e.g. npm installs, git checkouts, or other large changes where we end up calling into JS only once with all of the events rather than separately for each one.

Another advantage to having the watchers be part of this package is that we can keep our in-memory snapshot up to date incrementally instead of potentially traversing the entire filesystem again on exit. This should make Windows and Linux a bit faster.

@mischnic
Copy link
Member

mischnic commented Mar 23, 2019

subscribe and getEventsSince returns relative paths with watchman and absolute paths with fsevents

fschanges (watcher *)$ node index.js
[ { path: 'yarn.lock', type: 'update' } ]
[ { path: 'yarn.lock', type: 'update' } ]
[ { path: 'index.js', type: 'update' } ]
^C
(watcher *)$ node index.js
[ { path:
     '/Users/niklas/.../parcel/fschanges/yarn.lock',
    type: 'update' } ]

@devongovett
Copy link
Member Author

yeah I was planning on writing some tests to ensure consistency next.

@devongovett
Copy link
Member Author

🎉

@devongovett devongovett merged commit 39fc0a6 into master Mar 29, 2019
@devongovett devongovett deleted the watcher branch March 29, 2019 04:54
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.

2 participants