File tree Expand file tree Collapse file tree 9 files changed +63
-72
lines changed
packages/event-bus-plugin Expand file tree Collapse file tree 9 files changed +63
-72
lines changed Original file line number Diff line number Diff line change @@ -2,17 +2,14 @@ import ReactDOM from 'react-dom/client'
2
2
import Devtools from './setup'
3
3
import { queryPlugin } from './plugin'
4
4
setTimeout ( ( ) => {
5
- queryPlugin . emit ( {
6
- payload : {
7
- title : 'Test Event' ,
8
- description :
9
- 'This is a test event from the TanStack Query Devtools plugin.' ,
10
- } ,
11
- type : 'query-devtools:test' ,
5
+ queryPlugin . emit ( 'test' , {
6
+ title : 'Test Event' ,
7
+ description :
8
+ 'This is a test event from the TanStack Query Devtools plugin.' ,
12
9
} )
13
10
} , 1000 )
14
11
15
- queryPlugin . on ( 'query-devtools: test' , ( event ) => {
12
+ queryPlugin . on ( 'test' , ( event ) => {
16
13
console . log ( 'Received test event:' , event )
17
14
} )
18
15
function App ( ) {
Original file line number Diff line number Diff line change 1
- import { z } from 'zod'
2
1
import { TanstackDevtoolsEventSubscription } from '@tanstack/devtools-event-bus-plugin'
3
2
4
- const eventMap = {
5
- 'query-devtools:test' : z . object ( {
6
- title : z . string ( ) ,
7
- description : z . string ( ) ,
8
- } ) ,
9
- 'query-devtools:init' : z . object ( {
10
- title : z . string ( ) ,
11
- description : z . string ( ) ,
12
- } ) ,
13
- 'query-devtools:query' : z . object ( {
14
- title : z . string ( ) ,
15
- description : z . string ( ) ,
16
- } ) ,
3
+ interface EventMap {
4
+ 'query-devtools:test' : {
5
+ title : string
6
+ description : string
7
+ }
8
+ 'query-devtools:init' : {
9
+ title : string
10
+ description : string
11
+ }
12
+ 'query-devtools:query' : {
13
+ title : string
14
+ description : string
15
+ }
17
16
}
18
-
19
- class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription <
20
- typeof eventMap
21
- > {
17
+ class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription < EventMap > {
22
18
constructor ( ) {
23
19
super ( {
24
20
pluginId : 'query-devtools' ,
Original file line number Diff line number Diff line change @@ -6,9 +6,9 @@ export default function ClientPlugin() {
6
6
Array < { type : string ; payload : { title : string ; description : string } } >
7
7
> ( [ ] )
8
8
useEffect ( ( ) => {
9
- const cleanup = queryPlugin . on ( 'query-devtools: test' , ( event ) => {
9
+ const cleanup = queryPlugin . on ( 'test' , ( event ) => {
10
10
console . log ( 'Received message in here:' , event )
11
- setEvents ( ( prev ) => [ ...prev , event ] )
11
+ setEvents ( ( prev ) => [ ...prev , event as any ] )
12
12
} )
13
13
14
14
return cleanup
@@ -21,13 +21,10 @@ export default function ClientPlugin() {
21
21
className = "bg-blue-500 text-white px-4 py-2 rounded"
22
22
onClick = { ( ) => {
23
23
console . log ( 'Button clicked, emitting event' )
24
- queryPlugin . emit ( {
25
- type : 'query-devtools:test' ,
26
- payload : {
27
- title : 'Button Clicked' ,
28
- description :
29
- 'This is a custom event triggered by the client plugin.' ,
30
- } ,
24
+ queryPlugin . emit ( 'test' , {
25
+ title : 'Button Clicked' ,
26
+ description :
27
+ 'This is a custom event triggered by the client plugin.' ,
31
28
} )
32
29
} }
33
30
>
Original file line number Diff line number Diff line change 1
- import { z } from 'zod'
2
1
import { TanstackDevtoolsEventSubscription } from '@tanstack/devtools-event-bus-plugin'
3
2
4
- const eventMap = {
5
- 'query-devtools:test' : z . object ( {
6
- title : z . string ( ) ,
7
- description : z . string ( ) ,
8
- } ) ,
9
- 'query-devtools:init' : z . object ( {
10
- title : z . string ( ) ,
11
- description : z . string ( ) ,
12
- } ) ,
13
- 'query-devtools:query' : z . object ( {
14
- title : z . string ( ) ,
15
- description : z . string ( ) ,
16
- } ) ,
3
+ interface EventMap {
4
+ 'query-devtools:test' : {
5
+ title : string
6
+ description : string
7
+ }
8
+ 'query-devtools:init' : {
9
+ title : string
10
+ description : string
11
+ }
12
+ 'query-devtools:query' : {
13
+ title : string
14
+ description : string
15
+ }
17
16
}
18
17
19
- class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription <
20
- typeof eventMap
21
- > {
18
+ class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription < EventMap > {
22
19
constructor ( ) {
23
20
super ( {
24
21
pluginId : 'query-devtools' ,
Original file line number Diff line number Diff line change @@ -6,16 +6,13 @@ This package is still under active development and might have breaking changes i
6
6
7
7
``` tsx
8
8
import { TanstackDevtoolsEventSubscription } from ' @tanstack/devtools-event-bus-plugin'
9
- interface EventMap {
9
+
10
+ interface EventMap {
10
11
' query-devtools:a' : { foo: string }
11
12
' query-devtools:b' : { foo: number }
12
- }
13
-
14
-
13
+ }
15
14
16
- class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription <
17
- EventMap
18
- > {
15
+ class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription <EventMap > {
19
16
constructor () {
20
17
super ({
21
18
pluginId: ' query-devtools' ,
Original file line number Diff line number Diff line change 51
51
"test:types" : " tsc" ,
52
52
"test:build" : " publint --strict" ,
53
53
"build" : " vite build"
54
- },
55
- "devDependencies" : {}
56
- }
54
+ }
55
+ }
Original file line number Diff line number Diff line change 1
1
export { TanstackDevtoolsEventSubscription } from './plugin'
2
- export type { EventMap } from './plugin'
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ export class TanstackDevtoolsEventSubscription<
22
22
> {
23
23
#pluginId: TPluginId
24
24
#eventTarget: ( ) => EventTarget
25
- #debug: boolean
26
25
26
+ #debug: boolean
27
27
constructor ( {
28
28
pluginId,
29
29
debug = false ,
@@ -72,18 +72,31 @@ export class TanstackDevtoolsEventSubscription<
72
72
)
73
73
}
74
74
75
- emit < TSuffix extends string > (
75
+ emit <
76
+ TSuffix extends Extract <
77
+ keyof TEventMap ,
78
+ `${TPluginId & string } :${string } `
79
+ > extends `${TPluginId & string } :${infer S } `
80
+ ? S
81
+ : never ,
82
+ > (
76
83
eventSuffix : TSuffix ,
77
84
payload : TEventMap [ `${TPluginId & string } :${TSuffix } `] ,
78
85
) {
79
86
this . emitEventToBus ( {
80
87
type : `${ this . #pluginId} :${ eventSuffix } ` ,
81
88
payload,
82
- pluginId : this . #pluginId,
83
89
} )
84
90
}
85
91
86
- on < TSuffix extends string > (
92
+ on <
93
+ TSuffix extends Extract <
94
+ keyof TEventMap ,
95
+ `${TPluginId & string } :${string } `
96
+ > extends `${TPluginId & string } :${infer S } `
97
+ ? S
98
+ : never ,
99
+ > (
87
100
eventSuffix : TSuffix ,
88
101
cb : (
89
102
event : TanStackDevtoolsEvent <
You can’t perform that action at this time.
0 commit comments