1
1
import { unref , ref , watchEffect } from "vue" ;
2
- import type { UseRequestPlugin , Interval } from "../types" ;
2
+ import type { UseRequestPlugin , Timeout } from "../types" ;
3
3
import isDocumentVisible from "../utils/isDocumentVisible" ;
4
4
import subscribeReVisible from "../utils/subscribeReVisible" ;
5
5
6
6
const usePollingPlugin : UseRequestPlugin < unknown , unknown [ ] > = (
7
7
fetchInstance ,
8
8
{ pollingInterval, pollingWhenHidden = true , pollingErrorRetryCount = - 1 }
9
9
) => {
10
- const timerRef = ref < Interval > ( ) ;
10
+ let timeouter : Timeout
11
11
const unsubscribeRef = ref < ( ) => void > ( ) ;
12
12
const countRef = ref < number > ( 0 ) ;
13
13
14
14
15
15
16
16
const stopPolling = ( ) => {
17
- if ( timerRef . value ) {
18
- clearInterval ( timerRef . value ) ;
17
+ if ( timeouter ) {
18
+ clearTimeout ( timeouter ) ;
19
19
}
20
20
unsubscribeRef . value ?.( ) ;
21
21
} ;
@@ -47,7 +47,7 @@ const usePollingPlugin: UseRequestPlugin<unknown, unknown[]> = (
47
47
// When an error occurs, the request is not repeated after pollingErrorRetryCount retries
48
48
( pollingErrorRetryCount !== - 1 && countRef . value <= pollingErrorRetryCount )
49
49
) {
50
- timerRef . value = setTimeout ( ( ) => {
50
+ timeouter = setTimeout ( ( ) => {
51
51
// if pollingWhenHidden = false && document is hidden, then stop polling and subscribe revisible
52
52
if ( ! pollingWhenHidden && ! isDocumentVisible ( ) ) {
53
53
unsubscribeRef . value = subscribeReVisible ( ( ) => {
@@ -60,15 +60,6 @@ const usePollingPlugin: UseRequestPlugin<unknown, unknown[]> = (
60
60
} else {
61
61
countRef . value = 0 ;
62
62
}
63
- // if (!pollingWhenHidden && !isDocumentVisible()) {
64
- // unsubscribeRef.value = subscribeReVisible(() => {
65
- // fetchInstance.refresh();
66
- // });
67
- // return;
68
- // }
69
- // timerRef.value = setInterval(() => {
70
- // fetchInstance.refresh();
71
- // },unref(pollingInterval));
72
63
} ,
73
64
onCancel : ( ) => {
74
65
stopPolling ( ) ;
0 commit comments