UNPKG

@slan-health/taro-vueuse

Version:

taro vue版本hooks

50 lines (49 loc) 1.7 kB
import { ref, computed, watchEffect } from "vue"; import { definePlugin } from "../definePlugin.js"; import { refToRaw, isNil } from "../utils/index.js"; import debounce from "../utils/lodash/debounce.js"; const useDebouncePlugin = definePlugin( (queryInstance, { debounceInterval, debounceOptions, manual }) => { const initialAutoRunFlag = ref(false); const debouncedRun = ref(); const debounceOptionsRef = computed(() => debounceOptions); const debounceIntervalRef = computed(() => refToRaw(debounceInterval)); const originRunRef = ref(queryInstance.context.runAsync); if (!manual) { initialAutoRunFlag.value = true; } watchEffect((onInvalidate) => { if (isNil(debounceIntervalRef.value)) return; debouncedRun.value = debounce( (callback) => callback(), debounceIntervalRef.value, debounceOptionsRef.value ); queryInstance.context.runAsync = (...args) => new Promise((resolve, reject) => { if (initialAutoRunFlag.value) { initialAutoRunFlag.value = false; originRunRef.value(...args).then(resolve).catch(reject); } else { debouncedRun.value(() => { originRunRef.value(...args).then(resolve).catch(reject); }); } }); onInvalidate(() => { var _a; (_a = debouncedRun.value) == null ? void 0 : _a.cancel(); queryInstance.context.runAsync = originRunRef.value; }); }); return { onCancel() { var _a; (_a = debouncedRun.value) == null ? void 0 : _a.cancel(); } }; } ); export { useDebouncePlugin as default }; //# sourceMappingURL=useDebouncePlugin.js.map