@web3r/flowerkit
Version:
Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).
1 lines • 2.9 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["export type TGetDebouncedFnArgs = Parameters<typeof getDebouncedFn>;\r\n\r\nexport type TGetDebouncedFnReturn = ReturnType<typeof getDebouncedFn>;\r\n\r\n/**\r\n * Returns a debounced version of a function that delays invoking `cb`\r\n * until after `wait` ms have elapsed since the last call.\r\n * Optionally invokes on the leading edge when `isImmediate` is true.\r\n *\r\n * @template {(...args: any[]) => any} T\r\n * @param {T} cb Function to debounce\r\n * @param {number} [wait=250] Delay in milliseconds\r\n * @param {boolean} [isImmediate=false] If `true`, invoke on the leading edge\r\n * @returns {(...args: Parameters<T>) => void}\r\n * @throws {TypeError} getDebouncedFn: cb must be a function\r\n * @throws {TypeError} getDebouncedFn: wait must be a non-negative finite number\r\n * @throws {TypeError} getDebouncedFn: isImmediate must be a boolean\r\n *\r\n * @example\r\n * const fn = getDebouncedFn((x: number) => console.log(x), 1000);\r\n * fn(1);\r\n */\r\nexport const getDebouncedFn = <T extends (...args: any[]) => any>(\r\n cb: T,\r\n wait: number = 250,\r\n isImmediate: boolean = false\r\n): ((...args: Parameters<T>) => void) => {\r\n if (typeof cb !== \"function\") {\r\n throw new TypeError(\"getDebouncedFn: cb must be a function\");\r\n }\r\n if (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0) {\r\n throw new TypeError(\"getDebouncedFn: wait must be a non-negative finite number\");\r\n }\r\n if (typeof isImmediate !== \"boolean\") {\r\n throw new TypeError(\"getDebouncedFn: isImmediate must be a boolean\");\r\n }\r\n\r\n let timeout: ReturnType<typeof setTimeout> | null = null;\r\n return function executedFunction(this: unknown, ...args: Parameters<T>): void {\r\n const context = this;\r\n const later = (): void => {\r\n timeout = null;\r\n if (!isImmediate) {\r\n cb.apply(context as any, args);\r\n }\r\n };\r\n const callNow = isImmediate && !timeout;\r\n if (timeout) {\r\n clearTimeout(timeout);\r\n }\r\n timeout = setTimeout(later, wait);\r\n if (callNow) {\r\n cb.apply(context as any, args);\r\n }\r\n };\r\n};\r\n"],"names":["getDebouncedFn","cb","wait","isImmediate","TypeError","Number","isFinite","timeout","executedFunction","args","context","this","later","apply","callNow","clearTimeout","setTimeout"],"mappings":";;;;;;;;;;;;;;;;;;GAsBO,MAAMA,eAAiBA,CAC5BC,GACAC,KAAe,IACfC,YAAuB,SAEvB,UAAWF,KAAO,WAChB,MAAM,IAAIG,UAAU,yCAEtB,UAAWF,OAAS,WAAaG,OAAOC,SAASJ,OAASA,KAAO,EAC/D,MAAM,IAAIE,UAAU,6DAEtB,UAAWD,cAAgB,UACzB,MAAM,IAAIC,UAAU,iDAGtB,IAAIG,QAAgD,KACpD,OAAO,SAASC,oBAAmCC,MACjD,MAAMC,QAAUC,KAChB,MAAMC,MAAQA,KACZL,QAAU,KACV,IAAKJ,YACHF,GAAGY,MAAMH,QAAgBD,OAG7B,MAAMK,QAAUX,cAAgBI,QAChC,GAAIA,QACFQ,aAAaR,SAEfA,QAAUS,WAAWJ,MAAOV,MAC5B,GAAIY,QACFb,GAAGY,MAAMH,QAAgBD,KAE7B"}