UNPKG

@carbon/react

Version:

React components for the Carbon Design System

60 lines (54 loc) 1.66 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var debounce$1 = require('../../function/debounce.mjs.js'); function debounce(func, debounceMs = 0, options = {}) { if (typeof options !== 'object') { options = {}; } const { signal, leading = false, trailing = true, maxWait } = options; const edges = Array(2); if (leading) { edges[0] = 'leading'; } if (trailing) { edges[1] = 'trailing'; } let result = undefined; let pendingAt = null; const _debounced = debounce$1.debounce(function (...args) { result = func.apply(this, args); pendingAt = null; }, debounceMs, { signal, edges }); const debounced = function (...args) { if (maxWait != null) { if (pendingAt === null) { pendingAt = Date.now(); } else { if (Date.now() - pendingAt >= maxWait) { result = func.apply(this, args); pendingAt = Date.now(); _debounced.cancel(); _debounced.schedule(); return result; } } } _debounced.apply(this, args); return result; }; const flush = () => { _debounced.flush(); return result; }; debounced.cancel = _debounced.cancel; debounced.flush = flush; return debounced; } exports.debounce = debounce;