@@ -179,45 +179,51 @@ export class ImportanceAlgorithm extends OrderingAlgorithm {
179179 }
180180
181181 public async handleRoomUpdate ( room : Room , cause : RoomUpdateCause ) : Promise < boolean > {
182- if ( cause === RoomUpdateCause . NewRoom || cause === RoomUpdateCause . RoomRemoved ) {
183- return this . handleSplice ( room , cause ) ;
184- }
182+ try {
183+ await this . updateLock . acquireAsync ( ) ;
185184
186- if ( cause !== RoomUpdateCause . Timeline && cause !== RoomUpdateCause . ReadReceipt ) {
187- throw new Error ( `Unsupported update cause: ${ cause } ` ) ;
188- }
185+ if ( cause === RoomUpdateCause . NewRoom || cause === RoomUpdateCause . RoomRemoved ) {
186+ return this . handleSplice ( room , cause ) ;
187+ }
189188
190- const category = this . getRoomCategory ( room ) ;
191- if ( this . sortingAlgorithm === SortAlgorithm . Manual ) {
192- return ; // Nothing to do here.
193- }
189+ if ( cause !== RoomUpdateCause . Timeline && cause !== RoomUpdateCause . ReadReceipt ) {
190+ throw new Error ( `Unsupported update cause: ${ cause } ` ) ;
191+ }
194192
195- const roomIdx = this . getRoomIndex ( room ) ;
196- if ( roomIdx === - 1 ) {
197- throw new Error ( `Room ${ room . roomId } has no index in ${ this . tagId } ` ) ;
198- }
193+ const category = this . getRoomCategory ( room ) ;
194+ if ( this . sortingAlgorithm === SortAlgorithm . Manual ) {
195+ return ; // Nothing to do here.
196+ }
199197
200- // Try to avoid doing array operations if we don't have to: only move rooms within
201- // the categories if we're jumping categories
202- const oldCategory = this . getCategoryFromIndices ( roomIdx , this . indices ) ;
203- if ( oldCategory !== category ) {
204- // Move the room and update the indices
205- this . moveRoomIndexes ( 1 , oldCategory , category , this . indices ) ;
206- this . cachedOrderedRooms . splice ( roomIdx , 1 ) ; // splice out the old index (fixed position)
207- this . cachedOrderedRooms . splice ( this . indices [ category ] , 0 , room ) ; // splice in the new room (pre-adjusted)
208- // Note: if moveRoomIndexes() is called after the splice then the insert operation
209- // will happen in the wrong place. Because we would have already adjusted the index
210- // for the category, we don't need to determine how the room is moving in the list.
211- // If we instead tried to insert before updating the indices, we'd have to determine
212- // whether the room was moving later (towards IDLE) or earlier (towards RED) from its
213- // current position, as it'll affect the category's start index after we remove the
214- // room from the array.
215- }
198+ const roomIdx = this . getRoomIndex ( room ) ;
199+ if ( roomIdx === - 1 ) {
200+ throw new Error ( `Room ${ room . roomId } has no index in ${ this . tagId } ` ) ;
201+ }
216202
217- // Sort the category now that we've dumped the room in
218- await this . sortCategory ( category ) ;
203+ // Try to avoid doing array operations if we don't have to: only move rooms within
204+ // the categories if we're jumping categories
205+ const oldCategory = this . getCategoryFromIndices ( roomIdx , this . indices ) ;
206+ if ( oldCategory !== category ) {
207+ // Move the room and update the indices
208+ this . moveRoomIndexes ( 1 , oldCategory , category , this . indices ) ;
209+ this . cachedOrderedRooms . splice ( roomIdx , 1 ) ; // splice out the old index (fixed position)
210+ this . cachedOrderedRooms . splice ( this . indices [ category ] , 0 , room ) ; // splice in the new room (pre-adjusted)
211+ // Note: if moveRoomIndexes() is called after the splice then the insert operation
212+ // will happen in the wrong place. Because we would have already adjusted the index
213+ // for the category, we don't need to determine how the room is moving in the list.
214+ // If we instead tried to insert before updating the indices, we'd have to determine
215+ // whether the room was moving later (towards IDLE) or earlier (towards RED) from its
216+ // current position, as it'll affect the category's start index after we remove the
217+ // room from the array.
218+ }
219219
220- return true ; // change made
220+ // Sort the category now that we've dumped the room in
221+ await this . sortCategory ( category ) ;
222+
223+ return true ; // change made
224+ } finally {
225+ await this . updateLock . release ( ) ;
226+ }
221227 }
222228
223229 private async sortCategory ( category : Category ) {
0 commit comments