You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

1 month ago
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var types = require('../../utils/types.js');
  5. var shared = require('@vue/shared');
  6. const useThrottleRender = (loading, throttle = 0) => {
  7. if (throttle === 0)
  8. return loading;
  9. const initVal = shared.isObject(throttle) && Boolean(throttle.initVal);
  10. const throttled = vue.ref(initVal);
  11. let timeoutHandle = null;
  12. const dispatchThrottling = (timer) => {
  13. if (types.isUndefined(timer)) {
  14. throttled.value = loading.value;
  15. return;
  16. }
  17. if (timeoutHandle) {
  18. clearTimeout(timeoutHandle);
  19. }
  20. timeoutHandle = setTimeout(() => {
  21. throttled.value = loading.value;
  22. }, timer);
  23. };
  24. const dispatcher = (type) => {
  25. if (type === "leading") {
  26. if (types.isNumber(throttle)) {
  27. dispatchThrottling(throttle);
  28. } else {
  29. dispatchThrottling(throttle.leading);
  30. }
  31. } else {
  32. if (shared.isObject(throttle)) {
  33. dispatchThrottling(throttle.trailing);
  34. } else {
  35. throttled.value = false;
  36. }
  37. }
  38. };
  39. vue.onMounted(() => dispatcher("leading"));
  40. vue.watch(() => loading.value, (val) => {
  41. dispatcher(val ? "leading" : "trailing");
  42. });
  43. return throttled;
  44. };
  45. exports.useThrottleRender = useThrottleRender;
  46. //# sourceMappingURL=index.js.map