debounce-toolkit
Version:
A debounce utility with multiple execution modes
3 lines (2 loc) • 1.12 kB
JavaScript
var e;function o(e,o,n){var t;o=null!=o?o:0;const r="string"==typeof n?n:null!==(t=null==n?void 0:n.mode)&&void 0!==t?t:exports.DebounceMode.TRAILING;if("function"!=typeof e)throw new TypeError("Expected fn to be a function");if("number"!=typeof o||o<0)throw new TypeError("Expected wait to be a non-negative number");if(!Object.values(exports.DebounceMode).includes(r))throw new TypeError("Invalid debounce mode");let u=null,l=null,p=null,c=!1;const d=()=>{u&&(clearTimeout(u),u=null)},i=function(...n){l=n,p=this;const t=(r===exports.DebounceMode.LEADING||r===exports.DebounceMode.BOTH)&&!c,i=r===exports.DebounceMode.TRAILING||r===exports.DebounceMode.BOTH;d(),t&&(e.apply(p,l),c=!0),u=setTimeout(()=>{!i||t&&!c||e.apply(p,l),u=null,c=!1},o)};return i.cancel=()=>{d(),c=!1},i.flush=()=>{u&&l&&(d(),e.apply(p,l),c=!1)},i.pending=()=>null!==u,i}Object.defineProperty(exports,"__esModule",{value:!0}),exports.DebounceMode=void 0,(e=exports.DebounceMode||(exports.DebounceMode={})).TRAILING="trailing",e.LEADING="leading",e.BOTH="both",exports.debounce=o,exports.default=o;
//# sourceMappingURL=index.cjs.js.map
;