1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Rewatching files more liberally and consistently

This commit is contained in:
Trevor Burnham 2011-12-22 11:30:12 -08:00
parent 2a0521fba6
commit 65d21766b7
2 changed files with 37 additions and 27 deletions

View file

@ -195,46 +195,51 @@
}; };
watch = function(source, base) { watch = function(source, base) {
var callback, compile, compileTimeout, prevStats, watchErr, watcher; var compile, compileTimeout, prevStats, rewatch, rewatchTimeout, watchErr, watcher;
prevStats = null; prevStats = null;
compileTimeout = null; compileTimeout = null;
rewatchTimeout = null;
watchErr = function(e) { watchErr = function(e) {
if (e.code === 'ENOENT') { if (e.code === 'ENOENT') {
if (sources.indexOf(source) === -1) return; if (sources.indexOf(source) === -1) return;
clearTimeout(rewatchTimeout);
return rewatchTimeout = wait(25, function() {
try {
rewatch();
return compile();
} catch (e) {
removeSource(source, base, true); removeSource(source, base, true);
return compileJoin(); return compileJoin();
}
});
} else { } else {
throw e; throw e;
} }
}; };
rewatch = function() {
var watcher;
if (typeof watcher !== "undefined" && watcher !== null) watcher.close();
return watcher = fs.watch(source, compile);
};
compile = function() { compile = function() {
clearTimeout(compileTimeout); clearTimeout(compileTimeout);
return compileTimeout = wait(25, function() { return compileTimeout = wait(25, function() {
return fs.stat(source, function(err, stats) { return fs.stat(source, function(err, stats) {
if (err) return watchErr(err); if (err) return watchErr(err);
if (prevStats && (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime())) { if (prevStats && (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime())) {
return; return rewatch();
} }
prevStats = stats; prevStats = stats;
return fs.readFile(source, function(err, code) { return fs.readFile(source, function(err, code) {
if (err) return watchErr(err); if (err) return watchErr(err);
return compileScript(source, code.toString(), base); compileScript(source, code.toString(), base);
return rewatch();
}); });
}); });
}); });
}; };
try { try {
return watcher = fs.watch(source, callback = function(event) { return watcher = fs.watch(source, compile);
compile();
return wait(250, function() {
try {
watcher.close();
return watcher = fs.watch(source, callback);
} catch (e) {
return watchErr(e);
}
});
});
} catch (e) { } catch (e) {
return watchErr(e); return watchErr(e);
} }

View file

@ -174,35 +174,40 @@ watch = (source, base) ->
prevStats = null prevStats = null
compileTimeout = null compileTimeout = null
rewatchTimeout = null
watchErr = (e) -> watchErr = (e) ->
if e.code is 'ENOENT' if e.code is 'ENOENT'
return if sources.indexOf(source) is -1 return if sources.indexOf(source) is -1
clearTimeout rewatchTimeout
rewatchTimeout = wait 25, ->
try
rewatch()
compile()
catch e
removeSource source, base, yes removeSource source, base, yes
compileJoin() compileJoin()
else throw e else throw e
rewatch = ->
watcher?.close()
watcher = fs.watch source, compile
compile = -> compile = ->
clearTimeout compileTimeout clearTimeout compileTimeout
compileTimeout = wait 25, -> compileTimeout = wait 25, ->
fs.stat source, (err, stats) -> fs.stat source, (err, stats) ->
return watchErr err if err return watchErr err if err
return if prevStats and (stats.size is prevStats.size and return rewatch() if prevStats and (stats.size is prevStats.size and
stats.mtime.getTime() is prevStats.mtime.getTime()) stats.mtime.getTime() is prevStats.mtime.getTime())
prevStats = stats prevStats = stats
fs.readFile source, (err, code) -> fs.readFile source, (err, code) ->
return watchErr err if err return watchErr err if err
compileScript(source, code.toString(), base) compileScript(source, code.toString(), base)
rewatch()
try try
watcher = fs.watch source, callback = (event) -> watcher = fs.watch source, compile
compile()
wait 250, ->
try
watcher.close()
watcher = fs.watch source, callback
catch e
watchErr e
catch e catch e
watchErr e watchErr e