fireworm
fireworm copied to clipboard
All events are broadcasted twice
adding, removing, changing any file matching glob will trigger two events of respective kind
Not able to reproduce with my basic manual tests. Could you give more detail? Perhaps a gist or a repo that reproduces it.
I'm running it on Windows... I'll try to get more details to you when I have a moment
here's a gist that reproduces this issue
https://gist.github.com/hexparrot/fc431960d08749d9061b
The bug is somewhere here: https://github.com/airportyh/fireworm/blob/master/lib/dir.js#L57
It's possible that there's some sort of race condition. Do we need to use some sort of mutex here?
Yeah, so if entryNames
contains multiple duplicates, multiple fs.stat
would be called, resulting in multiple emit signals.
i.e., you need to use the sync version of fs.stat
to avoid the bug.
Here's a simpler code that demonstrates a problem.
Array.prototype.includes = function(el) {
return !(this.indexOf(el) == -1);
}
In the synchronous mode nothing unusual happens:
var seen = [];
['file1', 'file1'].forEach(function(filename) {
if(!seen.includes(filename)) {
console.log(filename);
seen.push(filename);
}
});
file1
is printed once as expected.
But when we add asynchrony, the disaster occurs:
var seen = [];
['file1', 'file1'].forEach(function(filename, i) {
if(!seen.includes(filename)) {
setTimeout(function() {
console.log(filename);
seen.push(filename);
}, 1000);
}
});
Ok, noticed this: https://github.com/airportyh/fireworm/blob/master/lib/dir.js#L110
So the bug isn't there. The bug is that there's no such check for directories, so they're added twice, resulting in duplicate files.