index.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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 initVal = isObject(throttle) && Boolean(throttle.initVal);
  7. const throttled = ref(initVal);
  8. let timeoutHandle = null;
  9. const dispatchThrottling = (timer) => {
  10. if (isUndefined(timer)) {
  11. throttled.value = loading.value;
  12. return;
  13. }
  14. if (timeoutHandle) clearTimeout(timeoutHandle);
  15. timeoutHandle = setTimeout(() => {
  16. throttled.value = loading.value;
  17. }, timer);
  18. };
  19. const dispatcher = (type) => {
  20. if (type === "leading") {
  21. if (isNumber(throttle)) dispatchThrottling(throttle);
  22. else dispatchThrottling(throttle.leading);
  23. } else if (isObject(throttle)) dispatchThrottling(throttle.trailing);
  24. else throttled.value = false;
  25. };
  26. onMounted(() => dispatcher("leading"));
  27. watch(() => loading.value, (val) => {
  28. dispatcher(val ? "leading" : "trailing");
  29. });
  30. return throttled;
  31. };
  32. //#endregion
  33. export { useThrottleRender };
  34. //# sourceMappingURL=index.mjs.map