| 1 |
/* eslint-disable no-undefined,no-param-reassign,no-shadow */ |
| 2 |
|
| 3 |
/** |
| 4 |
* Throttle execution of a function. Especially useful for rate limiting |
| 5 |
* execution of handlers on events like resize and scroll. |
| 6 |
* |
| 7 |
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) |
| 8 |
* are most useful. |
| 9 |
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, |
| 10 |
* as-is, to `callback` when the throttled-function is executed. |
| 11 |
* @param {object} [options] - An object to configure options. |
| 12 |
* @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds |
| 13 |
* while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed |
| 14 |
* one final time after the last throttled-function call. (After the throttled-function has not been called for |
| 15 |
* `delay` milliseconds, the internal counter is reset). |
| 16 |
* @param {boolean} [options.noLeading] - Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback |
| 17 |
* immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that |
| 18 |
* callback will never executed if both noLeading = true and noTrailing = true. |
| 19 |
* @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is |
| 20 |
* false (at end), schedule `callback` to execute after `delay` ms. |
| 21 |
* |
| 22 |
* @returns {Function} A new, throttled, function. |
| 23 |
*/ |
| 24 |
function throttle (delay, callback, options) { |
| 25 |
var _ref = options || {}, |
| 26 |
_ref$noTrailing = _ref.noTrailing, |
| 27 |
noTrailing = _ref$noTrailing === void 0 ? false : _ref$noTrailing, |
| 28 |
_ref$noLeading = _ref.noLeading, |
| 29 |
noLeading = _ref$noLeading === void 0 ? false : _ref$noLeading, |
| 30 |
_ref$debounceMode = _ref.debounceMode, |
| 31 |
debounceMode = _ref$debounceMode === void 0 ? undefined : _ref$debounceMode; |
| 32 |
/* |
| 33 |
* After wrapper has stopped being called, this timeout ensures that |
| 34 |
* `callback` is executed at the proper times in `throttle` and `end` |
| 35 |
* debounce modes. |
| 36 |
*/ |
| 37 |
var timeoutID; |
| 38 |
var cancelled = false; |
| 39 |
|
| 40 |
// Keep track of the last time `callback` was executed. |
| 41 |
var lastExec = 0; |
| 42 |
|
| 43 |
// Function to clear existing timeout |
| 44 |
function clearExistingTimeout() { |
| 45 |
if (timeoutID) { |
| 46 |
clearTimeout(timeoutID); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Function to cancel next exec |
| 51 |
function cancel(options) { |
| 52 |
var _ref2 = options || {}, |
| 53 |
_ref2$upcomingOnly = _ref2.upcomingOnly, |
| 54 |
upcomingOnly = _ref2$upcomingOnly === void 0 ? false : _ref2$upcomingOnly; |
| 55 |
clearExistingTimeout(); |
| 56 |
cancelled = !upcomingOnly; |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* The `wrapper` function encapsulates all of the throttling / debouncing |
| 61 |
* functionality and when executed will limit the rate at which `callback` |
| 62 |
* is executed. |
| 63 |
*/ |
| 64 |
function wrapper() { |
| 65 |
for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) { |
| 66 |
arguments_[_key] = arguments[_key]; |
| 67 |
} |
| 68 |
var self = this; |
| 69 |
var elapsed = Date.now() - lastExec; |
| 70 |
if (cancelled) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Execute `callback` and update the `lastExec` timestamp. |
| 75 |
function exec() { |
| 76 |
lastExec = Date.now(); |
| 77 |
callback.apply(self, arguments_); |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* If `debounceMode` is true (at begin) this is used to clear the flag |
| 82 |
* to allow future `callback` executions. |
| 83 |
*/ |
| 84 |
function clear() { |
| 85 |
timeoutID = undefined; |
| 86 |
} |
| 87 |
if (!noLeading && debounceMode && !timeoutID) { |
| 88 |
/* |
| 89 |
* Since `wrapper` is being called for the first time and |
| 90 |
* `debounceMode` is true (at begin), execute `callback` |
| 91 |
* and noLeading != true. |
| 92 |
*/ |
| 93 |
exec(); |
| 94 |
} |
| 95 |
clearExistingTimeout(); |
| 96 |
if (debounceMode === undefined && elapsed > delay) { |
| 97 |
if (noLeading) { |
| 98 |
/* |
| 99 |
* In throttle mode with noLeading, if `delay` time has |
| 100 |
* been exceeded, update `lastExec` and schedule `callback` |
| 101 |
* to execute after `delay` ms. |
| 102 |
*/ |
| 103 |
lastExec = Date.now(); |
| 104 |
if (!noTrailing) { |
| 105 |
timeoutID = setTimeout(debounceMode ? clear : exec, delay); |
| 106 |
} |
| 107 |
} else { |
| 108 |
/* |
| 109 |
* In throttle mode without noLeading, if `delay` time has been exceeded, execute |
| 110 |
* `callback`. |
| 111 |
*/ |
| 112 |
exec(); |
| 113 |
} |
| 114 |
} else if (noTrailing !== true) { |
| 115 |
/* |
| 116 |
* In trailing throttle mode, since `delay` time has not been |
| 117 |
* exceeded, schedule `callback` to execute `delay` ms after most |
| 118 |
* recent execution. |
| 119 |
* |
| 120 |
* If `debounceMode` is true (at begin), schedule `clear` to execute |
| 121 |
* after `delay` ms. |
| 122 |
* |
| 123 |
* If `debounceMode` is false (at end), schedule `callback` to |
| 124 |
* execute after `delay` ms. |
| 125 |
*/ |
| 126 |
timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay); |
| 127 |
} |
| 128 |
} |
| 129 |
wrapper.cancel = cancel; |
| 130 |
|
| 131 |
// Return the wrapper function. |
| 132 |
return wrapper; |
| 133 |
} |
| 134 |
|
| 135 |
/* eslint-disable no-undefined */ |
| 136 |
|
| 137 |
/** |
| 138 |
* Debounce execution of a function. Debouncing, unlike throttling, |
| 139 |
* guarantees that a function is only executed a single time, either at the |
| 140 |
* very beginning of a series of calls, or at the very end. |
| 141 |
* |
| 142 |
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful. |
| 143 |
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is, |
| 144 |
* to `callback` when the debounced-function is executed. |
| 145 |
* @param {object} [options] - An object to configure options. |
| 146 |
* @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds |
| 147 |
* after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call. |
| 148 |
* (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset). |
| 149 |
* |
| 150 |
* @returns {Function} A new, debounced function. |
| 151 |
*/ |
| 152 |
function debounce (delay, callback, options) { |
| 153 |
var _ref = options || {}, |
| 154 |
_ref$atBegin = _ref.atBegin, |
| 155 |
atBegin = _ref$atBegin === void 0 ? false : _ref$atBegin; |
| 156 |
return throttle(delay, callback, { |
| 157 |
debounceMode: atBegin !== false |
| 158 |
}); |
| 159 |
} |
| 160 |
|
| 161 |
export { debounce, throttle }; |
| 162 |
//# sourceMappingURL=index.js.map |