diff --git a/lib/gaze.js b/lib/gaze.js index 0821a41..0c89959 100644 --- a/lib/gaze.js +++ b/lib/gaze.js @@ -34,6 +34,7 @@ function Gaze (patterns, opts, done) { opts = opts || {}; opts.mark = true; opts.interval = opts.interval || 100; + opts.debounceLeading = (opts.debounceLeading === true || opts.debounceLeading === 'true') opts.debounceDelay = opts.debounceDelay || 500; opts.cwd = opts.cwd || process.cwd(); this.options = opts; @@ -110,15 +111,24 @@ Gaze.prototype.emit = function () { // If cached doesnt exist, create a delay before running the next // then emit the event const cache = this._cached[filepath] || []; + const emitEvent = (args, e) => { + // Emit the event and `all` event + Gaze.super_.prototype.emit.apply(this, args); + Gaze.super_.prototype.emit.apply(this, ['all', e].concat([].slice.call(args, 1))); + } if (cache.indexOf(e) === -1) { helper.objectPush(this._cached, filepath, e); clearTimeout(timeoutId); timeoutId = setTimeout(() => { + if (this.options.debounceLeading) { + emitEvent(args, e); + } delete this._cached[filepath]; }, this.options.debounceDelay); // Emit the event and `all` event - Gaze.super_.prototype.emit.apply(this, args); - Gaze.super_.prototype.emit.apply(this, ['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