atom / watcher

Atom Filesystem Watcher
MIT License
200 stars 36 forks source link

An initial option, like chokidar, would be nice #182

Open nono opened 5 years ago

nono commented 5 years ago

Prerequisites

Description

chokidar sends add events for files and directories viewed on the initial scan. There is an option ignoreInitial to avoid doing it. It'd be nice to have an initial option for @atom/watcher to do the default behavior of chokidar on this aspect. It's particularly useful on linux where the watcher is already browsing the tree to tell inotify what directories to watch, as it would avoid to have to do it again outside of the watcher (it would costs more syscalls and introduces risk of race conditions).

Steps to Reproduce

  1. const w = await watcher.watchPath('/var/log', { initial: true }, events => { /* ... */ })
  2. [and so on...]

Expected behavior:

It sends an event for the existing files and directories inside /var/log.

Actual behavior:

Nothing currently.

Versions

Master

Additional Information

It's a suggestion of an improvement, not a bug report.

nono commented 5 years ago

By the way, I've opened the same issue for NSFW: https://github.com/Axosoft/nsfw/issues/60

smashwilson commented 5 years ago

I'm 👍 on having this, although we likely won't need this for Atom itself so I'm less likely to jump on it :smile:

The tricky bits here are dealing with race conditions - the filesystem may be modified concurrently with the initial scan, so it's tricky to keep the events we emit consistent.

50Wliu commented 5 years ago

although we likely won't need this for Atom itself so I'm less likely to jump on it

I've restarted work on the tree-view watchPath PR and I'm thinking that this would be nice so that the initial watchPath call can give the current directory structure rather than having to do a recursive fs.readdir. (Then again, there might be an easier way to do this than the way I'm trying to right now that won't necessitate an initial option.)

CxRes commented 5 years ago

I believe the way Chokidar solves this is by comparing the timestamp of the files/folders with the time at which the watcher is initialized. Items created after the watcher initialization are moved into a separate buffer, which are emitted after the ready event.

Even simpler thing to do would be to just emit mtime for the items along with the event.

george-thomas-hill commented 3 years ago

Bump.

I'd love it if @atom/watcher had an initial option.

I'm trying to make an in-RAM model of all the files on a hard drive, and an initial option would be the best way to do that.

I'd love it if anyone has any ideas for work-arounds.

Thank you!