Skip to content

Commit 7924487

Browse files
committed
chore: improve the client event bus api
1 parent b72cc40 commit 7924487

File tree

6 files changed

+41
-31
lines changed

6 files changed

+41
-31
lines changed

packages/devtools/src/core.tsx

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,12 @@ import { Portal, render } from 'solid-js/web'
33
import { ClientEventBus } from '@tanstack/devtools-event-bus/client'
44
import { DevtoolsProvider } from './context/devtools-context'
55
import { initialState } from './context/devtools-store'
6+
import type { ClientEventBusConfig } from '@tanstack/devtools-event-bus/client'
67
import type {
78
TanStackDevtoolsConfig,
89
TanStackDevtoolsPlugin,
910
} from './context/devtools-context'
1011

11-
export interface EventBusConfig {
12-
/**
13-
* Optional flag to indicate if the devtools server event bus is available to connect to.
14-
* This is used to determine if the devtools can connect to the server for real-time event streams.
15-
*/
16-
hasServer?: boolean
17-
18-
/**
19-
* Optional flag to enable debug mode for the event bus.
20-
*/
21-
debug?: boolean
22-
23-
/**
24-
* Optional port to connect to the devtools server event bus.
25-
* Defaults to 42069.
26-
*/
27-
port?: number
28-
}
2912
export interface TanStackDevtoolsInit {
3013
/**
3114
* Configuration for the devtools shell. These configuration options are used to set the
@@ -53,7 +36,7 @@ export interface TanStackDevtoolsInit {
5336
* ```
5437
*/
5538
plugins?: Array<TanStackDevtoolsPlugin>
56-
eventBusConfig?: EventBusConfig
39+
eventBusConfig?: ClientEventBusConfig
5740
}
5841

5942
export class TanStackDevtoolsCore {
@@ -65,7 +48,7 @@ export class TanStackDevtoolsCore {
6548
#dispose?: () => void
6649
#Component: any
6750
#eventBus: ClientEventBus | undefined
68-
#eventBusConfig: EventBusConfig | undefined
51+
#eventBusConfig: ClientEventBusConfig | undefined
6952

7053
constructor(init: TanStackDevtoolsInit) {
7154
this.#plugins = init.plugins || []
@@ -83,10 +66,9 @@ export class TanStackDevtoolsCore {
8366
const mountTo = el
8467
const dispose = render(() => {
8568
this.#Component = lazy(() => import('./devtools'))
86-
const { hasServer, ...rest } = this.#eventBusConfig || {}
8769
const Devtools = this.#Component
88-
this.#eventBus = new ClientEventBus(rest)
89-
this.#eventBus.start(hasServer)
70+
this.#eventBus = new ClientEventBus(this.#eventBusConfig)
71+
this.#eventBus.start()
9072
return (
9173
<DevtoolsProvider plugins={this.#plugins} config={this.#config}>
9274
<Portal mount={mountTo}>
@@ -116,3 +98,5 @@ export class TanStackDevtoolsCore {
11698
}
11799
}
118100
}
101+
102+
export type { ClientEventBusConfig }

packages/devtools/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { PLUGIN_CONTAINER_ID, PLUGIN_TITLE_CONTAINER_ID } from './constants'
22
export { TanStackDevtoolsCore } from './core'
3-
export type { TanStackDevtoolsInit, EventBusConfig } from './core'
3+
export type { TanStackDevtoolsInit, ClientEventBusConfig } from './core'
44
export type {
55
TanStackDevtoolsPlugin,
66
TanStackDevtoolsConfig,

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,47 @@ interface TanStackDevtoolsEvent<TEventName extends string, TPayload = any> {
44
pluginId?: string // Optional pluginId to filter events by plugin
55
}
66

7+
export interface ClientEventBusConfig {
8+
/**
9+
* Optional flag to indicate if the devtools server event bus is available to connect to.
10+
* This is used to determine if the devtools can connect to the server for real-time event streams.
11+
*/
12+
connectToServerBus?: boolean
13+
14+
/**
15+
* Optional flag to enable debug mode for the event bus.
16+
*/
17+
debug?: boolean
18+
19+
/**
20+
* Optional port to connect to the devtools server event bus.
21+
* Defaults to 42069.
22+
*/
23+
port?: number
24+
}
25+
726
export class ClientEventBus {
827
#port: number
928
#socket: WebSocket | null
1029
#eventSource: EventSource | null
1130
#eventTarget: EventTarget
1231
#debug: boolean
32+
#connectToServerBus: boolean
1333
#dispatcher = (e: Event) => {
1434
const event = (e as CustomEvent).detail
1535
this.emitToServer(event)
1636
this.emitToClients(event)
1737
}
18-
constructor({ port = 42069, debug = false } = {}) {
38+
constructor({
39+
port = 42069,
40+
debug = false,
41+
connectToServerBus = false,
42+
}: ClientEventBusConfig = {}) {
1943
this.#debug = debug
2044
this.#eventSource = null
2145
this.#port = port
2246
this.#socket = null
47+
this.#connectToServerBus = connectToServerBus
2348
this.#eventTarget = this.getGlobalTarget()
2449
this.debugLog('Initializing client event bus')
2550
}
@@ -53,12 +78,12 @@ export class ClientEventBus {
5378
}).catch(() => {})
5479
}
5580
}
56-
start(hasServer?: boolean) {
81+
start() {
5782
this.debugLog('Starting client event bus')
5883
if (typeof window === 'undefined') {
5984
return
6085
}
61-
if (hasServer) {
86+
if (this.#connectToServerBus) {
6287
this.connect()
6388
}
6489
this.#eventTarget = window
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { ClientEventBus } from './client'
2+
export type { ClientEventBusConfig } from './client'

packages/react-devtools/src/devtools.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { createPortal } from 'react-dom'
88
import type { JSX } from 'react'
99
import type {
10-
EventBusConfig,
10+
ClientEventBusConfig,
1111
TanStackDevtoolsConfig,
1212
TanStackDevtoolsPlugin,
1313
} from '@tanstack/devtools'
@@ -87,7 +87,7 @@ export interface TanStackDevtoolsReactInit {
8787
/**
8888
* Configuration for the TanStack Devtools client event bus.
8989
*/
90-
eventBusConfig?: EventBusConfig
90+
eventBusConfig?: ClientEventBusConfig
9191
}
9292

9393
const convertRender = (

packages/solid-devtools/src/devtools.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createEffect, createSignal, onCleanup, onMount } from 'solid-js'
33
import { Portal } from 'solid-js/web'
44
import type { JSX } from 'solid-js'
55
import type {
6-
EventBusConfig,
6+
ClientEventBusConfig,
77
TanStackDevtoolsConfig,
88
TanStackDevtoolsPlugin,
99
} from '@tanstack/devtools'
@@ -90,7 +90,7 @@ interface TanstackDevtoolsInit {
9090
/**
9191
* Configuration for the TanStack Devtools client event bus.
9292
*/
93-
eventBusConfig?: EventBusConfig
93+
eventBusConfig?: ClientEventBusConfig
9494
}
9595

9696
export const TanstackDevtools = ({

0 commit comments

Comments
 (0)