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:
parent
2a0521fba6
commit
65d21766b7
2 changed files with 37 additions and 27 deletions
|
@ -195,46 +195,51 @@
|
|||
};
|
||||
|
||||
watch = function(source, base) {
|
||||
var callback, compile, compileTimeout, prevStats, watchErr, watcher;
|
||||
var compile, compileTimeout, prevStats, rewatch, rewatchTimeout, watchErr, watcher;
|
||||
prevStats = null;
|
||||
compileTimeout = null;
|
||||
rewatchTimeout = null;
|
||||
watchErr = function(e) {
|
||||
if (e.code === 'ENOENT') {
|
||||
if (sources.indexOf(source) === -1) return;
|
||||
clearTimeout(rewatchTimeout);
|
||||
return rewatchTimeout = wait(25, function() {
|
||||
try {
|
||||
rewatch();
|
||||
return compile();
|
||||
} catch (e) {
|
||||
removeSource(source, base, true);
|
||||
return compileJoin();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
rewatch = function() {
|
||||
var watcher;
|
||||
if (typeof watcher !== "undefined" && watcher !== null) watcher.close();
|
||||
return watcher = fs.watch(source, compile);
|
||||
};
|
||||
compile = function() {
|
||||
clearTimeout(compileTimeout);
|
||||
return compileTimeout = wait(25, function() {
|
||||
return fs.stat(source, function(err, stats) {
|
||||
if (err) return watchErr(err);
|
||||
if (prevStats && (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime())) {
|
||||
return;
|
||||
return rewatch();
|
||||
}
|
||||
prevStats = stats;
|
||||
return fs.readFile(source, function(err, code) {
|
||||
if (err) return watchErr(err);
|
||||
return compileScript(source, code.toString(), base);
|
||||
compileScript(source, code.toString(), base);
|
||||
return rewatch();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
try {
|
||||
return watcher = fs.watch(source, callback = function(event) {
|
||||
compile();
|
||||
return wait(250, function() {
|
||||
try {
|
||||
watcher.close();
|
||||
return watcher = fs.watch(source, callback);
|
||||
} catch (e) {
|
||||
return watchErr(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
return watcher = fs.watch(source, compile);
|
||||
} catch (e) {
|
||||
return watchErr(e);
|
||||
}
|
||||
|
|
|
@ -174,35 +174,40 @@ watch = (source, base) ->
|
|||
|
||||
prevStats = null
|
||||
compileTimeout = null
|
||||
rewatchTimeout = null
|
||||
|
||||
watchErr = (e) ->
|
||||
if e.code is 'ENOENT'
|
||||
return if sources.indexOf(source) is -1
|
||||
clearTimeout rewatchTimeout
|
||||
rewatchTimeout = wait 25, ->
|
||||
try
|
||||
rewatch()
|
||||
compile()
|
||||
catch e
|
||||
removeSource source, base, yes
|
||||
compileJoin()
|
||||
else throw e
|
||||
|
||||
rewatch = ->
|
||||
watcher?.close()
|
||||
watcher = fs.watch source, compile
|
||||
|
||||
compile = ->
|
||||
clearTimeout compileTimeout
|
||||
compileTimeout = wait 25, ->
|
||||
fs.stat source, (err, stats) ->
|
||||
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())
|
||||
prevStats = stats
|
||||
fs.readFile source, (err, code) ->
|
||||
return watchErr err if err
|
||||
compileScript(source, code.toString(), base)
|
||||
rewatch()
|
||||
|
||||
try
|
||||
watcher = fs.watch source, callback = (event) ->
|
||||
compile()
|
||||
wait 250, ->
|
||||
try
|
||||
watcher.close()
|
||||
watcher = fs.watch source, callback
|
||||
catch e
|
||||
watchErr e
|
||||
watcher = fs.watch source, compile
|
||||
catch e
|
||||
watchErr e
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue