|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.3 KB
|
| Rev | Line | |
|---|
| [a762898] | 1 | import { debounce as debounce$1 } from '../../function/debounce.mjs';
|
|---|
| 2 |
|
|---|
| 3 | function debounce(func, debounceMs = 0, options = {}) {
|
|---|
| 4 | if (typeof options !== 'object') {
|
|---|
| 5 | options = {};
|
|---|
| 6 | }
|
|---|
| 7 | const { leading = false, trailing = true, maxWait } = options;
|
|---|
| 8 | const edges = Array(2);
|
|---|
| 9 | if (leading) {
|
|---|
| 10 | edges[0] = 'leading';
|
|---|
| 11 | }
|
|---|
| 12 | if (trailing) {
|
|---|
| 13 | edges[1] = 'trailing';
|
|---|
| 14 | }
|
|---|
| 15 | let result = undefined;
|
|---|
| 16 | let pendingAt = null;
|
|---|
| 17 | const _debounced = debounce$1(function (...args) {
|
|---|
| 18 | result = func.apply(this, args);
|
|---|
| 19 | pendingAt = null;
|
|---|
| 20 | }, debounceMs, { edges });
|
|---|
| 21 | const debounced = function (...args) {
|
|---|
| 22 | if (maxWait != null) {
|
|---|
| 23 | if (pendingAt === null) {
|
|---|
| 24 | pendingAt = Date.now();
|
|---|
| 25 | }
|
|---|
| 26 | if (Date.now() - pendingAt >= maxWait) {
|
|---|
| 27 | result = func.apply(this, args);
|
|---|
| 28 | pendingAt = Date.now();
|
|---|
| 29 | _debounced.cancel();
|
|---|
| 30 | _debounced.schedule();
|
|---|
| 31 | return result;
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 | _debounced.apply(this, args);
|
|---|
| 35 | return result;
|
|---|
| 36 | };
|
|---|
| 37 | const flush = () => {
|
|---|
| 38 | _debounced.flush();
|
|---|
| 39 | return result;
|
|---|
| 40 | };
|
|---|
| 41 | debounced.cancel = _debounced.cancel;
|
|---|
| 42 | debounced.flush = flush;
|
|---|
| 43 | return debounced;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | export { debounce };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.