Skip to content

Commit f7009a5

Browse files
committed
feat: improve plugin ts typing
1 parent 8e362c1 commit f7009a5

File tree

9 files changed

+63
-72
lines changed

9 files changed

+63
-72
lines changed

examples/react/basic/src/index.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@ import ReactDOM from 'react-dom/client'
22
import Devtools from './setup'
33
import { queryPlugin } from './plugin'
44
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.',
129
})
1310
}, 1000)
1411

15-
queryPlugin.on('query-devtools:test', (event) => {
12+
queryPlugin.on('test', (event) => {
1613
console.log('Received test event:', event)
1714
})
1815
function App() {

examples/react/basic/src/plugin.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
import { z } from 'zod'
21
import { TanstackDevtoolsEventSubscription } from '@tanstack/devtools-event-bus-plugin'
32

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+
}
1716
}
18-
19-
class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<
20-
typeof eventMap
21-
> {
17+
class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<EventMap> {
2218
constructor() {
2319
super({
2420
pluginId: 'query-devtools',

examples/react/start/src/components/client-plugin.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ export default function ClientPlugin() {
66
Array<{ type: string; payload: { title: string; description: string } }>
77
>([])
88
useEffect(() => {
9-
const cleanup = queryPlugin.on('query-devtools:test', (event) => {
9+
const cleanup = queryPlugin.on('test', (event) => {
1010
console.log('Received message in here:', event)
11-
setEvents((prev) => [...prev, event])
11+
setEvents((prev) => [...prev, event as any])
1212
})
1313

1414
return cleanup
@@ -21,13 +21,10 @@ export default function ClientPlugin() {
2121
className="bg-blue-500 text-white px-4 py-2 rounded"
2222
onClick={() => {
2323
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.',
3128
})
3229
}}
3330
>

examples/react/start/src/plugin.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import { z } from 'zod'
21
import { TanstackDevtoolsEventSubscription } from '@tanstack/devtools-event-bus-plugin'
32

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+
}
1716
}
1817

19-
class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<
20-
typeof eventMap
21-
> {
18+
class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<EventMap> {
2219
constructor() {
2320
super({
2421
pluginId: 'query-devtools',

packages/event-bus-plugin/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ This package is still under active development and might have breaking changes i
66

77
```tsx
88
import { TanstackDevtoolsEventSubscription } from '@tanstack/devtools-event-bus-plugin'
9-
interface EventMap {
9+
10+
interface EventMap {
1011
'query-devtools:a': { foo: string }
1112
'query-devtools:b': { foo: number }
12-
}
13-
14-
13+
}
1514

16-
class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<
17-
EventMap
18-
> {
15+
class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<EventMap> {
1916
constructor() {
2017
super({
2118
pluginId: 'query-devtools',

packages/event-bus-plugin/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,5 @@
5151
"test:types": "tsc",
5252
"test:build": "publint --strict",
5353
"build": "vite build"
54-
},
55-
"devDependencies": {}
56-
}
54+
}
55+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { TanstackDevtoolsEventSubscription } from './plugin'
2-
export type { EventMap } from './plugin'

packages/event-bus-plugin/src/plugin.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export class TanstackDevtoolsEventSubscription<
2222
> {
2323
#pluginId: TPluginId
2424
#eventTarget: () => EventTarget
25-
#debug: boolean
2625

26+
#debug: boolean
2727
constructor({
2828
pluginId,
2929
debug = false,
@@ -72,18 +72,31 @@ export class TanstackDevtoolsEventSubscription<
7272
)
7373
}
7474

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+
>(
7683
eventSuffix: TSuffix,
7784
payload: TEventMap[`${TPluginId & string}:${TSuffix}`],
7885
) {
7986
this.emitEventToBus({
8087
type: `${this.#pluginId}:${eventSuffix}`,
8188
payload,
82-
pluginId: this.#pluginId,
8389
})
8490
}
8591

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+
>(
87100
eventSuffix: TSuffix,
88101
cb: (
89102
event: TanStackDevtoolsEvent<

pnpm-lock.yaml

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)