From 64a51af18d4067e86bd8ee19387f65d62f3cd044 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 18 Nov 2025 14:19:03 +0100 Subject: [PATCH] feat(ui): Manually define when to do long-polling in the `RoomListService`. This patch uses the newly introduced `SlidingSyncListBuilder::requires_timeout` to define when the `RoomListService` must apply a long-polling depending on its state machine. --- .../src/room_list_service/mod.rs | 23 +++++++++++++++++-- .../src/room_list_service/state.rs | 5 ++++ 2 files changed, 26 insertions(+), 2 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 610e80d67..463ddb1c7 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/mod.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/mod.rs @@ -203,6 +203,9 @@ impl RoomListService { builder = builder.share_pos(); } + let state_machine = StateMachine::new(); + let observable_state = state_machine.cloned_state(); + let sliding_sync = builder .add_cached_list( SlidingSyncList::builder(ALL_ROOMS_LIST_NAME) @@ -222,7 +225,23 @@ impl RoomListService { // If unset, both invited and joined rooms are returned. If false, no invited rooms are // returned. If true, only invited rooms are returned. is_invite: None, - }))), + }))) + .requires_timeout(move |request_generator| { + // We want Sliding Sync to apply the poll + network timeout —i.e. to do the + // long-polling— in some particular cases. Let's define them. + match observable_state.get() { + // These are the states where we want an immediate response from the + // server, with no long-polling. + State::Init + | State::SettingUp + | State::Recovering + | State::Error { .. } + | State::Terminated { .. } => false, + + // Otherwise we want long-polling if the list is fully-loaded. + State::Running => request_generator.is_fully_loaded(), + } + }), ) .await .map_err(Error::SlidingSync)? @@ -234,7 +253,7 @@ impl RoomListService { // Eagerly subscribe the event cache to sync responses. client.event_cache().subscribe()?; - Ok(Self { client, sliding_sync, state_machine: StateMachine::new() }) + 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 9b4dc0b6b..51c9d5673 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/state.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/state.rs @@ -94,6 +94,11 @@ impl StateMachine { self.state.get() } + /// Clone the inner [`Self::state`]. + pub(super) fn cloned_state(&self) -> SharedObservable { + self.state.clone() + } + /// Set the new state. /// /// Setting a new state will update `Self::last_state_update`.