Skip to content

Commit 847c549

Browse files
author
James Halliday
committed
track updating status to remove race condition that deletes watchers
1 parent a9788af commit 847c549

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function watchify (b, opts) {
1616
var delay = typeof opts.delay === 'number' ? opts.delay : 600;
1717
var changingDeps = {};
1818
var pending = false;
19+
var updating = false;
1920

2021
var wopts = {persistent: true};
2122
if (opts.ignoreWatch) {
@@ -93,6 +94,11 @@ function watchify (b, opts) {
9394
watchFile(mfile, dep);
9495
});
9596
});
97+
b.on('bundle', function (bundle) {
98+
bundle.on('error', onend);
99+
bundle.on('end', onend);
100+
function onend () { updating = false }
101+
});
96102

97103
function watchFile (file, dep) {
98104
dep = dep || file;
@@ -119,20 +125,25 @@ function watchify (b, opts) {
119125
function invalidate (id) {
120126
if (cache) delete cache[id];
121127
if (pkgcache) delete pkgcache[id];
128+
changingDeps[id] = true;
129+
if (updating) return;
130+
122131
if (fwatchers[id]) {
123132
fwatchers[id].forEach(function (w) {
124133
w.close();
125134
});
126135
delete fwatchers[id];
127136
delete fwatcherFiles[id];
128137
}
129-
changingDeps[id] = true;
130138

131139
// wait for the disk/editor to quiet down first:
132140
if (!pending) setTimeout(function () {
133141
pending = false;
134-
b.emit('update', Object.keys(changingDeps));
135-
changingDeps = {};
142+
if (!updating) {
143+
b.emit('update', Object.keys(changingDeps));
144+
updating = true;
145+
changingDeps = {};
146+
}
136147
}, delay);
137148
pending = true;
138149
}

0 commit comments

Comments
 (0)