1
- import type { StandardSchemaV1 } from '@standard-schema/spec'
2
-
3
1
interface TanStackDevtoolsEvent < TEventName extends string , TPayload = any > {
4
2
type : TEventName
5
3
payload : TPayload
@@ -10,16 +8,8 @@ declare global {
10
8
var __TANSTACK_EVENT_TARGET__ : EventTarget | null
11
9
}
12
10
13
- export type EventMap < TEventPrefix extends string > = Record <
14
- `${TEventPrefix } :${string } `,
15
- StandardSchemaV1 . InferInput < any >
16
- >
17
-
18
11
type 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 ] >
23
13
} [ keyof TEventMap ]
24
14
25
15
export class TanstackDevtoolsEventSubscription <
@@ -33,6 +23,7 @@ export class TanstackDevtoolsEventSubscription<
33
23
#pluginId: TPluginId
34
24
#eventTarget: ( ) => EventTarget
35
25
#debug: boolean
26
+
36
27
constructor ( {
37
28
pluginId,
38
29
debug = false ,
@@ -81,32 +72,35 @@ export class TanstackDevtoolsEventSubscription<
81
72
)
82
73
}
83
74
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 } `] ,
89
78
) {
90
- this . emitEventToBus ( event )
79
+ this . emitEventToBus ( {
80
+ type : `${ this . #pluginId} :${ eventSuffix } ` ,
81
+ payload,
82
+ pluginId : this . #pluginId,
83
+ } )
91
84
}
92
85
93
- on < TKey extends keyof TEventMap > (
94
- eventName : TKey ,
86
+ on < TSuffix extends string > (
87
+ eventSuffix : TSuffix ,
95
88
cb : (
96
89
event : TanStackDevtoolsEvent <
97
- TKey & string ,
98
- StandardSchemaV1 . InferOutput < TEventMap [ TKey ] >
90
+ `${ TPluginId & string } :${ TSuffix } ` ,
91
+ TEventMap [ `${ TPluginId & string } :${ TSuffix } ` ]
99
92
> ,
100
93
) => void ,
101
94
) {
95
+ const eventName = `${ this . #pluginId} :${ eventSuffix } ` as const
102
96
const handler = ( e : Event ) => {
103
97
this . debugLog ( 'Received event from bus' , ( e as CustomEvent ) . detail )
104
98
cb ( ( e as CustomEvent ) . detail )
105
99
}
106
- this . #eventTarget( ) . addEventListener ( eventName as string , handler )
100
+ this . #eventTarget( ) . addEventListener ( eventName , handler )
107
101
this . debugLog ( 'Registered event to bus' , eventName )
108
102
return ( ) => {
109
- this . #eventTarget( ) . removeEventListener ( eventName as string , handler )
103
+ this . #eventTarget( ) . removeEventListener ( eventName , handler )
110
104
}
111
105
}
112
106
0 commit comments