{"version":3,"file":"debounce-DlDzKYqG.cjs","names":[],"sources":["../src/functions/debounce/debounce.ts"],"sourcesContent":["import type { AnyFunction } from '../../types/_internal';\n\n/**\n * Creates a debounced function that delays invoking `func` until after `delay` milliseconds have elapsed since the last time the debounced function was invoked.\n * @param func The function to debounce.\n * @param delay The number of milliseconds to delay.\n * @param options Optional configuration options.\n * @param options.leading Whether to call the function on the leading edge of the timeout.\n * @returns A debounced function that can be called multiple times, but only invokes `func` once per `delay` milliseconds.\n */\n// biome-ignore lint/suspicious/noConfusingVoidType: this is not a confusing void location, it defines the return type of the `TFunction`\nexport function debounce<TFunction extends AnyFunction<void | undefined>>(\n  func: TFunction,\n  delay: number,\n  options: {\n    /**\n     * Whether to call the function on the leading edge of the timeout.\n     * @default false\n     */\n    leading?: boolean;\n  } = {}\n): DebouncedFunction<TFunction> {\n  let timeoutId: ReturnType<typeof setTimeout> | null = null;\n  let isCalled = false;\n  let latestArguments: Parameters<TFunction>;\n\n  function debouncedFunction(...args: Parameters<TFunction>): void {\n    latestArguments = args;\n\n    if (options.leading && !isCalled) {\n      func(...args);\n      isCalled = true;\n    }\n\n    if (timeoutId !== null) {\n      clearTimeout(timeoutId);\n    }\n\n    timeoutId = setTimeout(() => {\n      if (!options.leading) {\n        func(...args);\n      }\n      isCalled = false;\n    }, delay);\n  }\n\n  debouncedFunction.flush = () => {\n    if (timeoutId !== null) {\n      clearTimeout(timeoutId);\n    }\n\n    func(...latestArguments);\n  };\n\n  debouncedFunction.clear = () => {\n    if (timeoutId !== null) {\n      clearTimeout(timeoutId);\n    }\n  };\n\n  return debouncedFunction;\n}\n\n// biome-ignore lint/suspicious/noConfusingVoidType: this is not a confusing void location, it defines the return type of the `TFunction`\ninterface DebouncedFunction<TFunction extends AnyFunction<void | undefined>> {\n  (...args: Parameters<TFunction>): void;\n  clear(): void;\n  flush(): void;\n}\n"],"mappings":";;;;;;;;;;AAWA,SAAgB,SACd,MACA,OACA,UAMI,EAAE,EACwB;CAC9B,IAAI,YAAkD;CACtD,IAAI,WAAW;CACf,IAAI;CAEJ,SAAS,kBAAkB,GAAG,MAAmC;AAC/D,oBAAkB;AAElB,MAAI,QAAQ,WAAW,CAAC,UAAU;AAChC,QAAK,GAAG,KAAK;AACb,cAAW;;AAGb,MAAI,cAAc,KAChB,cAAa,UAAU;AAGzB,cAAY,iBAAiB;AAC3B,OAAI,CAAC,QAAQ,QACX,MAAK,GAAG,KAAK;AAEf,cAAW;KACV,MAAM;;AAGX,mBAAkB,cAAc;AAC9B,MAAI,cAAc,KAChB,cAAa,UAAU;AAGzB,OAAK,GAAG,gBAAgB;;AAG1B,mBAAkB,cAAc;AAC9B,MAAI,cAAc,KAChB,cAAa,UAAU;;AAI3B,QAAO"}