Skip to content

Commit

Permalink
refactor(event cache): make it clearer that vecdiff updates must be h…
Browse files Browse the repository at this point in the history
…andled with `with_events_mut`

Every caller to `with_events_mut` must propagate the vector diff
updates, otherwise updates would be missing to the room event cache's
observers. This slightly tweaks the signature to make this a bit
clearer, and adjusts the code comment as well.
  • Loading branch information
bnjbvr committed Jan 6, 2025
1 parent c503583 commit 1480fad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
20 changes: 9 additions & 11 deletions crates/matrix-sdk/src/event_cache/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl RoomPagination {

let new_gap = paginator.prev_batch_token().map(|prev_token| Gap { prev_token });

let result = state
let (backpagination_outcome, updates_as_vector_diffs) = state
.with_events_mut(move |room_events| {
// Note: The chunk could be empty.
//
Expand Down Expand Up @@ -227,20 +227,18 @@ impl RoomPagination {
debug!("not storing previous batch token, because we deduplicated all new back-paginated events");
}

let sync_timeline_events_diffs = room_events.updates_as_vector_diffs();

if !sync_timeline_events_diffs.is_empty() {
let _ = self.inner.sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
diffs: sync_timeline_events_diffs,
origin: EventsOrigin::Sync,
});
}

BackPaginationOutcome { events, reached_start }
})
.await?;

Ok(Some(result))
if !updates_as_vector_diffs.is_empty() {
let _ = self.inner.sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
diffs: updates_as_vector_diffs,
origin: EventsOrigin::Sync,
});
}

Ok(Some(backpagination_outcome))
}

/// Get the latest pagination token, as stored in the room events linked
Expand Down
1 change: 0 additions & 1 deletion crates/matrix-sdk/src/event_cache/room/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ impl RoomEvents {
/// See [`AsVector`] to learn more.
///
/// [`Update`]: matrix_sdk_base::linked_chunk::Update
#[allow(unused)] // gonna be useful very soon! but we need it now for test purposes
pub fn updates_as_vector_diffs(&mut self) -> Vec<VectorDiff<Event>> {
self.chunks_updates_as_vectordiffs.take()
}
Expand Down
14 changes: 9 additions & 5 deletions crates/matrix-sdk/src/event_cache/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl RoomEventCacheInner {
// Add the previous back-pagination token (if present), followed by the timeline
// events themselves.
let sync_timeline_events_diffs = {
let sync_timeline_events_diffs = state
let (_, sync_timeline_events_diffs) = state
.with_events_mut(|room_events| {
if let Some(prev_token) = &prev_batch {
room_events.push_gap(Gap { prev_token: prev_token.clone() });
Expand All @@ -556,8 +556,6 @@ impl RoomEventCacheInner {
.replace_gap_at([], prev_gap_id)
.expect("we obtained the valid position beforehand");
}

room_events.updates_as_vector_diffs()
})
.await?;

Expand Down Expand Up @@ -641,6 +639,7 @@ fn chunk_debug_string(content: &ChunkContent<SyncTimelineEvent, Gap>) -> String
mod private {
use std::sync::Arc;

use eyeball_im::VectorDiff;
use matrix_sdk_base::{
deserialized_responses::{SyncTimelineEvent, TimelineEventKind},
event_cache::{
Expand Down Expand Up @@ -846,13 +845,18 @@ mod private {

/// Gives a temporary mutable handle to the underlying in-memory events,
/// and will propagate changes to the storage once done.
///
/// Returns the output of the given callback, as well as updates to the
/// linked chunk, as vector diff, so the caller may propagate
/// such updates, if needs be.
pub async fn with_events_mut<O, F: FnOnce(&mut RoomEvents) -> O>(
&mut self,
func: F,
) -> Result<O, EventCacheError> {
) -> Result<(O, Vec<VectorDiff<SyncTimelineEvent>>), EventCacheError> {
let output = func(&mut self.events);
self.propagate_changes().await?;
Ok(output)
let updates_as_vector_diffs = self.events.updates_as_vector_diffs();
Ok((output, updates_as_vector_diffs))
}
}

Expand Down

0 comments on commit 1480fad

Please sign in to comment.