paulmillr/chokidar — pull request #150 by nopnop (2014-09-10 13:40:31+00:00)
Title: fix: Closing a chokidar instance prevent other instances to watch a file

Using two (or more) instances of chokidar on the same file(s) with `usePolling=true` does not work properly. If one of those instances is closed, other instance does not watch files anymore:

```javascript
var chokidar = require('chokidar')
var watchFile = __filename;

var watchOnceAndClose = chokidar.watch(watchFile, { persistent:true })
watchOnceAndClose.on('change', function(file) {
console.log('[watchOnceAndClose] event:change')
console.log('[watchOnceAndClose] close()')
watchOnceAndClose.close();
})

var watchAndContinue = chokidar.watch(watchFile, { persistent:true })
watchAndContinue.on('change', function() {
console.log('[watchAndContinue] event:change')
})

process.on('exit', function() {
console.log('!!! No more file to watch... exit')
})
```

Changes on the file, with `usePolling`, produce:

```bash
[watchOnceAndClose] event:change
[watchOnceAndClose] close()
[watchAndContinue] event:change
!!! No more file to watch... exit
```

But this should produce (like without `usePolling`):

```bash
[watchOnceAndClose] event:change
[watchOnceAndClose] close()
[watchAndContinue] event:change
[watchAndContinue] event:change
[watchAndContinue] event:change
[watchAndContinue] event:change
# ...
```

When `usePolling` is `true` chokidar use `fs.unwatchFile()` without the listener callback provided to `fs.watchFile()`. According to [fs.unwatchFile api](http://nodejs.org/api/fs.html#fs_fs_unwatchfile_filename_listener) *all listeners are removed* if `fs.unwatchFile()` is called without the `listener` to remove.

This PR fix this issue by storing the listener of each watched file.