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