Comments (4)
When investigating the bug, I think I understand the problem better now. I'll try to explain it.
- The code runs on Linux an calls
watch('/home', { recursive: true });
Here, watch()
will check is.directory()
and call watcher.watchDirectory()
.
watcher.watchDirectory()
will call hasNativeRecursive()
.
hasNativeRecursive()
tests if native fs.watch()
supports recursion. After setTimeout(, 200ms)
it will callback with false
.
Then, watcher.watchDirectory()
will continue and call getSubDirectories()
, and then make more calls watcher.watchDirectory()
until all sub directories are watched. This happens very quickly, but because fs.readdir()
is asynchronous, let us say it will take 1ms.
After 201ms, all directories are being watched. Now, step 2.
- The code calls
tree.newFile('home/new/file1');
. This will synchronously make directoryhome/new
and then synchronously create filefile1
inside it.
After the directory and file have been synchronously created, it is now time for Node.js to process the callback for FSWatcher
events. There is an event for the FSWatcher
object for /home
in our code, which says that /home/new
is created.
There is no event about /home/new/file1
being created, because we do not have an FSWatcher
for /home/new
.
Our code debounces the FSWatcher#change
callback by 200ms delay.
- The code makes change 1 to
tree.modify('home/new/file1');
, after 20ms.
There is no event about /home/new/file1
being changed, because we do not have an FSWatcher
for /home/new
yet.
- The code makes change 2 to
tree.modify('home/new/file1');
, after 50ms.
Still no FSWatcher to see the event...
- The code makes change 3 to
tree.modify('home/new/file1');
, after 150ms.
Still no FSWatcher...
- The debounce for
FSWatcher#change
callback is reached.
The callback is executed, and our code will:
- Check
is.directory
. - Call
self.watchDirectory()
for the new directory, to create anFSWatcher
. - Emit
change
to the user ofnode-watch
.
I propose that we move the debounce logic from the internal FSWatcher#change callback to only the emit('change')
for the user of node-watch.
This way, node-watch will create the FSWatcher as soon as it sees the directory created. That means it will see change 1 -2 -3. It also means it will still not see the creating of the file, but I think that is okay. This is only a problem when they are created synchronously (like in tests). In reality, when this is used by programs that help humans develop code (like watching unit tests), they cannot synchronously create a directory and file at the same time.
from node-watch.
The test case at #70 is passing for my locally with macOS, as well as on Travis with macOS. But, it is failing with Linux on Travis.
I am looking in the code to try and understand this. I see the following:
- Watch home (on Linux)
- Create directory home/new and create home/new/foo.
- Native
fs.watch
reportshome/new
as a new directory. node-watch
waits untildelay
.- After the delay is complete, a new watcher is created for
home/new
.
But, at this time, the file home/new/foo
has already been created and changes to it were not detected. When using a filter for a file extension (e.g. \.js$
) this means the file will never be found unless it is modified again after the delay.
I would like to help to solve this. What solution would you prefer? Perhaps we can separate the internal delay and external delay. So, that the new watcher is created a soon as possible.
from node-watch.
Forgive me, I don't quite follow my own code. It seems you understand better than me now and that's great!
I have just lost by laptop tonight (so sad) and I couldn't make some tests for some time.
I think I can trust you to make better decisions. Is it okay to add you as a collaborator ?
from node-watch.
@yuanchuan Oh no, I'm so sorry to hear that!
I'll be poking around this issue a bit over the holidays but don't feel any pressure on this, it's not urgent :)
I'd love to help out as a collaborator, but I would also prefer to receive review on my patches. It's a principle I follow in most of my projects to make sure at least two people share and understand how a specific part of the project works.
from node-watch.
Related Issues (20)
- Follow symlink HOT 2
- watch dir bug HOT 2
- Calling close() before the watcher is ready does nothing HOT 2
- Avoid synchronous fs calls HOT 1
- Catch errors when adding watchers for child directories HOT 1
- Catch error when use Node 14.15.3 on Linux SysTem HOT 2
- I got this error at first use lib in nodejs using express HOT 2
- node-watch is not working recursive in Ubuntu under WSL HOT 5
- when watch a dir that mount by nfs, if not work HOT 2
- New ES syntax HOT 2
- Only pass skip on initial recurse
- Travis CI is no longer free, try to switch to GitHub CI
- Fix tests on Windows
- fs.watch() not detecting changes to file from git operation HOT 3
- images update problem HOT 3
- Can TEMP_DIR able to change ?
- Is there a way, or a best practice to scan this large amount of files for processing ? HOT 4
- Add ignore option HOT 7
- multiple events for a file sent over network HOT 2
- High CPU consumption on window service HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-watch.