Skip to content

Commit a3238cd

Browse files
committed
refactor(event cache): remove indent in RoomEventCacheState::handle_sync
1 parent a884b2c commit a3238cd

File tree

1 file changed

+54
-55
lines changed
  • crates/matrix-sdk/src/event_cache/room

1 file changed

+54
-55
lines changed

crates/matrix-sdk/src/event_cache/room/mod.rs

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,73 +1344,72 @@ mod private {
13441344
prev_batch = None;
13451345
}
13461346

1347+
if all_duplicates {
1348+
// No new events and no gap (per the previous check), thus no need to change the
1349+
// room state. We're done!
1350+
return Ok(false);
1351+
}
1352+
13471353
// During a sync, when a duplicated event is found, the old event is removed and
13481354
// the new event is added.
13491355
//
13501356
// Let's remove the old events that are duplicated.
1351-
let timeline_event_diffs = if all_duplicates {
1352-
// No new events, thus no need to change the room events.
1353-
vec![]
1354-
} else {
1355-
// Remove the old duplicated events.
1356-
//
1357-
// We don't have to worry the removals can change the position of the
1358-
// existing events, because we are pushing all _new_
1359-
// `events` at the back.
1360-
let mut timeline_event_diffs = self
1361-
.remove_events(in_memory_duplicated_event_ids, in_store_duplicated_event_ids)
1362-
.await?;
13631357

1364-
// Add the previous back-pagination token (if present), followed by the timeline
1365-
// events themselves.
1366-
let new_timeline_event_diffs = self
1367-
.with_events_mut(true, |room_events| {
1368-
// If we only received duplicated events, we don't need to store the gap: if
1369-
// there was a gap, we'd have received an unknown event at the tail of
1370-
// the room's timeline (unless the server reordered sync events since the
1371-
// last time we sync'd).
1372-
if let Some(prev_token) = &prev_batch {
1373-
// As a tiny optimization: remove the last chunk if it's an empty event
1374-
// one, as it's not useful to keep it before a gap.
1375-
let prev_chunk_to_remove =
1376-
room_events.rchunks().next().and_then(|chunk| {
1377-
(chunk.is_items() && chunk.num_items() == 0)
1378-
.then_some(chunk.identifier())
1379-
});
1380-
1381-
room_events.push_gap(Gap { prev_token: prev_token.clone() });
1382-
1383-
if let Some(prev_chunk_to_remove) = prev_chunk_to_remove {
1384-
room_events.remove_empty_chunk_at(prev_chunk_to_remove).expect(
1358+
// Remove the old duplicated events.
1359+
//
1360+
// We don't have to worry the removals can change the position of the
1361+
// existing events, because we are pushing all _new_
1362+
// `events` at the back.
1363+
let mut timeline_event_diffs = self
1364+
.remove_events(in_memory_duplicated_event_ids, in_store_duplicated_event_ids)
1365+
.await?;
1366+
1367+
// Add the previous back-pagination token (if present), followed by the timeline
1368+
// events themselves.
1369+
let new_timeline_event_diffs = self
1370+
.with_events_mut(true, |room_events| {
1371+
// If we only received duplicated events, we don't need to store the gap: if
1372+
// there was a gap, we'd have received an unknown event at the tail of
1373+
// the room's timeline (unless the server reordered sync events since the
1374+
// last time we sync'd).
1375+
if let Some(prev_token) = &prev_batch {
1376+
// As a tiny optimization: remove the last chunk if it's an empty event
1377+
// one, as it's not useful to keep it before a gap.
1378+
let prev_chunk_to_remove = room_events.rchunks().next().and_then(|chunk| {
1379+
(chunk.is_items() && chunk.num_items() == 0)
1380+
.then_some(chunk.identifier())
1381+
});
1382+
1383+
room_events.push_gap(Gap { prev_token: prev_token.clone() });
1384+
1385+
if let Some(prev_chunk_to_remove) = prev_chunk_to_remove {
1386+
room_events.remove_empty_chunk_at(prev_chunk_to_remove).expect(
13851387
"we just checked the chunk is there, and it's an empty item chunk",
13861388
);
1387-
}
13881389
}
1390+
}
13891391

1390-
room_events.push_events(events.clone());
1392+
room_events.push_events(events.clone());
13911393

1392-
events.clone()
1393-
})
1394-
.await?;
1394+
events.clone()
1395+
})
1396+
.await?;
13951397

1396-
timeline_event_diffs.extend(new_timeline_event_diffs);
1397-
1398-
if timeline.limited && prev_batch.is_some() {
1399-
// If there was a previous batch token for a limited timeline, unload the chunks
1400-
// so it only contains the last one; otherwise, there might be a
1401-
// valid gap in between, and observers may not render it (yet).
1402-
//
1403-
// We must do this *after* the above call to `.with_events_mut`, so the new
1404-
// events and gaps are properly persisted to storage.
1405-
if let Some(diffs) = self.shrink_to_last_chunk().await? {
1406-
// Override the diffs with the new ones, as per `shrink_to_last_chunk`'s API
1407-
// contract.
1408-
timeline_event_diffs = diffs;
1409-
}
1410-
}
1398+
timeline_event_diffs.extend(new_timeline_event_diffs);
14111399

1412-
timeline_event_diffs
1413-
};
1400+
if timeline.limited && prev_batch.is_some() {
1401+
// If there was a previous batch token for a limited timeline, unload the chunks
1402+
// so it only contains the last one; otherwise, there might be a
1403+
// valid gap in between, and observers may not render it (yet).
1404+
//
1405+
// We must do this *after* the above call to `.with_events_mut`, so the new
1406+
// events and gaps are properly persisted to storage.
1407+
if let Some(diffs) = self.shrink_to_last_chunk().await? {
1408+
// Override the diffs with the new ones, as per `shrink_to_last_chunk`'s API
1409+
// contract.
1410+
timeline_event_diffs = diffs;
1411+
}
1412+
}
14141413

14151414
if !timeline_event_diffs.is_empty() {
14161415
let _ = sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {

0 commit comments

Comments
 (0)