{"version":3,"file":"useThrottle.mjs","sources":["../../../../src/composables/useThrottle.ts"],"sourcesContent":["/**\n * @description returns throttled function or value\n * the last one always returns last-call value in the end if no more new calls were provided\n * @example\n *    import { useThrottleFunction, useThrottleValue } from '../../composables'\n *    ...\n *    const localThrottledFunction = useThrottleFunction(functionToThrottle, props)\n *    const localThrottledValue = useThrottleValue(reactiveValueToThrottle, props)\n */\n\nimport {\n  ref, toRef, unref,\n  watch,\n  Ref, ExtractPropTypes, ComponentInternalInstance,\n} from 'vue'\n\ntype ThrottledFunctionArgs = any[]\ntype ThrottledFunction<Output> = (...args: ThrottledFunctionArgs) => Output\n\nexport const useThrottleProps = {\n  delay: {\n    type: Number,\n    default: 0,\n    validator: (value: number) => value >= 0,\n  },\n}\n\ntype UseThrottleProps = ExtractPropTypes<typeof useThrottleProps>\n\n/**\n * @param fn passed function to throttle\n * @param props { delay } call delay in ms\n */\nexport function useThrottleFunction<Output> (fn: ThrottledFunction<Output>, props: UseThrottleProps) {\n  const delay = toRef(props, 'delay') ?? 0\n\n  const isThrottled = ref(true)\n\n  /**\n   * No way this will be returned without reassignment, so we don't want typescript\n   * to always keep undefined as possible return type. If function returns undefined itself\n   * it will be still presented by typescript as undefined (expected behaviour).\n   */\n  let lastCallResult = undefined as any as Output\n\n  return function (this: any, ...args: ThrottledFunctionArgs) {\n    const invoke = () => fn.apply(this, args)\n\n    if (!unref(delay)) { return invoke() }\n\n    if (isThrottled.value) {\n      isThrottled.value = false\n\n      setTimeout(() => (isThrottled.value = true), unref(delay))\n\n      lastCallResult = invoke()\n    }\n\n    return lastCallResult\n  }\n}\n\n/**\n * @param value passed reactive value to throttle\n * @param props { delay } call delay in ms\n */\nexport function useThrottleValue<T> (value: Ref<T>, props: UseThrottleProps): Ref<T> {\n  const delay = toRef(props, 'delay') ?? 0\n\n  if (!unref(delay)) { return value }\n\n  const isThrottled = ref(true)\n\n  const previousCallValue = ref<T>() as Ref<T>\n  const previousReturnedValue = ref<T>() as Ref<T>\n  const currentCallValue = ref<T>() as Ref<T>\n\n  watch(value, () => {\n    // we save and return last call value at the end if no more calls were provided\n    previousCallValue.value = value.value\n    const lastCallValue = setTimeout(() => {\n      currentCallValue.value = previousCallValue.value\n    }, unref(delay))\n\n    if (isThrottled.value) {\n      isThrottled.value = false\n\n      currentCallValue.value = value.value\n      previousReturnedValue.value = value.value\n\n      // no need to return previously saved value anymore\n      clearTimeout(lastCallValue)\n\n      setTimeout(() => (isThrottled.value = true), unref(delay))\n    } else {\n      currentCallValue.value = previousReturnedValue.value\n    }\n  }, { immediate: true })\n\n  return currentCallValue\n}\n"],"names":[],"mappings":";AAmBO,MAAM,mBAAmB;AAAA,EAC9B,OAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IACT,WAAW,CAAC,UAAkB,SAAS;AAAA,EACzC;AACF;AAQgB,SAAA,oBAA6B,IAA+B,OAAyB;AACnG,QAAM,QAAQ,MAAM,OAAO,OAAO,KAAK;AAEjC,QAAA,cAAc,IAAI,IAAI;AAO5B,MAAI,iBAAiB;AAErB,SAAO,YAAwB,MAA6B;AAC1D,UAAM,SAAS,MAAM,GAAG,MAAM,MAAM,IAAI;AAEpC,QAAA,CAAC,MAAM,KAAK,GAAG;AAAE,aAAO,OAAO;AAAA,IAAE;AAErC,QAAI,YAAY,OAAO;AACrB,kBAAY,QAAQ;AAEpB,iBAAW,MAAO,YAAY,QAAQ,MAAO,MAAM,KAAK,CAAC;AAEzD,uBAAiB,OAAO;AAAA,IAC1B;AAEO,WAAA;AAAA,EAAA;AAEX;AAMgB,SAAA,iBAAqB,OAAe,OAAiC;AACnF,QAAM,QAAQ,MAAM,OAAO,OAAO,KAAK;AAEnC,MAAA,CAAC,MAAM,KAAK,GAAG;AAAS,WAAA;AAAA,EAAM;AAE5B,QAAA,cAAc,IAAI,IAAI;AAE5B,QAAM,oBAAoB;AAC1B,QAAM,wBAAwB;AAC9B,QAAM,mBAAmB;AAEzB,QAAM,OAAO,MAAM;AAEjB,sBA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