UNPKG

@slan-health/taro-vueuse

Version:

taro vue版本hooks

67 lines (66 loc) 2.07 kB
import { ref, computed } from "vue"; import { definePlugin } from "../definePlugin.js"; import { refToRaw } from "../utils/index.js"; const useErrorRetryPlugin = definePlugin( (queryInstance, { errorRetryCount = 0, errorRetryInterval = 0 }) => { const retryTimer = ref(); const retriedCount = ref(0); const errorRetryCountRef = computed(() => refToRaw(errorRetryCount)); const errorRetryIntervalRef = computed(() => refToRaw(errorRetryInterval)); let isRetrying = false; const resetRetriedCount = () => { retriedCount.value = 0; }; const actualErrorRetryInterval = computed(() => { if (errorRetryIntervalRef.value) return errorRetryIntervalRef.value; const baseTime = 1e3; const minCoefficient = 1; const maxCoefficient = 9; const coefficient = Math.floor( Math.random() * 2 ** Math.min(retriedCount.value, maxCoefficient) + minCoefficient ); return baseTime * coefficient; }); const errorRetryHooks = () => { let timerId; const isInfiniteRetry = errorRetryCountRef.value === -1; const hasRetryCount = retriedCount.value < errorRetryCountRef.value; if (isInfiniteRetry || hasRetryCount) { if (!isInfiniteRetry) retriedCount.value += 1; timerId = setTimeout(() => { isRetrying = true; queryInstance.context.refresh(); }, actualErrorRetryInterval.value); } return () => timerId && clearTimeout(timerId); }; const clearTimer = () => { if (retryTimer.value) { retryTimer.value(); } }; return { onBefore() { if (!isRetrying) { resetRetriedCount(); } isRetrying = false; clearTimer(); }, onSuccess() { resetRetriedCount(); }, onError() { retryTimer.value = errorRetryHooks(); }, onCancel() { resetRetriedCount(); clearTimer(); } }; } ); export { useErrorRetryPlugin as default }; //# sourceMappingURL=useErrorRetryPlugin.js.map