| 64 | } |
| 65 | |
| 66 | export class ClientEventBus { |
| 67 | #port: number |
| 68 | #host: string |
| 69 | #protocol: 'http' | 'https' |
| 70 | #socket: WebSocket | null |
| 71 | #eventSource: EventSource | null |
| 72 | #eventTarget: EventTarget |
| 73 | #debug: boolean |
| 74 | #connectToServerBus: boolean |
| 75 | #broadcastChannel: BroadcastChannel | null |
| 76 | // Events emitted while the WebSocket is still establishing its connection. |
| 77 | // They are buffered here and flushed once the socket opens so early events |
| 78 | // (e.g. the marketplace's `mounted` request) are never silently dropped. |
| 79 | #pendingServerEvents: Array<string> = [] |
| 80 | #dispatcher = (e: Event) => { |
| 81 | const event = (e as CustomEvent).detail |
| 82 | this.emitToServer(event) |
| 83 | this.emitToClients(event) |
| 84 | } |
| 85 | #connectFunction = () => { |
| 86 | this.debugLog( |
| 87 | 'Connection request made to event-bus, replying back with success', |
| 88 | ) |
| 89 | this.#eventTarget.dispatchEvent(new CustomEvent('tanstack-connect-success')) |
| 90 | } |
| 91 | constructor({ |
| 92 | port = 4206, |
| 93 | host = 'localhost', |
| 94 | protocol = 'http', |
| 95 | debug = false, |
| 96 | connectToServerBus = false, |
| 97 | }: ClientEventBusConfig = {}) { |
| 98 | this.#debug = debug |
| 99 | this.#broadcastChannel = new BroadcastChannel('tanstack-devtools') |
| 100 | this.#eventSource = null |
| 101 | this.#port = getDefaultPort(port) |
| 102 | this.#host = getDefaultHost(host) |
| 103 | this.#protocol = getDefaultProtocol(protocol) |
| 104 | this.#socket = null |
| 105 | this.#connectToServerBus = connectToServerBus |
| 106 | this.#eventTarget = this.getGlobalTarget() |
| 107 | this.#broadcastChannel.onmessage = (e) => { |
| 108 | this.emitToClients(parseWithBigInt(e.data), true) |
| 109 | } |
| 110 | this.debugLog('Initializing client event bus') |
| 111 | } |
| 112 | |
| 113 | private emitToClients( |
| 114 | event: TanStackDevtoolsEvent<string>, |
| 115 | fromBroadcastChannel = false, |
| 116 | ) { |
| 117 | this.debugLog('Emitting event from client bus', event) |
| 118 | const specificEvent = new CustomEvent(event.type, { detail: event }) |
| 119 | this.debugLog('Emitting event to specific client listeners', event) |
| 120 | this.#eventTarget.dispatchEvent(specificEvent) |
| 121 | const globalEvent = new CustomEvent('tanstack-devtools-global', { |
| 122 | detail: event, |
| 123 | }) |
nothing calls this directly
no test coverage detected