shama/gaze — pull request #39 by jpommerening (2013-05-08 13:54:23+00:00)
Title: Multiple instances watching the same file

Hey Kyle!

Commit 4f960ab adds a timeout to the test `multipleWatchersSimultaneously` that causes the test to fail if the expected number of "changed" events were not fired after one second. (Fixing #31)

Commit 601ef79 fixes the problem.

The problem seems to have been the usage of `fs.unwatchFile( file [, listener] )` without a listener, causing node to remove *all* listeners, even those that were registered by another gaze instance. Additionally, `fs.unwatchFile` was even called if the file was never polled.

I fixed it by storing all callbacks that are passed to `fs.watchFile` in a map (file -> callback) and only unregister the callbacks, that the instance registered.

New "private" methods:

* `_unpollFile`: If the given file was once passed to `fs.watchFile`, unregister exactly the callback that was used by this instance.
* `_unpollDir`: First "unpoll" the directory, then all it's files.