@@ -82,6 +82,8 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
8282 private byteStreamHandlers = new Map < string , ByteStreamHandler > ( ) ;
8383 private textStreamHandlers = new Map < string , TextStreamHandler > ( ) ;
8484
85+ private preConnectEvents : FfiEvent [ ] = [ ] ;
86+
8587 e2eeManager ?: E2EEManager ;
8688 connectionState : ConnectionState = ConnectionState . CONN_DISCONNECTED ;
8789
@@ -180,6 +182,8 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
180182 options,
181183 } ) ;
182184
185+ FfiClient . instance . on ( FfiClientEvent . FfiEvent , this . onFfiEvent ) ;
186+
183187 const res = FfiClient . instance . request < ConnectResponse > ( {
184188 message : {
185189 case : 'connect' ,
@@ -210,8 +214,6 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
210214 rp . trackPublications . set ( publication . sid ! , publication ) ;
211215 }
212216 }
213-
214- FfiClient . instance . on ( FfiClientEvent . FfiEvent , this . onFfiEvent ) ;
215217 break ;
216218 case 'error' :
217219 default :
@@ -279,7 +281,22 @@ export class Room extends (EventEmitter as new () => TypedEmitter<RoomCallbacks>
279281
280282 private onFfiEvent = ( ffiEvent : FfiEvent ) => {
281283 if ( ! this . localParticipant || ! this . ffiHandle || ! this . info ) {
282- throw TypeError ( 'cannot handle ffi events before connectCallback' ) ;
284+ this . preConnectEvents . push ( ffiEvent ) ;
285+ return ;
286+ }
287+
288+ // process preConnectEvents if we received the connectCallback after the events were queued
289+ for ( const ev of this . preConnectEvents ) {
290+ this . processFfiEvent ( ev ) ;
291+ }
292+ this . preConnectEvents = [ ] ;
293+
294+ this . processFfiEvent ( ffiEvent ) ;
295+ } ;
296+
297+ private processFfiEvent = ( ffiEvent : FfiEvent ) => {
298+ if ( ! this . localParticipant || ! this . ffiHandle || ! this . info ) {
299+ throw new Error ( 'processFfiEvent called before connect' ) ;
283300 }
284301
285302 if ( ffiEvent . message . case == 'rpcMethodInvocation' ) {
0 commit comments