{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../packages/hooks/use-throttle-render/index.ts"],"sourcesContent":["import { onMounted, ref, watch } from 'vue'\nimport { isNumber, isObject, isUndefined } from '@element-plus/utils'\n\nimport type { Ref } from 'vue'\n\nexport type ThrottleType =\n  { leading?: number; trailing?: number; initVal?: boolean } | number\n\nexport const useThrottleRender = (\n  loading: Ref<boolean>,\n  throttle: ThrottleType = 0\n) => {\n  if (throttle === 0) return loading\n  const initVal = isObject(throttle) && Boolean(throttle.initVal)\n  const throttled = ref(initVal)\n  let timeoutHandle: ReturnType<typeof setTimeout> | null = null\n\n  const dispatchThrottling = (timer: number | undefined) => {\n    if (isUndefined(timer)) {\n      throttled.value = loading.value\n      return\n    }\n    if (timeoutHandle) {\n      clearTimeout(timeoutHandle)\n    }\n    timeoutHandle = setTimeout(() => {\n      throttled.value = loading.value\n    }, timer)\n  }\n\n  const dispatcher = (type: 'leading' | 'trailing') => {\n    if (type === 'leading') {\n      if (isNumber(throttle)) {\n        dispatchThrottling(throttle)\n      } else {\n        dispatchThrottling(throttle.leading)\n      }\n    } else {\n      if (isObject(throttle)) {\n        dispatchThrottling(throttle.trailing)\n      } else {\n        throttled.value = false\n      }\n    }\n  }\n\n  onMounted(() => dispatcher('leading'))\n\n  watch(\n    () => loading.value,\n    (val) => {\n      dispatcher(val ? 'leading' : 'trailing')\n    }\n  )\n\n  return throttled\n}\n"],"mappings":";;;AAQA,MAAa,qBACX,SACA,WAAyB,MACtB;CACH,IAAI,aAAa,GAAG,OAAO;CAC3B,MAAM,UAAU,SAAS,QAAQ,KAAK,QAAQ,SAAS,OAAO;CAC9D,MAAM,YAAY,IAAI,OAAO;CAC7B,IAAI,gBAAsD;CAE1D,MAAM,sBAAsB,UAA8B;EACxD,IAAI,YAAY,KAAK,GAAG;GACtB,UAAU,QAAQ,QAAQ;GAC1B;EACF;EACA,IAAI,eACF,aAAa,aAAa;EAE5B,gBAAgB,iBAAiB;GAC/B,UAAU,QAAQ,QAAQ;EAC5B,GAAG,KAAK;CACV;CAEA,MAAM,cAAc,SAAiC;EACnD,IAAI,SAAS,WAAW;GACtB,IAAI,SAAS,QAAQ,GACnB,mBAAmB,QAAQ;QAE3B,mBAAmB,SAAS,OAAO;EAEvC,OACE,IAAI,SAAS,QAAQ,GACnB,mBAAmB,SAAS,QAAQ;OAEpC,UAAU,QAAQ;CAGxB;CAEA,gBAAgB,WAAW,SAAS,CAAC;CAErC,YACQ,QAAQ,QACb,QAAQ;EACP,WAAW,MAAM,YAAY,UAAU;CACzC,CACF;CAEA,OAAO;AACT"}