Skip to content

Commit

Permalink
Dispatch updates to the UI later only after processing all of them in…
Browse files Browse the repository at this point in the history
…stead of after each one
  • Loading branch information
stefanceriu committed Aug 23, 2023
1 parent 5addf9d commit 2f05400
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,41 @@ class RoomSummaryProvider: RoomSummaryProviderProtocol {

MXLog.verbose("\(name): Received \(diffs.count) diffs, current room list \(rooms.compactMap { $0.id ?? "Empty" })")

var updatedItems = rooms
for diff in diffs {
// Special case resets in order to prevent large updates from blocking the UI
// Render the first resetDiffChunkingThreshhold as a reset and then append the rest to give the UI time to update
let resetDiffChunkingThreshhold = 50
if case .reset(let values) = diff, values.count > resetDiffChunkingThreshhold {
processDiff(RoomListEntriesUpdate.reset(values: Array(values[..<resetDiffChunkingThreshhold])))
processDiff(RoomListEntriesUpdate.append(values: Array(values.dropFirst(resetDiffChunkingThreshhold))))
// Special case resets in order to prevent large updates from blocking the UI
// Render the first resetDiffChunkingThreshhold as a reset and then append the rest to give the UI time to update
updatedItems = processDiff(.reset(values: Array(values[..<resetDiffChunkingThreshhold])), on: updatedItems)

// Once a reset is chunked dispatch the first part to the UI for rendering
rooms = updatedItems

updatedItems = processDiff(.append(values: Array(values.dropFirst(resetDiffChunkingThreshhold))), on: updatedItems)
} else {
processDiff(diff)
updatedItems = processDiff(diff, on: updatedItems)
}
}
rooms = updatedItems

detectDuplicatesInRoomList(rooms)

MXLog.verbose("\(name): Finished applying \(diffs.count) diffs, new room list \(rooms.compactMap { $0.id ?? "Empty" })")
}

private func processDiff(_ diff: RoomListEntriesUpdate) {
guard let collectionDiff = buildDiff(from: diff, on: rooms) else {
private func processDiff(_ diff: RoomListEntriesUpdate, on currentItems: [RoomSummary]) -> [RoomSummary] {
guard let collectionDiff = buildDiff(from: diff, on: currentItems) else {
MXLog.error("\(name): Failed building CollectionDifference from \(diff)")
return
return currentItems
}

guard let updatedItems = rooms.applying(collectionDiff) else {
guard let updatedItems = currentItems.applying(collectionDiff) else {
MXLog.error("\(name): Failed applying diff: \(collectionDiff)")
return
return currentItems
}

rooms = updatedItems
return updatedItems
}

private func fetchRoomInfo(roomListItem: RoomListItemProtocol) -> RoomInfo? {
Expand Down

0 comments on commit 2f05400

Please sign in to comment.