source: node_modules/es-toolkit/dist/compat/function/debounce.js

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.4 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const debounce$1 = require('../../function/debounce.js');
6
7function debounce(func, debounceMs = 0, options = {}) {
8 if (typeof options !== 'object') {
9 options = {};
10 }
11 const { leading = false, trailing = true, maxWait } = options;
12 const edges = Array(2);
13 if (leading) {
14 edges[0] = 'leading';
15 }
16 if (trailing) {
17 edges[1] = 'trailing';
18 }
19 let result = undefined;
20 let pendingAt = null;
21 const _debounced = debounce$1.debounce(function (...args) {
22 result = func.apply(this, args);
23 pendingAt = null;
24 }, debounceMs, { edges });
25 const debounced = function (...args) {
26 if (maxWait != null) {
27 if (pendingAt === null) {
28 pendingAt = Date.now();
29 }
30 if (Date.now() - pendingAt >= maxWait) {
31 result = func.apply(this, args);
32 pendingAt = Date.now();
33 _debounced.cancel();
34 _debounced.schedule();
35 return result;
36 }
37 }
38 _debounced.apply(this, args);
39 return result;
40 };
41 const flush = () => {
42 _debounced.flush();
43 return result;
44 };
45 debounced.cancel = _debounced.cancel;
46 debounced.flush = flush;
47 return debounced;
48}
49
50exports.debounce = debounce;
Note: See TracBrowser for help on using the repository browser.