@@ -141,8 +141,6 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
141
141
142
142
private e2eeManager : E2EEManager | undefined ;
143
143
144
- private cachedParticipantSids : Array < string > ;
145
-
146
144
private connectionReconcileInterval ?: ReturnType < typeof setInterval > ;
147
145
148
146
private regionUrlProvider ?: RegionUrlProvider ;
@@ -153,6 +151,8 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
153
151
154
152
private log = log ;
155
153
154
+ private bufferedEvents : Array < any > = [ ] ;
155
+
156
156
/**
157
157
* Creates a new Room, the primary construct for a LiveKit session.
158
158
* @param options
@@ -161,7 +161,6 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
161
161
super ( ) ;
162
162
this . setMaxListeners ( 100 ) ;
163
163
this . participants = new Map ( ) ;
164
- this . cachedParticipantSids = [ ] ;
165
164
this . identityToSid = new Map ( ) ;
166
165
this . options = { ...roomOptionDefaults , ...options } ;
167
166
@@ -312,22 +311,16 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
312
311
if ( this . setAndEmitConnectionState ( ConnectionState . Reconnecting ) ) {
313
312
this . emit ( RoomEvent . Reconnecting ) ;
314
313
}
315
- this . cachedParticipantSids = Array . from ( this . participants . keys ( ) ) ;
316
314
} )
317
315
. on ( EngineEvent . Resumed , ( ) => {
318
316
this . setAndEmitConnectionState ( ConnectionState . Connected ) ;
319
317
this . emit ( RoomEvent . Reconnected ) ;
320
318
this . registerConnectionReconcile ( ) ;
321
319
this . updateSubscriptions ( ) ;
322
-
323
- // once reconnected, figure out if any participants connected during reconnect and emit events for it
324
- const diffParticipants = Array . from ( this . participants . values ( ) ) . filter (
325
- ( p ) => ! this . cachedParticipantSids . includes ( p . sid ) ,
326
- ) ;
327
- diffParticipants . forEach ( ( p ) => this . emit ( RoomEvent . ParticipantConnected , p ) ) ;
328
- this . cachedParticipantSids = [ ] ;
320
+ this . emitBufferedEvents ( ) ;
329
321
} )
330
322
. on ( EngineEvent . SignalResumed , ( ) => {
323
+ this . bufferedEvents = [ ] ;
331
324
if ( this . state === ConnectionState . Reconnecting ) {
332
325
this . sendSyncState ( ) ;
333
326
}
@@ -1072,6 +1065,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
1072
1065
// clear out existing remote participants, since they may have attached
1073
1066
// the old engine
1074
1067
this . participants . clear ( ) ;
1068
+ this . bufferedEvents = [ ] ;
1075
1069
this . maybeCreateEngine ( ) ;
1076
1070
}
1077
1071
@@ -1161,8 +1155,8 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
1161
1155
...this . logContext ,
1162
1156
region : joinResponse . serverRegion ,
1163
1157
} ) ;
1158
+ this . bufferedEvents = [ ] ;
1164
1159
1165
- this . cachedParticipantSids = [ ] ;
1166
1160
this . applyJoinResponse ( joinResponse ) ;
1167
1161
1168
1162
try {
@@ -1188,15 +1182,12 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
1188
1182
this . setAndEmitConnectionState ( ConnectionState . Connected ) ;
1189
1183
this . emit ( RoomEvent . Reconnected ) ;
1190
1184
this . registerConnectionReconcile ( ) ;
1191
-
1192
- // emit participant connected events after connection has been re-established
1193
- this . participants . forEach ( ( participant ) => {
1194
- this . emit ( RoomEvent . ParticipantConnected , participant ) ;
1195
- } ) ;
1185
+ this . emitBufferedEvents ( ) ;
1196
1186
} ;
1197
1187
1198
1188
private handleDisconnect ( shouldStopTracks = true , reason ?: DisconnectReason ) {
1199
1189
this . clearConnectionReconcile ( ) ;
1190
+ this . bufferedEvents = [ ] ;
1200
1191
if ( this . state === ConnectionState . Disconnected ) {
1201
1192
return ;
1202
1193
}
@@ -1707,12 +1698,22 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
1707
1698
return true ;
1708
1699
}
1709
1700
1701
+ private emitBufferedEvents ( ) {
1702
+ this . bufferedEvents . forEach ( ( [ ev , args ] ) => {
1703
+ this . emit ( ev , ...args ) ;
1704
+ } ) ;
1705
+ this . bufferedEvents = [ ] ;
1706
+ }
1707
+
1710
1708
private emitWhenConnected < E extends keyof RoomEventCallbacks > (
1711
1709
event : E ,
1712
1710
...args : Parameters < RoomEventCallbacks [ E ] >
1713
1711
) : boolean {
1714
1712
if ( this . state === ConnectionState . Connected ) {
1715
1713
return this . emit ( event , ...args ) ;
1714
+ } else if ( this . state === ConnectionState . Reconnecting ) {
1715
+ // in case the room is reconnecting, buffer the events by firing them later after emitting RoomEvent.Reconnected
1716
+ this . bufferedEvents . push ( [ event , args ] ) ;
1716
1717
}
1717
1718
return false ;
1718
1719
}
0 commit comments