Debian Codegraph @

node/throttle-debounce/cjs/index.js

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