Skip to content

Commit cf23787

Browse files
feat: simplify EventClient types to accept unprefixed event maps (#361)
* feat: simplify EventClient types to accept unprefixed event maps EventClient now accepts event maps with plain event names instead of requiring the pluginId prefix in every key. The prefix is handled purely at runtime. This removes the complex conditional types that were stripping prefixes from emit/on/createEventPayload signatures. * ci: apply automated fixes * chore: add patch changeset for event client type simplification --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent b75b84d commit cf23787

File tree

11 files changed

+51
-87
lines changed

11 files changed

+51
-87
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@tanstack/devtools-event-client': patch
3+
'@tanstack/devtools-client': patch
4+
---
5+
6+
Simplify EventClient types to accept unprefixed event maps

examples/preact/basic/src/plugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { EventClient } from '@tanstack/devtools-event-client'
22

33
interface EventMap {
4-
'query-devtools:test': {
4+
test: {
55
title: string
66
description: string
77
}
8-
'query-devtools:init': {
8+
init: {
99
title: string
1010
description: string
1111
}
12-
'query-devtools:query': {
12+
query: {
1313
title: string
1414
description: string
1515
}

examples/preact/custom-devtools/src/eventClient.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { EventClient } from '@tanstack/devtools-event-client'
22

33
type EventMap = {
4-
// The key of the event map is a combination of {pluginId}:{eventSuffix}
5-
// The value is the expected type of the event payload
6-
'custom-devtools:counter-state': { count: number; history: Array<number> }
4+
'counter-state': { count: number; history: Array<number> }
75
}
86

97
class CustomEventClient extends EventClient<EventMap> {
108
constructor() {
119
super({
12-
// The pluginId must match that of the event map key
1310
pluginId: 'custom-devtools',
1411
debug: true,
1512
})

examples/react/basic/src/plugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { EventClient } from '@tanstack/devtools-event-client'
22

33
interface EventMap {
4-
'query-devtools:test': {
4+
test: {
55
title: string
66
description: string
77
}
8-
'query-devtools:init': {
8+
init: {
99
title: string
1010
description: string
1111
}
12-
'query-devtools:query': {
12+
query: {
1313
title: string
1414
description: string
1515
}

examples/react/custom-devtools/src/eventClient.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { EventClient } from '@tanstack/devtools-event-client'
22

33
type EventMap = {
4-
// The key of the event map is a combination of {pluginId}:{eventSuffix}
5-
// The value is the expected type of the event payload
6-
'custom-devtools:counter-state': { count: number; history: Array<number> }
4+
'counter-state': { count: number; history: Array<number> }
75
}
86

97
class CustomEventClient extends EventClient<EventMap> {
108
constructor() {
119
super({
12-
// The pluginId must match that of the event map key
1310
pluginId: 'custom-devtools',
1411
debug: true,
1512
})

examples/react/https/src/plugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { EventClient } from '@tanstack/devtools-event-client'
22

33
interface EventMap {
4-
'query-devtools:test': {
4+
test: {
55
title: string
66
description: string
77
}
8-
'query-devtools:init': {
8+
init: {
99
title: string
1010
description: string
1111
}
12-
'query-devtools:query': {
12+
query: {
1313
title: string
1414
description: string
1515
}

examples/react/start/src/devtools/route-navigation-event-client.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@ export interface RouteNavigationEvent {
99
}
1010

1111
type RouteNavigationEventMap = {
12-
'route-navigation:navigate': RouteNavigationEvent
13-
'route-navigation:clear': undefined
12+
navigate: RouteNavigationEvent
13+
clear: undefined
1414
}
1515

16-
class RouteNavigationEventClient extends EventClient<
17-
RouteNavigationEventMap,
18-
'route-navigation'
19-
> {
16+
class RouteNavigationEventClient extends EventClient<RouteNavigationEventMap> {
2017
constructor() {
2118
super({
2219
pluginId: 'route-navigation',

examples/react/time-travel/src/zustand-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { EventClient } from '@tanstack/devtools-event-client'
22
import { createStore } from 'zustand'
33

44
interface ZustandEventMap {
5-
'zustand:stateChange': any
6-
'zustand:revertSnapshot': any
5+
stateChange: any
6+
revertSnapshot: any
77
}
88
export const eventClient = new EventClient<ZustandEventMap>({
99
pluginId: 'zustand',

packages/devtools-client/src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,37 @@ export interface PluginInjection {
5353
}
5454

5555
interface EventMap {
56-
'tanstack-devtools-core:ready': {
56+
ready: {
5757
packageJson: PackageJson | null
5858
outdatedDeps: OutdatedDeps | null
5959
}
60-
'tanstack-devtools-core:outdated-deps-read': {
60+
'outdated-deps-read': {
6161
outdatedDeps: OutdatedDeps | null
6262
}
63-
'tanstack-devtools-core:package-json-read': {
63+
'package-json-read': {
6464
packageJson: PackageJson | null
6565
}
66-
'tanstack-devtools-core:mounted': void
67-
'tanstack-devtools-core:install-devtools': PluginInjection
68-
'tanstack-devtools-core:devtools-installed': {
66+
mounted: void
67+
'install-devtools': PluginInjection
68+
'devtools-installed': {
6969
packageName: string
7070
success: boolean
7171
error?: string
7272
}
73-
'tanstack-devtools-core:add-plugin-to-devtools': PluginInjection
74-
'tanstack-devtools-core:plugin-added': {
73+
'add-plugin-to-devtools': PluginInjection
74+
'plugin-added': {
7575
packageName: string
7676
success: boolean
7777
error?: string
7878
}
79-
'tanstack-devtools-core:bump-package-version': PluginInjection & {
79+
'bump-package-version': PluginInjection & {
8080
devtoolsPackage: string
8181
minVersion?: string
8282
}
83-
'tanstack-devtools-core:package-json-updated': {
83+
'package-json-updated': {
8484
packageJson: PackageJson | null
8585
}
86-
'tanstack-devtools-core:trigger-toggled': {
86+
'trigger-toggled': {
8787
isOpen: boolean
8888
}
8989
}

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

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,12 @@ declare global {
88
}
99

1010
type AllDevtoolsEvents<TEventMap extends Record<string, any>> = {
11-
[Key in keyof TEventMap]: TanStackDevtoolsEvent<Key & string, TEventMap[Key]>
12-
}[keyof TEventMap]
11+
[Key in keyof TEventMap & string]: TanStackDevtoolsEvent<Key, TEventMap[Key]>
12+
}[keyof TEventMap & string]
1313

14-
export class EventClient<
15-
TEventMap extends Record<string, any>,
16-
TPluginId extends string = TEventMap extends Record<infer P, any>
17-
? P extends `${infer Id}:${string}`
18-
? Id
19-
: never
20-
: never,
21-
> {
14+
export class EventClient<TEventMap extends Record<string, any>> {
2215
#enabled = true
23-
#pluginId: TPluginId
16+
#pluginId: string
2417
#eventTarget: () => EventTarget
2518
#debug: boolean
2619
#queuedEvents: Array<TanStackDevtoolsEvent<string, any>>
@@ -80,7 +73,7 @@ export class EventClient<
8073
enabled = true,
8174
reconnectEveryMs = 300,
8275
}: {
83-
pluginId: TPluginId
76+
pluginId: string
8477
debug?: boolean
8578
reconnectEveryMs?: number
8679
enabled?: boolean
@@ -194,33 +187,19 @@ export class EventClient<
194187
this.dispatchCustomEvent('tanstack-dispatch-event', event)
195188
}
196189

197-
createEventPayload<
198-
TSuffix extends Extract<
199-
keyof TEventMap,
200-
`${TPluginId & string}:${string}`
201-
> extends `${TPluginId & string}:${infer S}`
202-
? S
203-
: never,
204-
>(
205-
eventSuffix: TSuffix,
206-
payload: TEventMap[`${TPluginId & string}:${TSuffix}`],
190+
createEventPayload<TEvent extends keyof TEventMap & string>(
191+
eventSuffix: TEvent,
192+
payload: TEventMap[TEvent],
207193
) {
208194
return {
209195
type: `${this.#pluginId}:${eventSuffix}`,
210196
payload,
211197
pluginId: this.#pluginId,
212198
}
213199
}
214-
emit<
215-
TSuffix extends Extract<
216-
keyof TEventMap,
217-
`${TPluginId & string}:${string}`
218-
> extends `${TPluginId & string}:${infer S}`
219-
? S
220-
: never,
221-
>(
222-
eventSuffix: TSuffix,
223-
payload: TEventMap[`${TPluginId & string}:${TSuffix}`],
200+
emit<TEvent extends keyof TEventMap & string>(
201+
eventSuffix: TEvent,
202+
payload: TEventMap[TEvent],
224203
) {
225204
if (!this.#enabled) {
226205
this.debugLog(
@@ -262,21 +241,9 @@ export class EventClient<
262241
return this.emitEventToBus(this.createEventPayload(eventSuffix, payload))
263242
}
264243

265-
on<
266-
TSuffix extends Extract<
267-
keyof TEventMap,
268-
`${TPluginId & string}:${string}`
269-
> extends `${TPluginId & string}:${infer S}`
270-
? S
271-
: never,
272-
>(
273-
eventSuffix: TSuffix,
274-
cb: (
275-
event: TanStackDevtoolsEvent<
276-
`${TPluginId & string}:${TSuffix}`,
277-
TEventMap[`${TPluginId & string}:${TSuffix}`]
278-
>,
279-
) => void,
244+
on<TEvent extends keyof TEventMap & string>(
245+
eventSuffix: TEvent,
246+
cb: (event: TanStackDevtoolsEvent<TEvent, TEventMap[TEvent]>) => void,
280247
options?: {
281248
withEventTarget?: boolean
282249
},

0 commit comments

Comments
 (0)