{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["export type TThrottledFn<T extends (...args: any[]) => any> = ((\n  ...args: Parameters<T>\n) => ReturnType<T> | undefined) & {\n  cancel: () => void;\n  flush: () => ReturnType<T> | undefined;\n  pending: () => boolean;\n};\n\nexport type TGetThrottledFnArgs = Parameters<typeof getThrottledFn>;\n\r\nexport type TGetThrottledFnReturn = ReturnType<typeof getThrottledFn>;\r\n\r\n/**\r\n * Gets a throttled function with specific delay\r\n * @template {(...args: any[]) => any} T\r\n * @param {T} func function\r\n * @param {number} [delay=1000] delay in ms, 1000 by default\r\n * @returns {TThrottledFn<T>} Throttled function with cancel, flush, and pending controls\n * @throws {TypeError} getThrottledFn: func must be a function\r\n * @throws {TypeError} getThrottledFn: delay must be a non-negative finite number\r\n * @example\r\n * // How to implement function throttling?\r\n * const getDataFromAPI = () => Promise.resolve([]);\r\n * const getThrottledDataFromAPI = getThrottledFn(getDataFromAPI, 3000);\n * getThrottledDataFromAPI(); // => []\n * @example\n * // Throttle scroll progress updates to avoid excessive layout work\n * const updateProgress = getThrottledFn(() => renderScrollProgress(), 100);\n * window.addEventListener(\"scroll\", updateProgress);\n * updateProgress.cancel();\n */\nexport const getThrottledFn = <T extends (...args: any[]) => any>(\n  func: T,\n  delay: number = 1000\n): TThrottledFn<T> => {\n  if (typeof func !== \"function\") {\r\n    throw new TypeError(\"getThrottledFn: func must be a function\");\r\n  }\r\n  if (typeof delay !== \"number\" || !Number.isFinite(delay) || delay < 0) {\r\n    throw new TypeError(\"getThrottledFn: delay must be a non-negative finite number\");\r\n  }\r\n\n  let timeout: ReturnType<typeof setTimeout> | null = null;\n  let lastArgs: Parameters<T> | null = null;\n  let lastContext: unknown;\n  let result: ReturnType<T> | undefined;\n  const invoke = (): ReturnType<T> | undefined => {\n    if (!lastArgs) {\n      return result;\n    }\n    const args = lastArgs;\n    const context = lastContext;\n    lastArgs = null;\n    lastContext = undefined;\n    result = func.apply(context, args) as ReturnType<T>;\n    return result;\n  };\n  const throttled = function throttledFunction(\n    this: unknown,\n    ...args: Parameters<T>\n  ): ReturnType<T> | undefined {\n    if (!timeout) {\n      lastArgs = args;\n      lastContext = this;\n      invoke();\n      timeout = setTimeout((): void => {\n        timeout = null;\n        lastArgs = null;\n        lastContext = undefined;\n      }, delay);\n    } else {\n      lastArgs = args;\n      lastContext = this;\n    }\n    return result;\n  } as TThrottledFn<T>;\n\n  throttled.cancel = (): void => {\n    if (timeout) {\n      clearTimeout(timeout);\n    }\n    timeout = null;\n    lastArgs = null;\n    lastContext = undefined;\n  };\n  throttled.flush = (): ReturnType<T> | undefined => {\n    if (timeout) {\n      clearTimeout(timeout);\n      timeout = null;\n      return invoke();\n    }\n    return result;\n  };\n  throttled.pending = (): boolean => timeout !== null;\n\n  return throttled;\n};\n"],"names":["getThrottledFn","func","delay","TypeError","Number","isFinite","timeout","lastArgs","lastContext","result","invoke","args","context","undefined","apply","throttled","throttledFunction","this","setTimeout","cancel","clearTimeout","flush","pending"],"mappings":"yDA+BO,MAAMA,eAAiB,CAC5BC,KACAC,MAAgB,QAEhB,UAAWD,OAAS,WAClB,MAAM,IAAIE,UAAU,2CAEtB,UAAWD,QAAU,WAAaE,OAAOC,SAASH,QAAUA,MAAQ,EAClE,MAAM,IAAIC,UAAU,8DAGtB,IAAIG,QAAgD,KACpD,IAAIC,SAAiC,KACrC,IAAIC,YACJ,IAAIC,OACJ,MAAMC,OAAS,KACb,IAAKH,SACH,OAAOE,OAET,MAAME,KAAOJ,SACb,MAAMK,QAAUJ,YAChBD,SAAW,KACXC,iBAAcK,EACdJ,OAASR,KAAKa,MAAMF,QAASD,MAC7B,OAAOF,QAET,MAAMM,UAAY,SAASC,qBAEtBL,MAEH,IAAKL,QAAS,CACZC,SAAWI,KACXH,YAAcS,KACdP,SACAJ,QAAUY,WAAW,KACnBZ,QAAU,KACVC,SAAW,KACXC,iBAAcK,GACbX,MACL,KAAO,CACLK,SAAWI,KACXH,YAAcS,IAChB,CACA,OAAOR,MACT,EAEAM,UAAUI,OAAS,KACjB,GAAIb,QACFc,aAAad,SAEfA,QAAU,KACVC,SAAW,KACXC,iBAAcK,GAEhBE,UAAUM,MAAQ,KAChB,GAAIf,QAAS,CACXc,aAAad,SACbA,QAAU,KACV,OAAOI,QACT,CACA,OAAOD,QAETM,UAAUO,QAAU,IAAehB,UAAY,KAE/C,OAAOS"}