{"version":3,"sources":["../../src/utils/debounce.ts"],"sourcesContent":["import type { AnyFunc } from './basic'\n\nexport type DebounceOptions = {\n  /**\n   * The wait time in milliseconds before the debounced function can be invoked again.\n   *\n   * @defaultValue 0 milliseconds\n   */\n  wait?: number\n  /**\n   * Determines whether the function should be invoked immediately at the leading edge of the timeout.\n   *\n   * @defaultValue false\n   */\n  leading?: boolean\n  /**\n   * Determines whether the function should be invoked after the wait time as passed at the trailing edge.\n   *\n   * @defaultValue true\n   */\n  trailing?: boolean\n  /**\n   * The maximum time in milliseconds the function can be delayed before it's invoked.\n   *\n   * @defaultValue 0 (no maximum delay)\n   *\n   * @since 1.10.0\n   */\n  maxWait?: number\n}\n\nexport type DebouncedFn<T extends AnyFunc> = {\n  /**\n   * The debounced function.\n   */\n  (this: ThisParameterType<T>, ...args: Parameters<T>): ReturnType<T> | undefined\n  /**\n   * Cancels the debounced function.\n   *\n   * @deprecated Use `debounced.cancel()` instead.\n   */\n  clear: () => void\n  /**\n   * Cancels the debounced function.\n   */\n  cancel: () => void\n  /**\n   * Invokes the debounced function immediately.\n   */\n  flush: () => ReturnType<T> | undefined\n}\n\n/**\n * Creates a debounced function that delays invoking the provided function until after `wait` milliseconds have elapsed since the last time the debounced function was invoked.\n *\n * @from lodash/debounce\n */\nexport function debounce<T extends AnyFunc>(fn: T, options: DebounceOptions = {}): DebouncedFn<T> {\n  const { wait = 0, maxWait = 0, leading = false, trailing = true } = options\n\n  let lastArgs: Parameters<T> | undefined\n  let lastThis: ThisParameterType<T> | undefined\n  let result: ReturnType<T> | undefined\n  let timerId: ReturnType<typeof setTimeout> | undefined\n  let lastCallTime: number | undefined\n  let lastInvokeTime = 0\n\n  const maxing = maxWait > 0\n\n  function invokeFunc(time: number) {\n    const args = lastArgs\n    const thisArg = lastThis\n    lastArgs = lastThis = undefined\n    lastInvokeTime = time\n    result = fn.apply(thisArg, args as any)\n    return result\n  }\n\n  function leadingEdge(time: number) {\n    lastInvokeTime = time\n    timerId = setTimeout(timerExpired, wait)\n    return leading ? invokeFunc(time) : result\n  }\n\n  /* v8 ignore next 6 */\n  function remainingWait(time: number) {\n    const timeSinceLastCall = time - (lastCallTime ?? 0)\n    const timeSinceLastInvoke = time - lastInvokeTime\n    const timeWaiting = wait - timeSinceLastCall\n    return maxing ? Math.min(timeWaiting, maxWait - timeSinceLastInvoke) : timeWaiting\n  }\n\n  function shouldInvoke(time: number) {\n    const timeSinceLastCall = time - (lastCallTime ?? 0)\n    const timeSinceLastInvoke = time - lastInvokeTime\n    return (\n      lastCallTime === undefined ||\n      timeSinceLastCall >= wait ||\n      timeSinceLastCall < 0 ||\n      (maxing && timeSinceLastInvoke >= maxWait)\n    )\n  }\n\n  function timerExpired() {\n    const time = Date.now()\n    if (shouldInvoke(time)) {\n      return trailingEdge(time)\n    }\n    timerId = setTimeout(timerExpired, remainingWait(time))\n  }\n\n  function trailingEdge(time: number) {\n    timerId = undefined\n    if (trailing && lastArgs) {\n      return invokeFunc(time)\n    }\n    lastArgs = lastThis = undefined\n    return result\n  }\n\n  function cancel() {\n    if (timerId !== undefined) {\n      clearTimeout(timerId)\n    }\n    lastInvokeTime = 0\n    lastArgs = lastCallTime = lastThis = timerId = undefined\n  }\n\n  /* v8 ignore next 3 */\n  function flush() {\n    return timerId === undefined ? result : trailingEdge(Date.now())\n  }\n\n  function debounced(this: ThisParameterType<T>, ...args: Parameters<T>): ReturnType<T> | undefined {\n    const time = Date.now()\n    const isInvoking = shouldInvoke(time)\n\n    lastArgs = args\n    // eslint-disable-next-line no-this-alias\n    lastThis = this\n    lastCallTime = time\n\n    if (isInvoking) {\n      if (timerId === undefined) {\n        return leadingEdge(lastCallTime)\n      }\n      /* v8 ignore next 5 */\n      if (maxing) {\n        clearTimeout(timerId)\n        timerId = setTimeout(timerExpired, wait)\n        return invokeFunc(lastCallTime)\n      }\n    }\n    if (timerId === undefined) {\n      timerId = setTimeout(timerExpired, wait)\n    }\n    return result\n  }\n\n  debounced.cancel = cancel\n  debounced.flush = flush\n\n  debounced.clear = cancel\n\n  return debounced\n}\n"],"names":["debounce","fn","options","wait","maxWait","leading","trailing","lastArgs","lastThis","result","timerId","lastCallTime","lastInvokeTime","maxing","invokeFunc","time","args","thisArg","undefined","apply","leadingEdge","setTimeout","timerExpired","remainingWait","timeSinceLastCall","timeSinceLastInvoke","timeWaiting","Math","min","shouldInvoke","Date","now","trailingEdge","cancel","clearTimeout","flush","debounced","isInvoking","clear"],"mappings":";;;;+BAyDgBA;;;eAAAA;;;AAAT,SAASA,SAA4BC,EAAK;QAAEC,UAAAA,iEAA2B,CAAC;IAC7E,oBAAoEA,QAA5DC,MAAAA,kCAAO,sCAAqDD,QAAlDE,SAAAA,wCAAU,yCAAwCF,QAArCG,SAAAA,wCAAU,8CAA2BH,QAApBI,UAAAA,0CAAW;IAE3D,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC;IACJ,IAAIC,iBAAiB;IAErB,IAAMC,SAAST,UAAU;IAEzB,SAASU,WAAWC,IAAY;QAC9B,IAAMC,OAAOT;QACb,IAAMU,UAAUT;QAChBD,WAAWC,WAAWU;QACtBN,iBAAiBG;QACjBN,SAASR,GAAGkB,KAAK,CAACF,SAASD;QAC3B,OAAOP;IACT;IAEA,SAASW,YAAYL,IAAY;QAC/BH,iBAAiBG;QACjBL,UAAUW,WAAWC,cAAcnB;QACnC,OAAOE,UAAUS,WAAWC,QAAQN;IACtC;IAEA,oBAAoB,GACpB,SAASc,cAAcR,IAAY;QACjC,IAAMS,oBAAoBT,OAAQJ,CAAAA,yBAAAA,0BAAAA,eAAgB,CAAA;QAClD,IAAMc,sBAAsBV,OAAOH;QACnC,IAAMc,cAAcvB,OAAOqB;QAC3B,OAAOX,SAASc,KAAKC,GAAG,CAACF,aAAatB,UAAUqB,uBAAuBC;IACzE;IAEA,SAASG,aAAad,IAAY;QAChC,IAAMS,oBAAoBT,OAAQJ,CAAAA,yBAAAA,0BAAAA,eAAgB,CAAA;QAClD,IA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