42
42
43
43
use std:: { collections:: BTreeMap , fmt:: Debug , sync:: Arc } ;
44
44
45
- use async_trait:: async_trait;
46
45
use matrix_sdk:: { sync:: RoomUpdate , Client , Room } ;
47
46
use matrix_sdk_base:: {
48
47
deserialized_responses:: { AmbiguityChange , SyncTimelineEvent } ,
@@ -55,14 +54,15 @@ use ruma::{
55
54
} ;
56
55
use tokio:: {
57
56
spawn,
58
- sync:: {
59
- broadcast:: { error:: RecvError , Receiver , Sender } ,
60
- RwLock ,
61
- } ,
57
+ sync:: broadcast:: { error:: RecvError , Receiver , Sender } ,
62
58
task:: JoinHandle ,
63
59
} ;
64
60
use tracing:: { debug, error, trace} ;
65
61
62
+ use self :: store:: { EventCacheStore , MemoryStore } ;
63
+
64
+ mod store;
65
+
66
66
/// An error observed in the [`EventCache`].
67
67
#[ derive( thiserror:: Error , Debug ) ]
68
68
pub enum EventCacheError {
@@ -143,51 +143,6 @@ impl EventCache {
143
143
}
144
144
}
145
145
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
-
191
146
/// A subset of an event cache, for a room.
192
147
///
193
148
/// Cloning is shallow, and thus is cheap to do.
0 commit comments