UNPKG

@slan-health/taro-vueuse

Version:

taro vue版本hooks

41 lines (40 loc) 1.4 kB
import { ref, computed, watchEffect } from "vue"; import { definePlugin } from "../definePlugin.js"; import { refToRaw, isNil } from "../utils/index.js"; import throttle from "../utils/lodash/throttle.js"; const useThrottlePlugin = definePlugin( (queryInstance, { throttleInterval, throttleOptions }) => { const throttledRun = ref(); const throttleIntervalRef = computed(() => refToRaw(throttleInterval)); const throttleOptionsRef = computed(() => throttleOptions); const originRunRef = ref(queryInstance.context.runAsync); watchEffect((onInvalidate) => { if (isNil(throttleInterval)) return {}; throttledRun.value = throttle( (callback) => callback(), throttleIntervalRef.value, throttleOptionsRef.value ); queryInstance.context.runAsync = (...args) => new Promise((resolve, reject) => { throttledRun.value(() => { originRunRef.value(...args).then(resolve).catch(reject); }); }); onInvalidate(() => { var _a; (_a = throttledRun.value) == null ? void 0 : _a.cancel(); queryInstance.context.runAsync = originRunRef.value; }); }); return { onCancel() { var _a; (_a = throttledRun.value) == null ? void 0 : _a.cancel(); } }; } ); export { useThrottlePlugin as default }; //# sourceMappingURL=useThrottlePlugin.js.map