@@ -41,7 +41,9 @@ export function start(
4141 // Would type this as "number" but that doesn't work for Node so ¯\_(ツ)_/¯
4242 // TODO: find a way to exclude Node type definition for storage because storage only works in browser
4343 // 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 ;
4547 let hitTimeout = false ;
4648 let cancelState = 0 ;
4749
@@ -58,22 +60,31 @@ export function start(
5860 }
5961
6062 function callWithDelay ( millis : number ) : void {
61- timeoutId = setTimeout ( ( ) => {
62- timeoutId = null ;
63+ retryTimeoutId = setTimeout ( ( ) => {
64+ retryTimeoutId = null ;
6365 f ( handler , canceled ( ) ) ;
6466 } , millis ) ;
6567 }
6668
69+ function clearGlobalTimeout ( ) : void {
70+ if ( globalTimeoutId ) {
71+ clearTimeout ( globalTimeoutId ) ;
72+ }
73+ }
74+
6775 function handler ( success : boolean , ...args : any [ ] ) : void {
6876 if ( triggeredCallback ) {
77+ clearGlobalTimeout ( ) ;
6978 return ;
7079 }
7180 if ( success ) {
81+ clearGlobalTimeout ( ) ;
7282 triggerCallback . call ( null , success , ...args ) ;
7383 return ;
7484 }
7585 const mustStop = canceled ( ) || hitTimeout ;
7686 if ( mustStop ) {
87+ clearGlobalTimeout ( ) ;
7788 triggerCallback . call ( null , success , ...args ) ;
7889 return ;
7990 }
@@ -97,14 +108,15 @@ export function start(
97108 return ;
98109 }
99110 stopped = true ;
111+ clearGlobalTimeout ( ) ;
100112 if ( triggeredCallback ) {
101113 return ;
102114 }
103- if ( timeoutId !== null ) {
115+ if ( retryTimeoutId !== null ) {
104116 if ( ! wasTimeout ) {
105117 cancelState = 2 ;
106118 }
107- clearTimeout ( timeoutId ) ;
119+ clearTimeout ( retryTimeoutId ) ;
108120 callWithDelay ( 0 ) ;
109121 } else {
110122 if ( ! wasTimeout ) {
@@ -113,7 +125,7 @@ export function start(
113125 }
114126 }
115127 callWithDelay ( 0 ) ;
116- setTimeout ( ( ) => {
128+ globalTimeoutId = setTimeout ( ( ) => {
117129 hitTimeout = true ;
118130 stop ( true ) ;
119131 } , timeout ) ;
0 commit comments