Skip to content

Commit 53c6751

Browse files
Chore/fix connecting issue with client (#261)
* chore: fix issue with config * fix issue with the connection logic * fix issue with the connection logic * ci: apply automated fixes * fix tests * fix --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 87764c2 commit 53c6751

File tree

3 files changed

+274
-27
lines changed

3 files changed

+274
-27
lines changed

.changeset/full-humans-create.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/devtools-utils': patch
3+
---
4+
5+
fix issue with client connectivity by not calling the connect method multiple times

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ export class EventClient<
2929
#connectEveryMs: number
3030
#retryCount = 0
3131
#maxRetries = 5
32+
#connecting = false
33+
3234
#onConnected = () => {
3335
this.debugLog('Connected to event bus')
3436
this.#connected = true
37+
this.#connecting = false
3538
this.debugLog('Emitting queued events', this.#queuedEvents)
3639
this.#queuedEvents.forEach((event) => this.emitEventToBus(event))
3740
this.#queuedEvents = []
@@ -41,30 +44,39 @@ export class EventClient<
4144
this.#onConnected,
4245
)
4346
}
44-
#connectFunction = () => {
45-
this.#eventTarget().addEventListener(
46-
'tanstack-connect-success',
47-
this.#onConnected,
48-
)
47+
// fired off right away and then at intervals
48+
#retryConnection = () => {
4949
if (this.#retryCount < this.#maxRetries) {
5050
this.#retryCount++
5151
this.dispatchCustomEvent('tanstack-connect', {})
52+
5253
return
5354
}
54-
5555
this.#eventTarget().removeEventListener(
5656
'tanstack-connect',
57-
this.#connectFunction,
57+
this.#retryConnection,
5858
)
59+
5960
this.debugLog('Max retries reached, giving up on connection')
6061
this.stopConnectLoop()
6162
}
6263

64+
// This is run to register connection handlers on first emit attempt
65+
#connectFunction = () => {
66+
if (this.#connecting) return
67+
this.#connecting = true
68+
this.#eventTarget().addEventListener(
69+
'tanstack-connect-success',
70+
this.#onConnected,
71+
)
72+
this.#retryConnection()
73+
}
74+
6375
constructor({
6476
pluginId,
6577
debug = false,
6678
enabled = true,
67-
reconnectEveryMs = 1000,
79+
reconnectEveryMs = 300,
6880
}: {
6981
pluginId: TPluginId
7082
debug?: boolean
@@ -83,16 +95,18 @@ export class EventClient<
8395
}
8496

8597
private startConnectLoop() {
98+
// if connected, trying to connect, or the internalId is already set, do nothing
8699
if (this.#connectIntervalId !== null || this.#connected) return
87100
this.debugLog(`Starting connect loop (every ${this.#connectEveryMs}ms)`)
88101

89102
this.#connectIntervalId = setInterval(
90-
this.#connectFunction,
103+
this.#retryConnection,
91104
this.#connectEveryMs,
92105
) as unknown as number
93106
}
94107

95108
private stopConnectLoop() {
109+
this.#connecting = false
96110
if (this.#connectIntervalId === null) {
97111
return
98112
}
@@ -203,7 +217,7 @@ export class EventClient<
203217
pluginId: this.#pluginId,
204218
})
205219
// start connection to event bus
206-
if (typeof CustomEvent !== 'undefined') {
220+
if (typeof CustomEvent !== 'undefined' && !this.#connecting) {
207221
this.#connectFunction()
208222
this.startConnectLoop()
209223
}

0 commit comments

Comments
 (0)