@@ -29,9 +29,12 @@ export class EventClient<
2929 #connectEveryMs: number
3030 #retryCount = 0
3131 #maxRetries = 5
32+ #connecting = false
33+
3234 #onConnected = ( ) => {
3335 this . debugLog ( 'Connected to event bus' )
3436 this . #connected = true
37+ this . #connecting = false
3538 this . debugLog ( 'Emitting queued events' , this . #queuedEvents)
3639 this . #queuedEvents. forEach ( ( event ) => this . emitEventToBus ( event ) )
3740 this . #queuedEvents = [ ]
@@ -42,6 +45,9 @@ export class EventClient<
4245 )
4346 }
4447 #connectFunction = ( ) => {
48+ if ( this . #connecting) return
49+
50+ this . #connecting = true
4551 this . #eventTarget( ) . addEventListener (
4652 'tanstack-connect-success' ,
4753 this . #onConnected,
@@ -56,6 +62,7 @@ export class EventClient<
5662 'tanstack-connect' ,
5763 this . #connectFunction,
5864 )
65+
5966 this . debugLog ( 'Max retries reached, giving up on connection' )
6067 this . stopConnectLoop ( )
6168 }
@@ -93,6 +100,7 @@ export class EventClient<
93100 }
94101
95102 private stopConnectLoop ( ) {
103+ this . #connecting = false
96104 if ( this . #connectIntervalId === null ) {
97105 return
98106 }
@@ -203,7 +211,7 @@ export class EventClient<
203211 pluginId : this . #pluginId,
204212 } )
205213 // start connection to event bus
206- if ( typeof CustomEvent !== 'undefined' ) {
214+ if ( typeof CustomEvent !== 'undefined' && ! this . #connecting ) {
207215 this . #connectFunction( )
208216 this . startConnectLoop ( )
209217 }
0 commit comments