@@ -187,6 +187,8 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
187187
188188 private e2eeManager : BaseE2EEManager | undefined ;
189189
190+ private e2eeStateMutex : Mutex = new Mutex ( ) ;
191+
190192 private connectionReconcileInterval ?: ReturnType < typeof setInterval > ;
191193
192194 private regionUrlProvider ?: RegionUrlProvider ;
@@ -293,7 +295,6 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
293295 } ) ;
294296
295297 this . disconnectLock = new Mutex ( ) ;
296-
297298 this . localParticipant = new LocalParticipant (
298299 '' ,
299300 '' ,
@@ -411,13 +412,21 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
411412 * @experimental
412413 */
413414 async setE2EEEnabled ( enabled : boolean ) {
414- if ( this . e2eeManager ) {
415- await Promise . all ( [ this . localParticipant . setE2EEEnabled ( enabled ) ] ) ;
416- if ( this . localParticipant . identity !== '' ) {
417- this . e2eeManager . setParticipantCryptorEnabled ( enabled , this . localParticipant . identity ) ;
415+ const unlock = await this . e2eeStateMutex . lock ( ) ;
416+ try {
417+ if ( this . e2eeManager ) {
418+ if ( this . isE2EEEnabled !== enabled ) {
419+ await this . localParticipant . setE2EEEnabled ( enabled ) ;
420+
421+ if ( this . localParticipant . identity !== '' ) {
422+ this . e2eeManager . setParticipantCryptorEnabled ( enabled , this . localParticipant . identity ) ;
423+ }
424+ }
425+ } else {
426+ throw Error ( 'e2ee not configured, please set e2ee settings within the room options' ) ;
418427 }
419- } else {
420- throw Error ( 'e2ee not configured, please set e2ee settings within the room options' ) ;
428+ } finally {
429+ unlock ( ) ;
421430 }
422431 }
423432
@@ -450,6 +459,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
450459 this . emit ( RoomEvent . EncryptionError , error , participant ) ;
451460 } ) ;
452461 this . e2eeManager ?. setup ( this ) ;
462+ this . e2eeManager ?. setupEngine ( this . engine ) ;
453463 }
454464 }
455465
0 commit comments