Skip to content

Commit

Permalink
fix: use-request polling error
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonYong committed Jun 7, 2024
1 parent 6fb4b90 commit 1bd1ba4
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions packages/hooks/src/useRequest/plugins/usePollingPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { unref, ref, watchEffect } from "vue";
import type { UseRequestPlugin, Interval } from "../types";
import type { UseRequestPlugin, Timeout } from "../types";
import isDocumentVisible from "../utils/isDocumentVisible";
import subscribeReVisible from "../utils/subscribeReVisible";

const usePollingPlugin: UseRequestPlugin<unknown, unknown[]> = (
fetchInstance,
{ pollingInterval, pollingWhenHidden = true, pollingErrorRetryCount = -1 }
) => {
const timerRef = ref<Interval>();
let timeouter:Timeout
const unsubscribeRef = ref<() => void>();
const countRef = ref<number>(0);



const stopPolling = () => {
if (timerRef.value) {
clearInterval(timerRef.value);
if (timeouter) {
clearTimeout(timeouter);
}
unsubscribeRef.value?.();
};
Expand Down Expand Up @@ -47,7 +47,7 @@ const usePollingPlugin: UseRequestPlugin<unknown, unknown[]> = (
// When an error occurs, the request is not repeated after pollingErrorRetryCount retries
(pollingErrorRetryCount !== -1 && countRef.value <= pollingErrorRetryCount)
) {
timerRef.value = setTimeout(() => {
timeouter = setTimeout(() => {
// if pollingWhenHidden = false && document is hidden, then stop polling and subscribe revisible
if (!pollingWhenHidden && !isDocumentVisible()) {
unsubscribeRef.value = subscribeReVisible(() => {
Expand All @@ -60,15 +60,6 @@ const usePollingPlugin: UseRequestPlugin<unknown, unknown[]> = (
} else {
countRef.value = 0;
}
// if (!pollingWhenHidden && !isDocumentVisible()) {
// unsubscribeRef.value = subscribeReVisible(() => {
// fetchInstance.refresh();
// });
// return;
// }
// timerRef.value = setInterval(() => {
// fetchInstance.refresh();
// },unref(pollingInterval));
},
onCancel: () => {
stopPolling();
Expand Down

0 comments on commit 1bd1ba4

Please sign in to comment.