Skip to content

Commit 1ad5e89

Browse files
committed
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.
1 parent 39a9f96 commit 1ad5e89

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

crates/matrix-sdk-ui/src/room_list_service/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ impl RoomListService {
203203
builder = builder.share_pos();
204204
}
205205

206+
let state_machine = StateMachine::new();
207+
let observable_state = state_machine.cloned_state();
208+
206209
let sliding_sync = builder
207210
.add_cached_list(
208211
SlidingSyncList::builder(ALL_ROOMS_LIST_NAME)
@@ -222,7 +225,23 @@ impl RoomListService {
222225
// If unset, both invited and joined rooms are returned. If false, no invited rooms are
223226
// returned. If true, only invited rooms are returned.
224227
is_invite: None,
225-
}))),
228+
})))
229+
.requires_timeout(move |request_generator| {
230+
// We want Sliding Sync to apply the poll + network timeout —i.e. to do the
231+
// long-polling— in some particular cases. Let's define them.
232+
match observable_state.get() {
233+
// These are the states where we want an immediate response from the
234+
// server, with no long-polling.
235+
State::Init
236+
| State::SettingUp
237+
| State::Recovering
238+
| State::Error { .. }
239+
| State::Terminated { .. } => false,
240+
241+
// Otherwise we want long-polling if the list is fully-loaded.
242+
State::Running => request_generator.is_fully_loaded(),
243+
}
244+
}),
226245
)
227246
.await
228247
.map_err(Error::SlidingSync)?
@@ -234,7 +253,7 @@ impl RoomListService {
234253
// Eagerly subscribe the event cache to sync responses.
235254
client.event_cache().subscribe()?;
236255

237-
Ok(Self { client, sliding_sync, state_machine: StateMachine::new() })
256+
Ok(Self { client, sliding_sync, state_machine })
238257
}
239258

240259
/// Start to sync the room list.

crates/matrix-sdk-ui/src/room_list_service/state.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ impl StateMachine {
9494
self.state.get()
9595
}
9696

97+
/// Clone the inner [`Self::state`].
98+
pub(super) fn cloned_state(&self) -> SharedObservable<State> {
99+
self.state.clone()
100+
}
101+
97102
/// Set the new state.
98103
///
99104
/// Setting a new state will update `Self::last_state_update`.

0 commit comments

Comments
 (0)