Skip to content

Commit

Permalink
put the watcher back after a brief delay
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 11, 2011
1 parent ad7226c commit 03d3219
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
32 changes: 20 additions & 12 deletions index.js
Expand Up @@ -53,8 +53,13 @@ var exports = module.exports = function (entryFile, opts) {
;

if (opts.watch) {
var pending = {};

w.register(function (body, file) {
var watcher = function (curr, prev) {
if (pending[file]) return;
pending[file] = true;

if (curr.nlink === 0) {
// deleted
if (w.files[file]) {
Expand All @@ -67,19 +72,22 @@ var exports = module.exports = function (entryFile, opts) {
_cache = null;
}
else if (curr.mtime !== prev.mtime) {
// modified
fs.unwatchFile(file);
try {
w.reload(file);
_cache = null;
self.emit('bundle');
}
catch (e) {
self.emit('syntaxError', e);
if (self.listeners('syntaxError').length === 0) {
console.error(e && e.stack || e);
// modified, wait a little before reloading
// since modifications tend to come in waves
setTimeout(function () {
try {
w.reload(file);
_cache = null;
self.emit('bundle');
}
}
catch (e) {
self.emit('syntaxError', e);
if (self.listeners('syntaxError').length === 0) {
console.error(e && e.stack || e);
}
}
pending[file] = false;
}, 10);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name" : "browserify",
"version" : "1.7.6",
"version" : "1.7.7",
"description" : "Browser-side require() for js directories and npm modules",
"main" : "index.js",
"bin" : {
Expand Down
24 changes: 13 additions & 11 deletions test/watch.js
Expand Up @@ -32,18 +32,20 @@ test('watch', function (t) {

function getBundle (cb) {
var req = { host : 'localhost', port : port, path : '/bundle.js' };
http.get(req, function (res) {
t.equal(res.statusCode, 200);

var src = '';
res.on('data', function (buf) {
src += buf.toString();
});

res.on('end', function () {
cb(src)
setTimeout(function () {
http.get(req, function (res) {
t.equal(res.statusCode, 200);

var src = '';
res.on('data', function (buf) {
src += buf.toString();
});

res.on('end', function () {
cb(src)
});
});
});
}, 50);
}

function compareSources () {
Expand Down

0 comments on commit 03d3219

Please sign in to comment.