@@ -41,7 +41,9 @@ export function start(
41
41
// Would type this as "number" but that doesn't work for Node so ¯\_(ツ)_/¯
42
42
// TODO: find a way to exclude Node type definition for storage because storage only works in browser
43
43
// eslint-disable-next-line @typescript-eslint/no-explicit-any
44
- let timeoutId : any = null ;
44
+ let retryTimeoutId : any = null ;
45
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
+ let globalTimeoutId : any = null ;
45
47
let hitTimeout = false ;
46
48
let cancelState = 0 ;
47
49
@@ -58,22 +60,31 @@ export function start(
58
60
}
59
61
60
62
function callWithDelay ( millis : number ) : void {
61
- timeoutId = setTimeout ( ( ) => {
62
- timeoutId = null ;
63
+ retryTimeoutId = setTimeout ( ( ) => {
64
+ retryTimeoutId = null ;
63
65
f ( handler , canceled ( ) ) ;
64
66
} , millis ) ;
65
67
}
66
68
69
+ function clearGlobalTimeout ( ) : void {
70
+ if ( globalTimeoutId ) {
71
+ clearTimeout ( globalTimeoutId ) ;
72
+ }
73
+ }
74
+
67
75
function handler ( success : boolean , ...args : any [ ] ) : void {
68
76
if ( triggeredCallback ) {
77
+ clearGlobalTimeout ( ) ;
69
78
return ;
70
79
}
71
80
if ( success ) {
81
+ clearGlobalTimeout ( ) ;
72
82
triggerCallback . call ( null , success , ...args ) ;
73
83
return ;
74
84
}
75
85
const mustStop = canceled ( ) || hitTimeout ;
76
86
if ( mustStop ) {
87
+ clearGlobalTimeout ( ) ;
77
88
triggerCallback . call ( null , success , ...args ) ;
78
89
return ;
79
90
}
@@ -97,14 +108,15 @@ export function start(
97
108
return ;
98
109
}
99
110
stopped = true ;
111
+ clearGlobalTimeout ( ) ;
100
112
if ( triggeredCallback ) {
101
113
return ;
102
114
}
103
- if ( timeoutId !== null ) {
115
+ if ( retryTimeoutId !== null ) {
104
116
if ( ! wasTimeout ) {
105
117
cancelState = 2 ;
106
118
}
107
- clearTimeout ( timeoutId ) ;
119
+ clearTimeout ( retryTimeoutId ) ;
108
120
callWithDelay ( 0 ) ;
109
121
} else {
110
122
if ( ! wasTimeout ) {
@@ -113,7 +125,7 @@ export function start(
113
125
}
114
126
}
115
127
callWithDelay ( 0 ) ;
116
- setTimeout ( ( ) => {
128
+ globalTimeoutId = setTimeout ( ( ) => {
117
129
hitTimeout = true ;
118
130
stop ( true ) ;
119
131
} , timeout ) ;
0 commit comments