| 5 | } |
| 6 | |
| 7 | export class EventClient<TEventMap extends Record<string, any>> { |
| 8 | #enabled = true |
| 9 | #pluginId: string |
| 10 | #eventTarget: () => EventTarget |
| 11 | #debug: boolean |
| 12 | #queuedEvents: Array<TanStackDevtoolsEvent<string, any>> |
| 13 | #connected: boolean |
| 14 | #connectIntervalId: number | null |
| 15 | #connectEveryMs: number |
| 16 | #retryCount = 0 |
| 17 | #maxRetries = 5 |
| 18 | #connecting = false |
| 19 | #failedToConnect = false |
| 20 | #internalEventTarget: EventTarget | null = null |
| 21 | |
| 22 | #onConnected = () => { |
| 23 | this.debugLog('Connected to event bus') |
| 24 | this.#connected = true |
| 25 | this.#connecting = false |
| 26 | this.debugLog('Emitting queued events', this.#queuedEvents) |
| 27 | this.#queuedEvents.forEach((event) => this.emitEventToBus(event)) |
| 28 | this.#queuedEvents = [] |
| 29 | this.stopConnectLoop() |
| 30 | this.#eventTarget().removeEventListener( |
| 31 | 'tanstack-connect-success', |
| 32 | this.#onConnected, |
| 33 | ) |
| 34 | } |
| 35 | // fired off right away and then at intervals |
| 36 | #retryConnection = () => { |
| 37 | if (this.#retryCount < this.#maxRetries) { |
| 38 | this.#retryCount++ |
| 39 | this.dispatchCustomEvent('tanstack-connect', {}) |
| 40 | |
| 41 | return |
| 42 | } |
| 43 | this.#eventTarget().removeEventListener( |
| 44 | 'tanstack-connect', |
| 45 | this.#retryConnection, |
| 46 | ) |
| 47 | this.#failedToConnect = true |
| 48 | this.debugLog('Max retries reached, giving up on connection') |
| 49 | this.stopConnectLoop() |
| 50 | } |
| 51 | |
| 52 | // This is run to register connection handlers on first emit attempt |
| 53 | #connectFunction = () => { |
| 54 | if (this.#connecting) return |
| 55 | this.#connecting = true |
| 56 | this.#eventTarget().addEventListener( |
| 57 | 'tanstack-connect-success', |
| 58 | this.#onConnected, |
| 59 | ) |
| 60 | this.#retryConnection() |
| 61 | } |
| 62 | |
| 63 | constructor({ |
| 64 | pluginId, |
nothing calls this directly
no test coverage detected