From c8e0a7ac8d17e4ebe7800fa6f7cd5d1ef3aa6d76 Mon Sep 17 00:00:00 2001 From: Aaron Hamid Date: Mon, 23 Mar 2015 16:50:06 -0400 Subject: [PATCH] add support for debouncing leading event --- lib/gaze.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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