@@ -30,6 +30,8 @@ export class EventClient<
3030 #retryCount = 0
3131 #maxRetries = 5
3232 #connecting = false
33+ #failedToConnect = false
34+ #internalEventTarget: EventTarget | null = null
3335
3436 #onConnected = ( ) => {
3537 this . debugLog ( 'Connected to event bus' )
@@ -56,7 +58,7 @@ export class EventClient<
5658 'tanstack-connect' ,
5759 this . #retryConnection,
5860 )
59-
61+ this . #failedToConnect = true
6062 this . debugLog ( 'Max retries reached, giving up on connection' )
6163 this . stopConnectLoop ( )
6264 }
@@ -90,6 +92,7 @@ export class EventClient<
9092 this . debugLog ( ' Initializing event subscription for plugin' , this . #pluginId)
9193 this . #queuedEvents = [ ]
9294 this . #connected = false
95+ this . #failedToConnect = false
9396 this . #connectIntervalId = null
9497 this . #connectEveryMs = reconnectEveryMs
9598 }
@@ -107,11 +110,13 @@ export class EventClient<
107110
108111 private stopConnectLoop ( ) {
109112 this . #connecting = false
113+
110114 if ( this . #connectIntervalId === null ) {
111115 return
112116 }
113117 clearInterval ( this . #connectIntervalId)
114118 this . #connectIntervalId = null
119+ this . #queuedEvents = [ ]
115120 this . debugLog ( 'Stopped connect loop' )
116121 }
117122
@@ -189,6 +194,23 @@ export class EventClient<
189194 this . dispatchCustomEvent ( 'tanstack-dispatch-event' , event )
190195 }
191196
197+ createEventPayload <
198+ TSuffix extends Extract <
199+ keyof TEventMap ,
200+ `${TPluginId & string } :${string } `
201+ > extends `${TPluginId & string } :${infer S } `
202+ ? S
203+ : never ,
204+ > (
205+ eventSuffix : TSuffix ,
206+ payload : TEventMap [ `${TPluginId & string } :${TSuffix } `] ,
207+ ) {
208+ return {
209+ type : `${ this . #pluginId} :${ eventSuffix } ` ,
210+ payload,
211+ pluginId : this . #pluginId,
212+ }
213+ }
192214 emit <
193215 TSuffix extends Extract <
194216 keyof TEventMap ,
@@ -208,14 +230,27 @@ export class EventClient<
208230 )
209231 return
210232 }
233+ if ( this . #internalEventTarget) {
234+ this . debugLog (
235+ 'Emitting event to internal event target' ,
236+ eventSuffix ,
237+ payload ,
238+ )
239+ this . #internalEventTarget. dispatchEvent (
240+ new CustomEvent ( `${ this . #pluginId} :${ eventSuffix } ` , {
241+ detail : this . createEventPayload ( eventSuffix , payload ) ,
242+ } ) ,
243+ )
244+ }
245+
246+ if ( this . #failedToConnect) {
247+ this . debugLog ( 'Previously failed to connect, not emitting to bus' )
248+ return
249+ }
211250 // wait to connect to the bus
212251 if ( ! this . #connected) {
213252 this . debugLog ( 'Bus not available, will be pushed as soon as connected' )
214- this . #queuedEvents. push ( {
215- type : `${ this . #pluginId} :${ eventSuffix } ` ,
216- payload,
217- pluginId : this . #pluginId,
218- } )
253+ this . #queuedEvents. push ( this . createEventPayload ( eventSuffix , payload ) )
219254 // start connection to event bus
220255 if ( typeof CustomEvent !== 'undefined' && ! this . #connecting) {
221256 this . #connectFunction( )
@@ -224,11 +259,7 @@ export class EventClient<
224259 return
225260 }
226261 // emit right now
227- return this . emitEventToBus ( {
228- type : `${ this . #pluginId} :${ eventSuffix } ` ,
229- payload,
230- pluginId : this . #pluginId,
231- } )
262+ return this . emitEventToBus ( this . createEventPayload ( eventSuffix , payload ) )
232263 }
233264
234265 on <
@@ -246,8 +277,20 @@ export class EventClient<
246277 TEventMap [ `${TPluginId & string } :${TSuffix } `]
247278 > ,
248279 ) => void ,
280+ options ?: {
281+ withEventTarget ?: boolean
282+ } ,
249283 ) {
284+ const withEventTarget = options ?. withEventTarget ?? false
250285 const eventName = `${ this . #pluginId} :${ eventSuffix } ` as const
286+ if ( withEventTarget ) {
287+ if ( ! this . #internalEventTarget) {
288+ this . #internalEventTarget = new EventTarget ( )
289+ }
290+ this . #internalEventTarget. addEventListener ( eventName , ( e ) => {
291+ cb ( ( e as CustomEvent ) . detail )
292+ } )
293+ }
251294 if ( ! this . #enabled) {
252295 this . debugLog (
253296 'Event bus client is disabled, not registering event' ,
@@ -262,6 +305,9 @@ export class EventClient<
262305 this . #eventTarget( ) . addEventListener ( eventName , handler )
263306 this . debugLog ( 'Registered event to bus' , eventName )
264307 return ( ) => {
308+ if ( withEventTarget ) {
309+ this . #internalEventTarget?. removeEventListener ( eventName , handler )
310+ }
265311 this . #eventTarget( ) . removeEventListener ( eventName , handler )
266312 }
267313 }
0 commit comments