index.mjs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { isNumber, isObject, isUndefined } from "../../utils/types.mjs";
  2. import { onMounted, ref, watch } from "vue";
  3. //#region ../../packages/hooks/use-throttle-render/index.ts
  4. const useThrottleRender = (loading, throttle = 0) => {
  5. if (throttle === 0) return loading;
  6. const throttled = ref(isObject(throttle) && Boolean(throttle.initVal));
  7. let timeoutHandle = null;
  8. const dispatchThrottling = (timer) => {
  9. if (isUndefined(timer)) {
  10. throttled.value = loading.value;
  11. return;
  12. }
  13. if (timeoutHandle) clearTimeout(timeoutHandle);
  14. timeoutHandle = setTimeout(() => {
  15. throttled.value = loading.value;
  16. }, timer);
  17. };
  18. const dispatcher = (type) => {
  19. if (type === "leading") if (isNumber(throttle)) dispatchThrottling(throttle);
  20. else dispatchThrottling(throttle.leading);
  21. else if (isObject(throttle)) dispatchThrottling(throttle.trailing);
  22. else throttled.value = false;
  23. };
  24. onMounted(() => dispatcher("leading"));
  25. watch(() => loading.value, (val) => {
  26. dispatcher(val ? "leading" : "trailing");
  27. });
  28. return throttled;
  29. };
  30. //#endregion
  31. export { useThrottleRender };
  32. //# sourceMappingURL=index.mjs.map