@@ -206,34 +206,37 @@ export function createWsRpcClientMode(
206206 for ( const name of connectionMeta . jsonSerializableMethods ?? [ ] )
207207 definitions . set ( name , { jsonSerializable : true } )
208208
209+ // Hoisted out of the `createRpcClient` call so `close()` below can reach it — birpc's own
210+ // `ChannelOptions` carries no reference back to what it was built from.
211+ const channel = createWsRpcChannel ( {
212+ url,
213+ authToken,
214+ definitions,
215+ ...wsOptions ,
216+ onConnected ( event ) {
217+ // Socket open — the trust handshake (already queued) settles the
218+ // status to `connected`/`unauthorized`. Stay `connecting` until then.
219+ wsOptions . onConnected ?.( event )
220+ } ,
221+ onError ( error ) {
222+ setStatus ( 'error' , error )
223+ events . emit ( 'connection:error' , error )
224+ rejectAllPending ( new DevframeConnectionError ( 'connection' , '[devframe] Connection to the devframe server failed' , { cause : error } ) )
225+ wsOptions . onError ?.( error )
226+ } ,
227+ onDisconnected ( event ) {
228+ // A clean close after we were connected, or a socket that never
229+ // opened — either way calls can no longer be served.
230+ if ( status !== 'error' )
231+ setStatus ( 'disconnected' )
232+ rejectAllPending ( new DevframeConnectionError ( 'connection' , '[devframe] Disconnected from the devframe server' , { cause : connectionError ?? undefined } ) )
233+ wsOptions . onDisconnected ?.( event )
234+ } ,
235+ } )
209236 const serverRpc = createRpcClient < DevframeRpcServerFunctions , DevframeRpcClientFunctions > (
210237 clientRpc . functions ,
211238 {
212- channel : createWsRpcChannel ( {
213- url,
214- authToken,
215- definitions,
216- ...wsOptions ,
217- onConnected ( event ) {
218- // Socket open — the trust handshake (already queued) settles the
219- // status to `connected`/`unauthorized`. Stay `connecting` until then.
220- wsOptions . onConnected ?.( event )
221- } ,
222- onError ( error ) {
223- setStatus ( 'error' , error )
224- events . emit ( 'connection:error' , error )
225- rejectAllPending ( new DevframeConnectionError ( 'connection' , '[devframe] Connection to the devframe server failed' , { cause : error } ) )
226- wsOptions . onError ?.( error )
227- } ,
228- onDisconnected ( event ) {
229- // A clean close after we were connected, or a socket that never
230- // opened — either way calls can no longer be served.
231- if ( status !== 'error' )
232- setStatus ( 'disconnected' )
233- rejectAllPending ( new DevframeConnectionError ( 'connection' , '[devframe] Disconnected from the devframe server' , { cause : connectionError ?? undefined } ) )
234- wsOptions . onDisconnected ?.( event )
235- } ,
236- } ) ,
239+ channel,
237240 rpcOptions,
238241 } ,
239242 )
@@ -402,5 +405,8 @@ export function createWsRpcClientMode(
402405 method ,
403406 )
404407 } ,
408+ close : ( ) => {
409+ channel . close ( )
410+ } ,
405411 }
406412}
0 commit comments