From bbf9bf2c0be026c2bcf237f48953918057a1e15c Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 18 Jun 2025 14:39:09 +0200 Subject: [PATCH] feat(room list service): skip the initial state when an initial `pos` is set --- crates/matrix-sdk-ui/src/room_list_service/mod.rs | 12 +++++++++++- crates/matrix-sdk-ui/src/room_list_service/state.rs | 4 ++-- .../tests/integration/room_list_service.rs | 8 +++++--- crates/matrix-sdk/src/sliding_sync/mod.rs | 6 ++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/mod.rs b/crates/matrix-sdk-ui/src/room_list_service/mod.rs index 0a106d2da..57ddbe13b 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/mod.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/mod.rs @@ -177,7 +177,17 @@ impl RoomListService { // Eagerly subscribe the event cache to sync responses. client.event_cache().subscribe()?; - Ok(Self { client, sliding_sync, state_machine: StateMachine::new() }) + let state_machine = StateMachine::new(); + + // If the sliding sync has successfully restored a sync position, skip the + // waiting for the initial sync, and set the state to `SettingUp`; this + // way, the first sync will move us to the steady state, and update the + // sliding sync list to use the growing sync mode. + if sliding_sync.has_pos().await { + state_machine.set(State::SettingUp); + } + + Ok(Self { client, sliding_sync, state_machine }) } /// Start to sync the room list. diff --git a/crates/matrix-sdk-ui/src/room_list_service/state.rs b/crates/matrix-sdk-ui/src/room_list_service/state.rs index 076c4933f..3488ffddd 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/state.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/state.rs @@ -107,8 +107,8 @@ impl StateMachine { self.state.subscribe() } - /// Transition to the next state, and execute the associated transition's - /// [`Actions`]. + /// Transition to the next state, and execute the necessary transition on + /// the sliding sync list. pub(super) async fn next(&self, sliding_sync: &SlidingSync) -> Result { use State::*; diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index d0b1f784e..50e67f357 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -1292,19 +1292,21 @@ async fn test_loading_states() -> Result<(), Error> { RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) } ); + // The sync skips the `Init` state, and immediately starts in the `SettingUp` + // state, as the sync `pos`ition marker was set. sync_then_assert_request_and_fake_response! { [server, room_list, sync] - states = Init => SettingUp, + states = SettingUp => Running, assert request >= { "lists": { ALL_ROOMS: { - "ranges": [[0, 19]], + "ranges": [[0, 11]], "timeline_limit": 1, }, }, }, respond with = { - "pos": "0", + "pos": "3", "lists": { ALL_ROOMS: { "count": 13, // 1 more room diff --git a/crates/matrix-sdk/src/sliding_sync/mod.rs b/crates/matrix-sdk/src/sliding_sync/mod.rs index 7065f1989..62958d0e0 100644 --- a/crates/matrix-sdk/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk/src/sliding_sync/mod.rs @@ -124,6 +124,12 @@ impl SlidingSync { Self { inner: Arc::new(inner) } } + /// Whether the current sliding sync instance has set a sync position + /// marker. + pub async fn has_pos(&self) -> bool { + self.inner.position.lock().await.pos.is_some() + } + async fn cache_to_storage(&self, position: &SlidingSyncPositionMarkers) -> Result<()> { cache::store_sliding_sync_state(self, position).await }