Skip to content

Commit 8e362c1

Browse files
committed
feat: enhance plugin api contract and typing
1 parent 1333d87 commit 8e362c1

File tree

3 files changed

+35
-44
lines changed

3 files changed

+35
-44
lines changed

packages/event-bus-plugin/README.md

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

55
## General Usage
66

7-
```tsx
8-
import { z } from 'zod'
7+
```tsx
98
import { TanstackDevtoolsEventSubscription } from '@tanstack/devtools-event-bus-plugin'
10-
11-
const eventMap = {
12-
'query-devtools:test': z.object({
13-
title: z.string(),
14-
description: z.string(),
15-
}),
16-
'query-devtools:init': z.object({
17-
title: z.string(),
18-
description: z.string(),
19-
}),
20-
'query-devtools:query': z.object({
21-
title: z.string(),
22-
description: z.string(),
23-
}),
9+
interface EventMap {
10+
'query-devtools:a': { foo: string }
11+
'query-devtools:b': { foo: number }
2412
}
13+
14+
2515

2616
class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<
27-
typeof eventMap
17+
EventMap
2818
> {
2919
constructor() {
3020
super({
@@ -34,6 +24,15 @@ class QueryDevtoolsPlugin extends TanstackDevtoolsEventSubscription<
3424
}
3525

3626
export const queryPlugin = new QueryDevtoolsPlugin()
27+
28+
// I'm fully typed here
29+
plugin.emit("a", {
30+
foo: "bar"
31+
})
32+
plugin.on('b', (e) => {
33+
// I'm fully typed here
34+
e.payload.foo
35+
})
3736
```
3837

3938
## Understanding the implementation

packages/event-bus-plugin/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,5 @@
5252
"test:build": "publint --strict",
5353
"build": "vite build"
5454
},
55-
"devDependencies": {
56-
"@standard-schema/spec": "^1.0.0"
57-
}
58-
}
55+
"devDependencies": {}
56+
}

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { StandardSchemaV1 } from '@standard-schema/spec'
2-
31
interface 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-
1811
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]>
2313
}[keyof TEventMap]
2414

2515
export 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

Comments
 (0)