MCPcopy Index your code
hub / github.com/TanStack/devtools / wireRuntimeBridgeChannels

Function wireRuntimeBridgeChannels

packages/devtools-vite/src/runtime-bridge.ts:76–113  ·  view source on GitHub ↗
(
  server: BridgeServerLike,
  getTarget: () => EventTarget | null | undefined,
)

Source from the content-addressed store, hash-verified

74}
75
76export function wireRuntimeBridgeChannels(
77 server: BridgeServerLike,
78 getTarget: () => EventTarget | null | undefined,
79): () => void {
80 const teardowns: Array<() => void> = []
81
82 for (const [name, env] of Object.entries(server.environments)) {
83 if (name === 'client') continue
84 const hot = env?.hot
85 if (
86 !hot ||
87 typeof hot.on !== 'function' ||
88 typeof hot.send !== 'function'
89 ) {
90 continue
91 }
92
93 // Worker -> ServerEventBus (broadcasts to browser + in-process listeners).
94 const onToServer = (event: any) => {
95 getTarget()?.dispatchEvent(
96 new CustomEvent('tanstack-dispatch-event', { detail: event }),
97 )
98 }
99 hot.on('tsd:to-server', onToServer)
100 teardowns.push(() => hot.off?.('tsd:to-server', onToServer))
101
102 // ServerEventBus output -> worker listeners.
103 const target = getTarget()
104 const forward = (e: Event) =>
105 hot.send!('tsd:to-client', (e as CustomEvent).detail)
106 target?.addEventListener('tanstack-devtools-global', forward)
107 teardowns.push(() =>
108 target?.removeEventListener('tanstack-devtools-global', forward),
109 )
110 }
111
112 return () => teardowns.forEach((off) => off())
113}

Callers 2

configureServerFunction · 0.90

Calls 1

onMethod · 0.45

Tested by

no test coverage detected