Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/cold-adults-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/devtools-event-client': patch
'@tanstack/devtools-vite': patch
'@tanstack/devtools-event-bus': patch
---

fix a bug for server event bus not connecting with clients properly
3 changes: 3 additions & 0 deletions examples/react/basic/src/setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export default function DevtoolsExample() {
return (
<>
<TanStackDevtools
eventBusConfig={{
connectToServerBus: true,
}}
plugins={[
{
name: 'TanStack Query',
Expand Down
2 changes: 1 addition & 1 deletion examples/react/start/src/components/client-plugin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { queryPlugin } from '@/plugin'
import { useEffect, useState } from 'react'
import { queryPlugin } from '@/plugin'

export default function ClientPlugin() {
const [events, setEvents] = useState<
Expand Down
22 changes: 22 additions & 0 deletions packages/devtools-vite/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { normalizePath } from 'vite'
// import fs from 'node:fs/promises'
import type { Connect } from 'vite'
import type { IncomingMessage, ServerResponse } from 'node:http'

Expand Down Expand Up @@ -47,3 +48,24 @@ export const handleDevToolsViteRequest = (
res.write('OK')
})
}

/* export const tryReadFile = async (
filePath: string
) => {
try {
const data = await fs.readFile(filePath, 'utf-8')
return data
} catch (error) {

return null
}
}

export const tryParseJson = (jsonString: string) => {
try {
const result = JSON.parse(jsonString)
return result
} catch (error) {
return null
}
} */
9 changes: 5 additions & 4 deletions packages/event-bus-client/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ export class EventClient<
)
}
#connectFunction = () => {
this.#eventTarget().addEventListener(
'tanstack-connect-success',
this.#onConnected,
)
if (this.#retryCount < this.#maxRetries) {
this.#retryCount++
this.#eventTarget().dispatchEvent(new CustomEvent('tanstack-connect'))
return
}

this.#eventTarget().removeEventListener(
'tanstack-connect',
this.#connectFunction,
Expand All @@ -71,10 +76,6 @@ export class EventClient<
this.#connectIntervalId = null
this.#connectEveryMs = 500

this.#eventTarget().addEventListener(
'tanstack-connect-success',
this.#onConnected,
)
this.#connectFunction()
this.startConnectLoop()
}
Expand Down
12 changes: 8 additions & 4 deletions packages/event-bus/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare global {
// eslint-disable-next-line no-var
var __TANSTACK_DEVTOOLS_WSS_SERVER__: WebSocketServer | null
// eslint-disable-next-line no-var
var __EVENT_TARGET__: EventTarget | null
var __TANSTACK_EVENT_TARGET__: EventTarget | null
}

export interface ServerEventBusConfig {
Expand All @@ -39,14 +39,18 @@ export class ServerEventBus {
this.emit(event)
}
#connectFunction = () => {
this.debugLog(
'Connection request made to event-bus, replying back with success',
)
this.#eventTarget.dispatchEvent(new CustomEvent('tanstack-connect-success'))
}
constructor({ port = 42069, debug = false }: ServerEventBusConfig = {}) {
this.#port = port
this.#eventTarget = globalThis.__EVENT_TARGET__ ?? new EventTarget()
this.#eventTarget =
globalThis.__TANSTACK_EVENT_TARGET__ ?? new EventTarget()
// we want to set the global event target only once so that we can emit/listen to events on the server
if (!globalThis.__EVENT_TARGET__) {
globalThis.__EVENT_TARGET__ = this.#eventTarget
if (!globalThis.__TANSTACK_EVENT_TARGET__) {
globalThis.__TANSTACK_EVENT_TARGET__ = this.#eventTarget
}
this.#server = globalThis.__TANSTACK_DEVTOOLS_SERVER__ ?? null
this.#wssServer = globalThis.__TANSTACK_DEVTOOLS_WSS_SERVER__ ?? null
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading