diff --git a/lib/gaze.js b/lib/gaze.js index 9b00810..d57f9fb 100644 --- a/lib/gaze.js +++ b/lib/gaze.js @@ -43,6 +43,7 @@ function Gaze(patterns, opts, done) { opts = opts || {}; opts.mark = true; opts.interval = opts.interval || 500; + opts.debounceLeading = (opts.debounceLeading == 'true') opts.debounceDelay = opts.debounceDelay || 500; opts.cwd = opts.cwd || process.cwd(); this.options = opts; @@ -167,15 +168,25 @@ Gaze.prototype.emit = function() { // If cached doesnt exist, create a delay before running the next // then emit the event var cache = this._cached[filepath] || []; + var options = this.options; + var emitEvent = function (args, e) { + Gaze.super_.prototype.emit.apply(self, args); + Gaze.super_.prototype.emit.apply(self, ['all', e].concat([].slice.call(args, 1))); + } + if (cache.indexOf(e) === -1) { helper.objectPush(self._cached, filepath, e); clearTimeout(this._timeoutId); this._timeoutId = setTimeout(function() { + if (options.debounceLeading) { + emitEvent(args, e); + } delete self._cached[filepath]; }, this.options.debounceDelay); // Emit the event and `all` event - Gaze.super_.prototype.emit.apply(self, args); - Gaze.super_.prototype.emit.apply(self, ['all', e].concat([].slice.call(args, 1))); + if (!this.options.debounceLeading) { + emitEvent(args, e); + } } // Detect if new folder added to trigger for matching files within folder