Skip to content

Commit 6a81cec

Browse files
committed
event cache: move store trait and memory impl to a new store file
1 parent ffc7648 commit 6a81cec

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

crates/matrix-sdk-ui/src/event_cache.rs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
use std::{collections::BTreeMap, fmt::Debug, sync::Arc};
4444

45-
use async_trait::async_trait;
4645
use matrix_sdk::{sync::RoomUpdate, Client, Room};
4746
use matrix_sdk_base::{
4847
deserialized_responses::{AmbiguityChange, SyncTimelineEvent},
@@ -55,14 +54,15 @@ use ruma::{
5554
};
5655
use tokio::{
5756
spawn,
58-
sync::{
59-
broadcast::{error::RecvError, Receiver, Sender},
60-
RwLock,
61-
},
57+
sync::broadcast::{error::RecvError, Receiver, Sender},
6258
task::JoinHandle,
6359
};
6460
use tracing::{debug, error, trace};
6561

62+
use self::store::{EventCacheStore, MemoryStore};
63+
64+
mod store;
65+
6666
/// An error observed in the [`EventCache`].
6767
#[derive(thiserror::Error, Debug)]
6868
pub enum EventCacheError {
@@ -143,51 +143,6 @@ impl EventCache {
143143
}
144144
}
145145

146-
/// A store that can be remember information about the event cache.
147-
///
148-
/// It really acts as a cache, in the sense that clearing the backing data
149-
/// should not have any irremediable effect, other than providing a lesser user
150-
/// experience.
151-
#[async_trait]
152-
pub trait EventCacheStore: Send + Sync {
153-
/// Returns all the known events for the given room.
154-
async fn room_events(&self, room: &RoomId) -> Result<Vec<SyncTimelineEvent>>;
155-
156-
/// Adds all the events to the given room.
157-
async fn add_room_events(&self, room: &RoomId, events: Vec<SyncTimelineEvent>) -> Result<()>;
158-
159-
/// Clear all the events from the given room.
160-
async fn clear_room_events(&self, room: &RoomId) -> Result<()>;
161-
}
162-
163-
struct MemoryStore {
164-
/// All the events per room, in sync order.
165-
by_room: RwLock<BTreeMap<OwnedRoomId, Vec<SyncTimelineEvent>>>,
166-
}
167-
168-
impl MemoryStore {
169-
fn new() -> Self {
170-
Self { by_room: Default::default() }
171-
}
172-
}
173-
174-
#[async_trait]
175-
impl EventCacheStore for MemoryStore {
176-
async fn room_events(&self, room: &RoomId) -> Result<Vec<SyncTimelineEvent>> {
177-
Ok(self.by_room.read().await.get(room).cloned().unwrap_or_default())
178-
}
179-
180-
async fn add_room_events(&self, room: &RoomId, events: Vec<SyncTimelineEvent>) -> Result<()> {
181-
self.by_room.write().await.entry(room.to_owned()).or_default().extend(events);
182-
Ok(())
183-
}
184-
185-
async fn clear_room_events(&self, room: &RoomId) -> Result<()> {
186-
let _ = self.by_room.write().await.remove(room);
187-
Ok(())
188-
}
189-
}
190-
191146
/// A subset of an event cache, for a room.
192147
///
193148
/// Cloning is shallow, and thus is cheap to do.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::collections::BTreeMap;
2+
3+
use async_trait::async_trait;
4+
use matrix_sdk_base::deserialized_responses::SyncTimelineEvent;
5+
use ruma::{OwnedRoomId, RoomId};
6+
use tokio::sync::RwLock;
7+
8+
use super::Result;
9+
10+
/// A store that can be remember information about the event cache.
11+
///
12+
/// It really acts as a cache, in the sense that clearing the backing data
13+
/// should not have any irremediable effect, other than providing a lesser user
14+
/// experience.
15+
#[async_trait]
16+
pub trait EventCacheStore: Send + Sync {
17+
/// Returns all the known events for the given room.
18+
async fn room_events(&self, room: &RoomId) -> Result<Vec<SyncTimelineEvent>>;
19+
20+
/// Adds all the events to the given room.
21+
async fn add_room_events(&self, room: &RoomId, events: Vec<SyncTimelineEvent>) -> Result<()>;
22+
23+
/// Clear all the events from the given room.
24+
async fn clear_room_events(&self, room: &RoomId) -> Result<()>;
25+
}
26+
27+
/// An [`EventCacheStore`] implementation that keeps all the information in
28+
/// memory.
29+
pub(crate) struct MemoryStore {
30+
/// All the events per room, in sync order.
31+
by_room: RwLock<BTreeMap<OwnedRoomId, Vec<SyncTimelineEvent>>>,
32+
}
33+
34+
impl MemoryStore {
35+
/// Create a new empty [`MemoryStore`].
36+
pub fn new() -> Self {
37+
Self { by_room: Default::default() }
38+
}
39+
}
40+
41+
#[async_trait]
42+
impl EventCacheStore for MemoryStore {
43+
async fn room_events(&self, room: &RoomId) -> Result<Vec<SyncTimelineEvent>> {
44+
Ok(self.by_room.read().await.get(room).cloned().unwrap_or_default())
45+
}
46+
47+
async fn add_room_events(&self, room: &RoomId, events: Vec<SyncTimelineEvent>) -> Result<()> {
48+
self.by_room.write().await.entry(room.to_owned()).or_default().extend(events);
49+
Ok(())
50+
}
51+
52+
async fn clear_room_events(&self, room: &RoomId) -> Result<()> {
53+
let _ = self.by_room.write().await.remove(room);
54+
Ok(())
55+
}
56+
}

0 commit comments

Comments
 (0)