Skip to content

Commit a0433e2

Browse files
committed
feat(sdk): Allow to reset an EventFocusedCache.
This patch updates `ResetCaches` to handle the event-focused caches.
1 parent fb65e41 commit a0433e2

2 files changed

Lines changed: 79 additions & 9 deletions

File tree

  • crates/matrix-sdk/src/event_cache/caches

crates/matrix-sdk/src/event_cache/caches/event_focused/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
3232
use std::sync::Arc;
3333

34+
use eyeball_im::VectorDiff;
3435
use matrix_sdk_base::{
3536
deserialized_responses::TimelineEvent,
3637
event_cache::{Event, Gap},
@@ -90,7 +91,7 @@ pub(crate) enum EventFocusedPaginationMode {
9091
},
9192
}
9293

93-
struct EventFocusedCacheState {
94+
pub(super) struct EventFocusedCacheState {
9495
/// The room owning this event-focused cache.
9596
room: WeakRoom,
9697

@@ -503,6 +504,24 @@ impl EventFocusedCacheState {
503504

504505
Ok((result.chunk, result.next_batch_token))
505506
}
507+
508+
/// Reset this data structure as if it were brand new.
509+
///
510+
/// Return a single diff update that is a clear of all events; as a
511+
/// result, the caller may override any pending diff updates
512+
/// with the result of this function.
513+
pub fn reset(&mut self) -> Result<Vec<VectorDiff<Event>>> {
514+
self.chunk.reset();
515+
self.propagate_changes();
516+
517+
let diff_updates = self.chunk.updates_as_vector_diffs();
518+
519+
// Ensure the contract defined in the doc comment is true:
520+
debug_assert_eq!(diff_updates.len(), 1);
521+
debug_assert!(matches!(diff_updates[0], VectorDiff::Clear));
522+
523+
Ok(diff_updates)
524+
}
506525
}
507526

508527
/// A cache for an event-focused timeline.
@@ -543,6 +562,16 @@ impl EventFocusedCache {
543562
}
544563
}
545564

565+
/// Return a reference to the state.
566+
pub(super) fn state(&self) -> &Arc<RwLock<EventFocusedCacheState>> {
567+
&self.inner
568+
}
569+
570+
/// Get a reference to the _update sender_.
571+
pub(super) async fn update_sender(&self) -> EventFocusedCacheUpdateSender {
572+
self.inner.read().await.sender.clone()
573+
}
574+
546575
/// Subscribe to updates from this event-focused timeline.
547576
pub async fn subscribe(&self) -> (Vec<Event>, Receiver<TimelineVectorDiffs>) {
548577
let inner = self.inner.read().await;

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

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ pub(super) struct ResetCaches<'c> {
430430
pinned_events::PinnedEventsCacheStateLockWriteGuard<'c>,
431431
pinned_events::PinnedEventsCacheUpdateSender,
432432
)>,
433+
event_focused_lock: OwnedRwLockWriteGuard<
434+
HashMap<event_focused::EventFocusedCacheKey, event_focused::EventFocusedCache>,
435+
>,
436+
event_focused_locks: Vec<(
437+
OwnedRwLockWriteGuard<event_focused::EventFocusedCacheState>,
438+
event_focused::EventFocusedCacheUpdateSender,
439+
)>,
433440
}
434441

435442
impl<'c> ResetCaches<'c> {
@@ -459,11 +466,26 @@ impl<'c> ResetCaches<'c> {
459466
None
460467
};
461468

462-
// TODO (in next commits).
463-
let _ = event_focused;
464-
todo!();
469+
// Acquire an exclusive access to the event-focused caches.
470+
// Then, for each event-focused, acquire an exclusive access to its state.
471+
let event_focused_lock = event_focused.clone().write_owned().await;
472+
let mut event_focused_locks = Vec::new();
473+
474+
for event_focused in event_focused_lock.values() {
475+
event_focused_locks.push((
476+
event_focused.state().clone().write_owned().await,
477+
event_focused.update_sender().await,
478+
));
479+
}
465480

466-
Ok(Self { room_lock, threads_lock, thread_locks, pinned_events_lock })
481+
Ok(Self {
482+
room_lock,
483+
threads_lock,
484+
thread_locks,
485+
pinned_events_lock,
486+
event_focused_lock,
487+
event_focused_locks,
488+
})
467489
}
468490

469491
/// Reset all the event caches, and broadcast the [`TimelineVectorDiffs`].
@@ -473,7 +495,14 @@ impl<'c> ResetCaches<'c> {
473495
///
474496
/// It can fail if resetting an event cache fails.
475497
pub async fn reset_all(self) -> Result<()> {
476-
let Self { room_lock, threads_lock, thread_locks, pinned_events_lock } = self;
498+
let Self {
499+
room_lock,
500+
threads_lock,
501+
thread_locks,
502+
pinned_events_lock,
503+
event_focused_lock,
504+
event_focused_locks,
505+
} = self;
477506

478507
// Room.
479508
{
@@ -491,9 +520,7 @@ impl<'c> ResetCaches<'c> {
491520

492521
// Threads.
493522
{
494-
for thread_lock in thread_locks {
495-
let (mut thread_state, thread_update_sender) = thread_lock;
496-
523+
for (mut thread_state, thread_update_sender) in thread_locks {
497524
let updates_as_vector_diffs = thread_state.reset().await?;
498525
thread_update_sender.send(
499526
TimelineVectorDiffs {
@@ -522,6 +549,20 @@ impl<'c> ResetCaches<'c> {
522549
}
523550
}
524551

552+
// Event-focused.
553+
{
554+
for (mut event_focused_state, event_focused_update_sender) in event_focused_locks {
555+
let updates_as_vector_diffs = event_focused_state.reset()?;
556+
let _ = event_focused_update_sender.send(TimelineVectorDiffs {
557+
diffs: updates_as_vector_diffs,
558+
origin: EventsOrigin::Cache,
559+
});
560+
}
561+
562+
// Now we can release the exclusive access over the event-focused caches.
563+
drop(event_focused_lock);
564+
}
565+
525566
Ok(())
526567
}
527568
}

0 commit comments

Comments
 (0)