@@ -36,7 +36,7 @@ use web_sys::IdbTransactionMode;
36
36
use crate :: event_cache_store:: {
37
37
migrations:: current:: keys,
38
38
serializer:: IndexeddbEventCacheStoreSerializer ,
39
- transaction:: IndexeddbEventCacheStoreTransaction ,
39
+ transaction:: { IndexeddbEventCacheStoreTransaction , IndexeddbEventCacheStoreTransactionError } ,
40
40
types:: { ChunkType , InBandEvent } ,
41
41
} ;
42
42
@@ -300,10 +300,38 @@ impl_event_cache_store! {
300
300
( Option <RawChunk <Event , Gap >>, ChunkIdentifierGenerator ) ,
301
301
IndexeddbEventCacheStoreError ,
302
302
> {
303
- self . memory_store
304
- . load_last_chunk( linked_chunk_id)
305
- . await
306
- . map_err( IndexeddbEventCacheStoreError :: MemoryStore )
303
+ let linked_chunk_id = linked_chunk_id. to_owned( ) ;
304
+ let room_id = linked_chunk_id. room_id( ) ;
305
+ let transaction = self . transaction(
306
+ & [ keys:: LINKED_CHUNKS , keys:: EVENTS , keys:: GAPS ] ,
307
+ IdbTransactionMode :: Readonly ,
308
+ ) ?;
309
+
310
+ if transaction. get_chunks_count_in_room( room_id) . await ? == 0 {
311
+ return Ok ( ( None , ChunkIdentifierGenerator :: new_from_scratch( ) ) ) ;
312
+ }
313
+ match transaction. get_chunk_by_next_chunk_id( room_id, & None ) . await {
314
+ Err ( IndexeddbEventCacheStoreTransactionError :: ItemIsNotUnique ) => {
315
+ Err ( IndexeddbEventCacheStoreError :: ChunksContainDisjointLists )
316
+ }
317
+ Err ( e) => Err ( e. into( ) ) ,
318
+ Ok ( None ) => Err ( IndexeddbEventCacheStoreError :: ChunksContainCycle ) ,
319
+ Ok ( Some ( last_chunk) ) => {
320
+ let last_chunk_identifier = ChunkIdentifier :: new( last_chunk. identifier) ;
321
+ let last_raw_chunk = transaction
322
+ . load_chunk_by_id( room_id, & last_chunk_identifier)
323
+ . await ?
324
+ . ok_or( IndexeddbEventCacheStoreError :: UnableToLoadChunk ) ?;
325
+ let max_chunk_id = transaction
326
+ . get_max_chunk_by_id( room_id)
327
+ . await ?
328
+ . map( |chunk| ChunkIdentifier :: new( chunk. identifier) )
329
+ . ok_or( IndexeddbEventCacheStoreError :: NoMaxChunkId ) ?;
330
+ let generator =
331
+ ChunkIdentifierGenerator :: new_from_previous_chunk_identifier( max_chunk_id) ;
332
+ Ok ( ( Some ( last_raw_chunk) , generator) )
333
+ }
334
+ }
307
335
}
308
336
309
337
async fn load_previous_chunk(
0 commit comments