diff --git a/lib/coffee-script/command.js b/lib/coffee-script/command.js index 8331b211..ba9b7559 100644 --- a/lib/coffee-script/command.js +++ b/lib/coffee-script/command.js @@ -188,20 +188,24 @@ }; watch = function(source, base) { - var callback, compile, prevStats, watchErr, watcher; + var callback, compile, compileTimeout, prevStats, watchErr, watcher; prevStats = null; + compileTimeout = null; compile = function() { - return fs.stat(source, function(err, stats) { - if (err) throw err; - if (prevStats && (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime())) { - return; - } - prevStats = stats; - return fs.readFile(source, function(err, code) { + clearTimeout(compileTimeout); + return compileTimeout = setTimeout(function() { + return fs.stat(source, function(err, stats) { if (err) throw err; - return compileScript(source, code.toString(), base); + if (prevStats && (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime())) { + return; + } + prevStats = stats; + return fs.readFile(source, function(err, code) { + if (err) throw err; + return compileScript(source, code.toString(), base); + }); }); - }); + }, 25); }; watchErr = function(e) { if (e.code === 'ENOENT') { diff --git a/src/command.coffee b/src/command.coffee index 601f4a2b..0bc8d956 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -168,16 +168,20 @@ loadRequires = -> watch = (source, base) -> prevStats = null + compileTimeout = null compile = -> - fs.stat source, (err, stats) -> - throw err if err - return if prevStats and (stats.size is prevStats.size and - stats.mtime.getTime() is prevStats.mtime.getTime()) - prevStats = stats - fs.readFile source, (err, code) -> + clearTimeout compileTimeout + compileTimeout = setTimeout -> + fs.stat source, (err, stats) -> throw err if err - compileScript(source, code.toString(), base) + return if prevStats and (stats.size is prevStats.size and + stats.mtime.getTime() is prevStats.mtime.getTime()) + prevStats = stats + fs.readFile source, (err, code) -> + throw err if err + compileScript(source, code.toString(), base) + , 25 watchErr = (e) -> if e.code is 'ENOENT'