Skip to content

Commit f85fcf5

Browse files
fix: fix a bug where event client wouldn't connect to the event bus (#88)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 2e33bff commit f85fcf5

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

.changeset/cold-adults-sleep.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@tanstack/devtools-event-client': patch
3+
'@tanstack/devtools-vite': patch
4+
'@tanstack/devtools-event-bus': patch
5+
---
6+
7+
fix a bug for server event bus not connecting with clients properly

examples/react/basic/src/setup.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export default function DevtoolsExample() {
6060
return (
6161
<>
6262
<TanStackDevtools
63+
eventBusConfig={{
64+
connectToServerBus: true,
65+
}}
6366
plugins={[
6467
{
6568
name: 'TanStack Query',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { queryPlugin } from '@/plugin'
21
import { useEffect, useState } from 'react'
2+
import { queryPlugin } from '@/plugin'
33

44
export default function ClientPlugin() {
55
const [events, setEvents] = useState<

packages/devtools-vite/src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { normalizePath } from 'vite'
2+
// import fs from 'node:fs/promises'
23
import type { Connect } from 'vite'
34
import type { IncomingMessage, ServerResponse } from 'node:http'
45

@@ -47,3 +48,24 @@ export const handleDevToolsViteRequest = (
4748
res.write('OK')
4849
})
4950
}
51+
52+
/* export const tryReadFile = async (
53+
filePath: string
54+
) => {
55+
try {
56+
const data = await fs.readFile(filePath, 'utf-8')
57+
return data
58+
} catch (error) {
59+
60+
return null
61+
}
62+
}
63+
64+
export const tryParseJson = (jsonString: string) => {
65+
try {
66+
const result = JSON.parse(jsonString)
67+
return result
68+
} catch (error) {
69+
return null
70+
}
71+
} */

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ export class EventClient<
4242
)
4343
}
4444
#connectFunction = () => {
45+
this.#eventTarget().addEventListener(
46+
'tanstack-connect-success',
47+
this.#onConnected,
48+
)
4549
if (this.#retryCount < this.#maxRetries) {
4650
this.#retryCount++
4751
this.#eventTarget().dispatchEvent(new CustomEvent('tanstack-connect'))
4852
return
4953
}
54+
5055
this.#eventTarget().removeEventListener(
5156
'tanstack-connect',
5257
this.#connectFunction,
@@ -71,10 +76,6 @@ export class EventClient<
7176
this.#connectIntervalId = null
7277
this.#connectEveryMs = 500
7378

74-
this.#eventTarget().addEventListener(
75-
'tanstack-connect-success',
76-
this.#onConnected,
77-
)
7879
this.#connectFunction()
7980
this.startConnectLoop()
8081
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ declare global {
1717
// eslint-disable-next-line no-var
1818
var __TANSTACK_DEVTOOLS_WSS_SERVER__: WebSocketServer | null
1919
// eslint-disable-next-line no-var
20-
var __EVENT_TARGET__: EventTarget | null
20+
var __TANSTACK_EVENT_TARGET__: EventTarget | null
2121
}
2222

2323
export interface ServerEventBusConfig {
@@ -39,14 +39,18 @@ export class ServerEventBus {
3939
this.emit(event)
4040
}
4141
#connectFunction = () => {
42+
this.debugLog(
43+
'Connection request made to event-bus, replying back with success',
44+
)
4245
this.#eventTarget.dispatchEvent(new CustomEvent('tanstack-connect-success'))
4346
}
4447
constructor({ port = 42069, debug = false }: ServerEventBusConfig = {}) {
4548
this.#port = port
46-
this.#eventTarget = globalThis.__EVENT_TARGET__ ?? new EventTarget()
49+
this.#eventTarget =
50+
globalThis.__TANSTACK_EVENT_TARGET__ ?? new EventTarget()
4751
// we want to set the global event target only once so that we can emit/listen to events on the server
48-
if (!globalThis.__EVENT_TARGET__) {
49-
globalThis.__EVENT_TARGET__ = this.#eventTarget
52+
if (!globalThis.__TANSTACK_EVENT_TARGET__) {
53+
globalThis.__TANSTACK_EVENT_TARGET__ = this.#eventTarget
5054
}
5155
this.#server = globalThis.__TANSTACK_DEVTOOLS_SERVER__ ?? null
5256
this.#wssServer = globalThis.__TANSTACK_DEVTOOLS_WSS_SERVER__ ?? null

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)