Skip to content

Commit 1b2387b

Browse files
committed
refactor(sdk): Enforce no pagination overlap in timeline
… by locking the pagination token for the entire duration of the API call.
1 parent 1e71036 commit 1b2387b

File tree

1 file changed

+12
-9
lines changed
  • crates/matrix-sdk/src/room/timeline

1 file changed

+12
-9
lines changed

crates/matrix-sdk/src/room/timeline/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
//!
1717
//! See [`Timeline`] for details.
1818
19-
use std::sync::{Arc, Mutex as StdMutex};
19+
use std::sync::Arc;
2020

2121
use futures_core::Stream;
2222
use futures_signals::signal_vec::{SignalVec, SignalVecExt, VecDiff};
23-
use matrix_sdk_base::deserialized_responses::{EncryptionInfo, SyncTimelineEvent};
23+
use matrix_sdk_base::{
24+
deserialized_responses::{EncryptionInfo, SyncTimelineEvent},
25+
locks::Mutex,
26+
};
2427
use ruma::{
2528
assign,
2629
events::{fully_read::FullyReadEventContent, AnyMessageLikeEventContent},
@@ -65,8 +68,8 @@ use self::{
6568
pub struct Timeline {
6669
inner: Arc<TimelineInner>,
6770
room: room::Common,
68-
start_token: StdMutex<Option<String>>,
69-
_end_token: StdMutex<Option<String>>,
71+
start_token: Mutex<Option<String>>,
72+
_end_token: Mutex<Option<String>>,
7073
event_handler_handles: Vec<EventHandlerHandle>,
7174
}
7275

@@ -125,8 +128,8 @@ impl Timeline {
125128
Timeline {
126129
inner,
127130
room: room.clone(),
128-
start_token: StdMutex::new(prev_token),
129-
_end_token: StdMutex::new(None),
131+
start_token: Mutex::new(prev_token),
132+
_end_token: Mutex::new(None),
130133
event_handler_handles,
131134
}
132135
}
@@ -163,11 +166,11 @@ impl Timeline {
163166
/// Add more events to the start of the timeline.
164167
#[instrument(skip(self), fields(room_id = %self.room.room_id()))]
165168
pub async fn paginate_backwards(&self, limit: UInt) -> Result<PaginationOutcome> {
166-
let start = self.start_token.lock().unwrap().clone();
169+
let mut start_lock = self.start_token.lock().await;
167170
let messages = self
168171
.room
169172
.messages(assign!(MessagesOptions::backward(), {
170-
from: start,
173+
from: start_lock.clone(),
171174
limit,
172175
}))
173176
.await?;
@@ -179,7 +182,7 @@ impl Timeline {
179182
}
180183

181184
let outcome = PaginationOutcome { more_messages: messages.end.is_some(), num_updates };
182-
*self.start_token.lock().unwrap() = messages.end;
185+
*start_lock = messages.end;
183186

184187
Ok(outcome)
185188
}

0 commit comments

Comments
 (0)