Skip to content

Commit f3b4ccf

Browse files
committed
feat: add queuing to event-clients
1 parent 38e1cb4 commit f3b4ccf

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

examples/react/start/src/plugin.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ class QueryDevtoolsClient extends EventClient<EventMap> {
1919
constructor() {
2020
super({
2121
pluginId: 'query-devtools',
22+
debug: true,
2223
})
2324
}
2425
}
2526

2627
export const queryPlugin = new QueryDevtoolsClient()
28+
// this should be queued and emitted when bus is available
29+
queryPlugin.emit('test', {
30+
title: 'Query Devtools',
31+
description: 'A plugin for query debugging',
32+
})

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,26 @@ export class EventClient<
2323
#pluginId: TPluginId
2424
#eventTarget: () => EventTarget
2525
#debug: boolean
26+
#queuedEvents: Array<TanStackDevtoolsEvent<string, any>>
27+
#connected: boolean
28+
#connectIntervalId: number | null
29+
#connectEveryMs: number
30+
#retryCount = 0
31+
#maxRetries = 5
2632

33+
#connectFunction = () => {
34+
if (this.#retryCount < this.#maxRetries) {
35+
this.#retryCount++
36+
this.#eventTarget().dispatchEvent(new CustomEvent('tanstack-connect'))
37+
return
38+
}
39+
this.#eventTarget().removeEventListener(
40+
'tanstack-connect',
41+
this.#connectFunction,
42+
)
43+
this.debugLog('Max retries reached, giving up on connection')
44+
clearInterval(this.#connectIntervalId!)
45+
}
2746
constructor({
2847
pluginId,
2948
debug = false,
@@ -35,6 +54,46 @@ export class EventClient<
3554
this.#eventTarget = this.getGlobalTarget
3655
this.#debug = debug
3756
this.debugLog(' Initializing event subscription for plugin', this.#pluginId)
57+
this.#queuedEvents = []
58+
this.#connected = false
59+
this.#connectIntervalId = null
60+
this.#connectEveryMs = 600
61+
this.startConnectLoop()
62+
}
63+
64+
private startConnectLoop() {
65+
if (this.#connectIntervalId !== null) return
66+
this.debugLog(`Starting connect loop (every ${this.#connectEveryMs}ms)`)
67+
const onConnected = () => {
68+
this.debugLog('Connected to event bus')
69+
this.#connected = true
70+
this.debugLog('Emitting queued events', this.#queuedEvents)
71+
this.#queuedEvents.forEach((event) => this.emitEventToBus(event))
72+
this.#queuedEvents = []
73+
this.stopConnectLoop()
74+
this.#eventTarget().removeEventListener(
75+
'tanstack-connect-success',
76+
onConnected,
77+
)
78+
}
79+
this.#eventTarget().addEventListener(
80+
'tanstack-connect-success',
81+
onConnected,
82+
)
83+
84+
this.#connectIntervalId = setInterval(
85+
this.#connectFunction,
86+
this.#connectEveryMs,
87+
) as unknown as number
88+
}
89+
90+
private stopConnectLoop() {
91+
if (this.#connectIntervalId === null) {
92+
return
93+
}
94+
clearInterval(this.#connectIntervalId)
95+
this.#connectIntervalId = null
96+
this.debugLog('Stopped connect loop')
3897
}
3998

4099
private debugLog(...args: Array<any>) {
@@ -84,7 +143,17 @@ export class EventClient<
84143
eventSuffix: TSuffix,
85144
payload: TEventMap[`${TPluginId & string}:${TSuffix}`],
86145
) {
87-
this.emitEventToBus({
146+
// wait to connect to the bus
147+
if (!this.#connected) {
148+
this.debugLog('Bus not available, will be pushed as soon as connected')
149+
return this.#queuedEvents.push({
150+
type: `${this.#pluginId}:${eventSuffix}`,
151+
payload,
152+
pluginId: this.#pluginId,
153+
})
154+
}
155+
// emit right now
156+
return this.emitEventToBus({
88157
type: `${this.#pluginId}:${eventSuffix}`,
89158
payload,
90159
pluginId: this.#pluginId,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ export class ClientEventBus {
3030
#eventTarget: EventTarget
3131
#debug: boolean
3232
#connectToServerBus: boolean
33+
3334
#dispatcher = (e: Event) => {
3435
const event = (e as CustomEvent).detail
3536
this.emitToServer(event)
3637
this.emitToClients(event)
3738
}
39+
#connectFunction = () => {
40+
this.debugLog(
41+
'Connection request made to event-bus, replying back with success',
42+
)
43+
this.#eventTarget.dispatchEvent(new CustomEvent('tanstack-connect-success'))
44+
}
3845
constructor({
3946
port = 42069,
4047
debug = false,
@@ -46,6 +53,7 @@ export class ClientEventBus {
4653
this.#socket = null
4754
this.#connectToServerBus = connectToServerBus
4855
this.#eventTarget = this.getGlobalTarget()
56+
4957
this.debugLog('Initializing client event bus')
5058
}
5159

@@ -91,6 +99,10 @@ export class ClientEventBus {
9199
'tanstack-dispatch-event',
92100
this.#dispatcher,
93101
)
102+
this.#eventTarget.addEventListener(
103+
'tanstack-connect',
104+
this.#connectFunction,
105+
)
94106
}
95107
stop() {
96108
this.debugLog('Stopping client event bus')
@@ -101,6 +113,10 @@ export class ClientEventBus {
101113
'tanstack-dispatch-event',
102114
this.#dispatcher,
103115
)
116+
this.#eventTarget.removeEventListener(
117+
'tanstack-connect',
118+
this.#connectFunction,
119+
)
104120
this.#eventSource?.close()
105121
this.#socket?.close()
106122
this.#socket = null

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export class ServerEventBus {
3333
this.debugLog('Dispatching event from dispatcher, forwarding', event)
3434
this.emit(event)
3535
}
36-
36+
#connectFunction = () => {
37+
this.#eventTarget.dispatchEvent(new CustomEvent('tanstack-connect-success'))
38+
}
3739
constructor({ port = 42069, debug = false } = {}) {
3840
this.#port = port
3941
this.#eventTarget = globalThis.__EVENT_TARGET__ ?? new EventTarget()
@@ -165,6 +167,10 @@ export class ServerEventBus {
165167
'tanstack-dispatch-event',
166168
this.#dispatcher,
167169
)
170+
this.#eventTarget.addEventListener(
171+
'tanstack-connect',
172+
this.#connectFunction,
173+
)
168174
this.handleNewConnection(wss)
169175

170176
// Handle connection upgrade for WebSocket
@@ -200,6 +206,10 @@ export class ServerEventBus {
200206
'tanstack-dispatch-event',
201207
this.#dispatcher,
202208
)
209+
this.#eventTarget.removeEventListener(
210+
'tanstack-connect',
211+
this.#connectFunction,
212+
)
203213
this.debugLog('[tanstack-devtools] All connections cleared')
204214
}
205215
}

0 commit comments

Comments
 (0)