@@ -123,39 +123,18 @@ type ReceiveConnectionsOptions = Omit<Options, 'label'> &
123123 * @param options.handler - The function to handle internal messages. Mutually exclusive
124124 * with `handlerPromise`.
125125 * @param options.handlerPromise - A promise that resolves to the handler function.
126- * Incoming messages await handler initialization once, then the resolved handler is
127- * cached and used directly for all subsequent messages. Mutually exclusive with `handler`.
126+ * Mutually exclusive with `handler`.
128127 * @param options.logger - The logger instance.
129128 * @param options.controlChannelName - The name of the control channel. Must match
130129 * the name used by {@link connectToKernel} on the other end.
131130 */
132131export const receiveInternalConnections = ( {
133- handler : directHandler ,
132+ handler,
134133 handlerPromise,
135134 logger,
136135 controlChannelName = COMMS_CONTROL_CHANNEL_NAME ,
137136} : ReceiveConnectionsOptions ) : void => {
138- // Support both direct handler and promise-based handler
139- let handler : HandleInternalMessage | null = null ;
140- let handlerResolution : Promise < HandleInternalMessage > ;
141-
142- if ( directHandler !== undefined ) {
143- // Direct handler - use immediately
144- handler = directHandler ;
145- handlerResolution = Promise . resolve ( directHandler ) ;
146- } else {
147- // Promise-based handler - cache once resolved
148- handlerResolution = handlerPromise . then (
149- ( resolvedHandler ) => {
150- handler = resolvedHandler ;
151- return resolvedHandler ;
152- } ,
153- ( error ) => {
154- // Re-throw to propagate initialization errors to message handlers
155- throw error ;
156- } ,
157- ) ;
158- }
137+ const handlerResolution = handler ? Promise . resolve ( handler ) : handlerPromise ;
159138
160139 const seenChannels = new Set < string > ( ) ;
161140 new BroadcastChannel ( controlChannelName ) . onmessage = ( event ) => {
@@ -183,39 +162,18 @@ export const receiveInternalConnections = ({
183162 `Received message from internal process "${ channelName } ": ${ JSON . stringify ( message ) } ` ,
184163 ) ;
185164
186- try {
187- // Use cached handler if available, otherwise await once
188- const messageHandler = handler ?? ( await handlerResolution ) ;
189- const reply = await messageHandler ( message ) ;
190- if ( reply !== undefined ) {
191- await kernelRpcStream . write ( reply ) ;
192- }
193- } catch ( error ) {
194- // Check if this is a handler initialization error
195- if ( handler === null ) {
196- logger . error (
197- `Error initializing message handler for internal process "${ channelName } ":` ,
198- error ,
199- ) ;
200- } else {
201- logger . error (
202- `Error handling message from internal process "${ channelName } ":` ,
203- error ,
204- ) ;
205- }
206- throw error ;
165+ const messageHandler = await handlerResolution ;
166+ const reply = await messageHandler ( message ) ;
167+ if ( reply !== undefined ) {
168+ await kernelRpcStream . write ( reply ) ;
207169 }
208170 } ) ;
209171 } )
210172 . catch ( ( error ) => {
211- // This catch handles connection errors and re-thrown handler errors
212- if ( handler !== null ) {
213- logger . error (
214- `Error handling message from internal process "${ channelName } ":` ,
215- error ,
216- ) ;
217- }
218- // Initialization errors are already logged in the try-catch above
173+ logger . error (
174+ `Error handling message from internal process "${ channelName } ":` ,
175+ error ,
176+ ) ;
219177 } )
220178 . finally ( ( ) => {
221179 logger . debug ( `Closed connection to internal process "${ channelName } "` ) ;
0 commit comments