@@ -30,6 +30,7 @@ export class EventClient<
3030 #retryCount = 0
3131 #maxRetries = 5
3232 #connecting = false
33+ #internalEventTarget: EventTarget | null = null
3334
3435 #onConnected = ( ) => {
3536 this . debugLog ( 'Connected to event bus' )
@@ -112,6 +113,7 @@ export class EventClient<
112113 }
113114 clearInterval ( this . #connectIntervalId)
114115 this . #connectIntervalId = null
116+ this . #queuedEvents = [ ]
115117 this . debugLog ( 'Stopped connect loop' )
116118 }
117119
@@ -189,6 +191,23 @@ export class EventClient<
189191 this . dispatchCustomEvent ( 'tanstack-dispatch-event' , event )
190192 }
191193
194+ createEventPayload <
195+ TSuffix extends Extract <
196+ keyof TEventMap ,
197+ `${TPluginId & string } :${string } `
198+ > extends `${TPluginId & string } :${infer S } `
199+ ? S
200+ : never ,
201+ > (
202+ eventSuffix : TSuffix ,
203+ payload : TEventMap [ `${TPluginId & string } :${TSuffix } `] ,
204+ ) {
205+ return {
206+ type : `${ this . #pluginId} :${ eventSuffix } ` ,
207+ payload,
208+ pluginId : this . #pluginId,
209+ }
210+ }
192211 emit <
193212 TSuffix extends Extract <
194213 keyof TEventMap ,
@@ -200,6 +219,18 @@ export class EventClient<
200219 eventSuffix : TSuffix ,
201220 payload : TEventMap [ `${TPluginId & string } :${TSuffix } `] ,
202221 ) {
222+ if ( this . #internalEventTarget) {
223+ this . debugLog (
224+ 'Emitting event to internal event target' ,
225+ eventSuffix ,
226+ payload ,
227+ )
228+ this . #internalEventTarget. dispatchEvent (
229+ new CustomEvent ( `${ this . #pluginId} :${ eventSuffix } ` , {
230+ detail : this . createEventPayload ( eventSuffix , payload ) ,
231+ } ) ,
232+ )
233+ }
203234 if ( ! this . #enabled) {
204235 this . debugLog (
205236 'Event bus client is disabled, not emitting event' ,
@@ -211,11 +242,7 @@ export class EventClient<
211242 // wait to connect to the bus
212243 if ( ! this . #connected) {
213244 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- } )
245+ this . #queuedEvents. push ( this . createEventPayload ( eventSuffix , payload ) )
219246 // start connection to event bus
220247 if ( typeof CustomEvent !== 'undefined' && ! this . #connecting) {
221248 this . #connectFunction( )
@@ -224,11 +251,7 @@ export class EventClient<
224251 return
225252 }
226253 // emit right now
227- return this . emitEventToBus ( {
228- type : `${ this . #pluginId} :${ eventSuffix } ` ,
229- payload,
230- pluginId : this . #pluginId,
231- } )
254+ return this . emitEventToBus ( this . createEventPayload ( eventSuffix , payload ) )
232255 }
233256
234257 on <
@@ -246,8 +269,20 @@ export class EventClient<
246269 TEventMap [ `${TPluginId & string } :${TSuffix } `]
247270 > ,
248271 ) => void ,
272+ options ?: {
273+ withEventTarget ?: boolean
274+ } ,
249275 ) {
276+ const withEventTarget = options ?. withEventTarget ?? false
250277 const eventName = `${ this . #pluginId} :${ eventSuffix } ` as const
278+ if ( withEventTarget ) {
279+ if ( ! this . #internalEventTarget) {
280+ this . #internalEventTarget = new EventTarget ( )
281+ }
282+ this . #internalEventTarget. addEventListener ( eventName , ( e ) => {
283+ cb ( ( e as CustomEvent ) . detail )
284+ } )
285+ }
251286 if ( ! this . #enabled) {
252287 this . debugLog (
253288 'Event bus client is disabled, not registering event' ,
@@ -262,6 +297,9 @@ export class EventClient<
262297 this . #eventTarget( ) . addEventListener ( eventName , handler )
263298 this . debugLog ( 'Registered event to bus' , eventName )
264299 return ( ) => {
300+ if ( withEventTarget ) {
301+ this . #internalEventTarget?. removeEventListener ( eventName , handler )
302+ }
265303 this . #eventTarget( ) . removeEventListener ( eventName , handler )
266304 }
267305 }
0 commit comments