Browse files

Workaround for known fs.watch() multiple-firing bug.

  • Loading branch information...
1 parent 5e33114 commit a047ab6f8f44e9d3a18ca29f95d39f3834c84cdf @meowgorithm committed Jan 24, 2013
Showing with 24 additions and 0 deletions.
  1. +24 −0 lib/cakewalk.js
View
24 lib/cakewalk.js
@@ -57,7 +57,31 @@ Builder.prototype = {
*/
start: function() {
var self = this;
+
+ // So fs.watch() is a little unstable and has a known bug where it
+ // often over-fires on OS X and Ubuntu. Until this changes, we work
+ // around this by using a setTimeout() to block rapid-fire fs.watch()
+ // events. See:
+ //
+ // https://github.com/joyent/node/issues/search?q=fs.watch%20twice
+ // https://github.com/joyent/node/issues/2054
+ //
+ // Also note that fs.watch() is a wrapper around a few different tools
+ // across various platforms, so behavior is known to be inconsistent
+ // and, at times, unreliable. See:
+ //
+ // http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener
+ var blocked = true;
+
fs.watch(this.source, function(event, filename) {
+
+ if(blocked) {
+ setTimeout(function() { blocked = false; }, 10);
+ return;
+ }
+
+ blocked = true;
+
if(event == 'change') {
self.build();
}

0 comments on commit a047ab6

Please sign in to comment.