1- import type { StandardSchemaV1 } from '@standard-schema/spec'
2-
31interface TanStackDevtoolsEvent < TEventName extends string , TPayload = any > {
42 type : TEventName
53 payload : TPayload
@@ -10,16 +8,8 @@ declare global {
108 var __TANSTACK_EVENT_TARGET__ : EventTarget | null
119}
1210
13- export type EventMap < TEventPrefix extends string > = Record <
14- `${TEventPrefix } :${string } `,
15- StandardSchemaV1 . InferInput < any >
16- >
17-
1811type AllDevtoolsEvents < TEventMap extends Record < string , any > > = {
19- [ Key in keyof TEventMap ] : TanStackDevtoolsEvent <
20- Key & string ,
21- StandardSchemaV1 . InferOutput < TEventMap [ Key ] >
22- >
12+ [ Key in keyof TEventMap ] : TanStackDevtoolsEvent < Key & string , TEventMap [ Key ] >
2313} [ keyof TEventMap ]
2414
2515export class TanstackDevtoolsEventSubscription <
@@ -33,6 +23,7 @@ export class TanstackDevtoolsEventSubscription<
3323 #pluginId: TPluginId
3424 #eventTarget: ( ) => EventTarget
3525 #debug: boolean
26+
3627 constructor ( {
3728 pluginId,
3829 debug = false ,
@@ -81,32 +72,35 @@ export class TanstackDevtoolsEventSubscription<
8172 )
8273 }
8374
84- emit < TKey extends keyof TEventMap > (
85- event : TanStackDevtoolsEvent <
86- TKey & string ,
87- StandardSchemaV1 . InferOutput < TEventMap [ TKey ] >
88- > ,
75+ emit < TSuffix extends string > (
76+ eventSuffix : TSuffix ,
77+ payload : TEventMap [ `${TPluginId & string } :${TSuffix } `] ,
8978 ) {
90- this . emitEventToBus ( event )
79+ this . emitEventToBus ( {
80+ type : `${ this . #pluginId} :${ eventSuffix } ` ,
81+ payload,
82+ pluginId : this . #pluginId,
83+ } )
9184 }
9285
93- on < TKey extends keyof TEventMap > (
94- eventName : TKey ,
86+ on < TSuffix extends string > (
87+ eventSuffix : TSuffix ,
9588 cb : (
9689 event : TanStackDevtoolsEvent <
97- TKey & string ,
98- StandardSchemaV1 . InferOutput < TEventMap [ TKey ] >
90+ `${ TPluginId & string } :${ TSuffix } ` ,
91+ TEventMap [ `${ TPluginId & string } :${ TSuffix } ` ]
9992 > ,
10093 ) => void ,
10194 ) {
95+ const eventName = `${ this . #pluginId} :${ eventSuffix } ` as const
10296 const handler = ( e : Event ) => {
10397 this . debugLog ( 'Received event from bus' , ( e as CustomEvent ) . detail )
10498 cb ( ( e as CustomEvent ) . detail )
10599 }
106- this . #eventTarget( ) . addEventListener ( eventName as string , handler )
100+ this . #eventTarget( ) . addEventListener ( eventName , handler )
107101 this . debugLog ( 'Registered event to bus' , eventName )
108102 return ( ) => {
109- this . #eventTarget( ) . removeEventListener ( eventName as string , handler )
103+ this . #eventTarget( ) . removeEventListener ( eventName , handler )
110104 }
111105 }
112106
0 commit comments