vue-hooks-plus
Version:
Vue hooks library
46 lines (45 loc) • 1.12 kB
JavaScript
import { ref } from "vue";
const useRetryPlugin = (fetchInstance, { retryInterval, retryCount }) => {
const timerRef = ref();
const countRef = ref(0);
const triggerByRetry = ref(false);
if (!retryCount) {
return {};
}
return {
name: "retryPlugin",
onBefore: () => {
if (!triggerByRetry.value) {
countRef.value = 0;
}
triggerByRetry.value = false;
if (timerRef.value) {
clearTimeout(timerRef.value);
}
},
onSuccess: () => {
countRef.value = 0;
},
onError: () => {
countRef.value += 1;
if (retryCount === -1 || countRef.value <= retryCount) {
const timeout = retryInterval != null ? retryInterval : Math.min(1e3 * 2 ** countRef.value, 3e4);
timerRef.value = setTimeout(() => {
triggerByRetry.value = true;
fetchInstance.refresh();
}, timeout);
} else {
countRef.value = 0;
}
},
onCancel: () => {
countRef.value = 0;
if (timerRef.value) {
clearTimeout(timerRef.value);
}
}
};
};
export {
useRetryPlugin as default
};