yuanchuan / node-watch

A wrapper and enhancements for fs.watch
https://npm.im/node-watch
MIT License
341 stars 44 forks source link

Should not watch filtered paths #93

Closed wmertens closed 4 years ago

wmertens commented 4 years ago

when "manually" recursing with a filter specified, I think node-watch watches paths that are always ignored.

I believe this test doesn't test what is says it tests: https://github.com/yuanchuan/node-watch/blob/3ad4726be15bd2475a6863345b8afc4229e47de8/test/test.js#L381

I think node-watch watches all paths recursively and then only filters the change events.

I imagine there may be a semantics issue around ignoring changes to parent directories but not children. Perhaps filter should be allowed to return -1 for "ignore but watch children", and falsy for "always ignore".

Also, if this works, it may be more efficient to "manually" recursively watch, so perhaps then it should not use native recursive watching when filter is specified?

yuanchuan commented 4 years ago

Hi, I'm not sure I fully understand.

Yes, you're right. On Linux, where the recursive watch is not supported natively, and when the recursive option is specified it will traverse all its sub-directories despite the filter function behaves.

So it's the same to filter inside the callback


function filter(name) {}

watch('./', { recursive: true }, function(evt, name) {
  if (filter(name)) {
    // action
  }
})
wmertens commented 4 years ago

Ok, so my request is to not watch paths that will be filtered out anyway.

And I proposed the -1 to indicate that a path has to be watched despite being filtered, although I'm not sure if that is a useful feature.

yuanchuan commented 4 years ago

That's an optimization. PRs welcome :)

yuanchuan commented 4 years ago

96 May be helpful, but I'm currently don't have enough energy to do so