Debian Codegraph @

node/throttle-debounce/umd/index.js

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