Skip to content

Commit ce9a079

Browse files
committed
feat(sdk): use the builder pattern for the other sliding sync mode too
Signed-off-by: Benjamin Bouvier <[email protected]>
1 parent fa25e4d commit ce9a079

File tree

3 files changed

+67
-22
lines changed

3 files changed

+67
-22
lines changed

bindings/matrix-sdk-ffi/src/sliding_sync.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,11 @@ impl SlidingSyncListBuilder {
492492
maximum_number_of_rooms_to_fetch: Option<u32>,
493493
) -> Arc<Self> {
494494
let mut builder = unwrap_or_clone_arc(self);
495-
builder.inner = builder
496-
.inner
497-
.sync_mode(SlidingSyncMode::new_paging(batch_size, maximum_number_of_rooms_to_fetch));
495+
let mut mode_builder = SlidingSyncMode::new_paging(batch_size);
496+
if let Some(num) = maximum_number_of_rooms_to_fetch {
497+
mode_builder = mode_builder.maximum_number_of_rooms_to_fetch(num);
498+
}
499+
builder.inner = builder.inner.sync_mode(mode_builder);
498500
Arc::new(builder)
499501
}
500502

@@ -504,9 +506,11 @@ impl SlidingSyncListBuilder {
504506
maximum_number_of_rooms_to_fetch: Option<u32>,
505507
) -> Arc<Self> {
506508
let mut builder = unwrap_or_clone_arc(self);
507-
builder.inner = builder
508-
.inner
509-
.sync_mode(SlidingSyncMode::new_growing(batch_size, maximum_number_of_rooms_to_fetch));
509+
let mut mode_builder = SlidingSyncMode::new_growing(batch_size);
510+
if let Some(num) = maximum_number_of_rooms_to_fetch {
511+
mode_builder = mode_builder.maximum_number_of_rooms_to_fetch(num);
512+
}
513+
builder.inner = builder.inner.sync_mode(mode_builder);
510514
Arc::new(builder)
511515
}
512516

crates/matrix-sdk/src/sliding_sync/list/mod.rs

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,47 @@ impl From<SlidingSyncSelectiveModeBuilder> for SlidingSyncMode {
736736
}
737737
}
738738

739+
#[derive(Clone, Debug)]
740+
enum WindowedModeBuilderKind {
741+
Paging,
742+
Growing,
743+
}
744+
745+
/// Builder for a new sliding sync list in growing/paging mode.
746+
#[derive(Clone, Debug)]
747+
pub struct SlidingSyncWindowedModeBuilder {
748+
mode: WindowedModeBuilderKind,
749+
batch_size: u32,
750+
maximum_number_of_rooms_to_fetch: Option<u32>,
751+
}
752+
753+
impl SlidingSyncWindowedModeBuilder {
754+
fn new(mode: WindowedModeBuilderKind, batch_size: u32) -> Self {
755+
Self { mode, batch_size, maximum_number_of_rooms_to_fetch: None }
756+
}
757+
758+
/// The maximum number of rooms to fetch.
759+
pub fn maximum_number_of_rooms_to_fetch(mut self, num: u32) -> Self {
760+
self.maximum_number_of_rooms_to_fetch = Some(num);
761+
self
762+
}
763+
}
764+
765+
impl From<SlidingSyncWindowedModeBuilder> for SlidingSyncMode {
766+
fn from(builder: SlidingSyncWindowedModeBuilder) -> Self {
767+
match builder.mode {
768+
WindowedModeBuilderKind::Paging => Self::Paging {
769+
batch_size: builder.batch_size,
770+
maximum_number_of_rooms_to_fetch: builder.maximum_number_of_rooms_to_fetch,
771+
},
772+
WindowedModeBuilderKind::Growing => Self::Growing {
773+
batch_size: builder.batch_size,
774+
maximum_number_of_rooms_to_fetch: builder.maximum_number_of_rooms_to_fetch,
775+
},
776+
}
777+
}
778+
}
779+
739780
/// How a [`SlidingSyncList`] fetches the data.
740781
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
741782
pub enum SlidingSyncMode {
@@ -782,13 +823,13 @@ impl SlidingSyncMode {
782823
}
783824

784825
/// Create a `SlidingSyncMode::Paging`.
785-
pub fn new_paging(batch_size: u32, maximum_number_of_rooms_to_fetch: Option<u32>) -> Self {
786-
Self::Paging { batch_size, maximum_number_of_rooms_to_fetch }
826+
pub fn new_paging(batch_size: u32) -> SlidingSyncWindowedModeBuilder {
827+
SlidingSyncWindowedModeBuilder::new(WindowedModeBuilderKind::Paging, batch_size)
787828
}
788829

789830
/// Create a `SlidingSyncMode::Growing`.
790-
pub fn new_growing(batch_size: u32, maximum_number_of_rooms_to_fetch: Option<u32>) -> Self {
791-
Self::Growing { batch_size, maximum_number_of_rooms_to_fetch }
831+
pub fn new_growing(batch_size: u32) -> SlidingSyncWindowedModeBuilder {
832+
SlidingSyncWindowedModeBuilder::new(WindowedModeBuilderKind::Growing, batch_size)
792833
}
793834
}
794835

@@ -957,7 +998,7 @@ mod tests {
957998
let (sender, _receiver) = channel(1);
958999

9591000
let mut list = SlidingSyncList::builder("testing")
960-
.sync_mode(SlidingSyncMode::new_paging(10, None))
1001+
.sync_mode(SlidingSyncMode::new_paging(10))
9611002
.build(sender);
9621003

9631004
assert_ranges! {
@@ -999,7 +1040,7 @@ mod tests {
9991040
let (sender, _receiver) = channel(1);
10001041

10011042
let mut list = SlidingSyncList::builder("testing")
1002-
.sync_mode(SlidingSyncMode::new_paging(10, Some(22)))
1043+
.sync_mode(SlidingSyncMode::new_paging(10).maximum_number_of_rooms_to_fetch(22))
10031044
.build(sender);
10041045

10051046
assert_ranges! {
@@ -1041,7 +1082,7 @@ mod tests {
10411082
let (sender, _receiver) = channel(1);
10421083

10431084
let mut list = SlidingSyncList::builder("testing")
1044-
.sync_mode(SlidingSyncMode::new_growing(10, None))
1085+
.sync_mode(SlidingSyncMode::new_growing(10))
10451086
.build(sender);
10461087

10471088
assert_ranges! {
@@ -1083,7 +1124,7 @@ mod tests {
10831124
let (sender, _receiver) = channel(1);
10841125

10851126
let mut list = SlidingSyncList::builder("testing")
1086-
.sync_mode(SlidingSyncMode::new_growing(10, Some(22)))
1127+
.sync_mode(SlidingSyncMode::new_growing(10).maximum_number_of_rooms_to_fetch(22))
10871128
.build(sender);
10881129

10891130
assert_ranges! {
@@ -1255,7 +1296,7 @@ mod tests {
12551296
};
12561297

12571298
// Changing from `Selective` to `Growing`.
1258-
list.set_sync_mode(SlidingSyncMode::new_growing(10, None)).unwrap();
1299+
list.set_sync_mode(SlidingSyncMode::new_growing(10)).unwrap();
12591300

12601301
assert_ranges! {
12611302
list = list,
@@ -1291,7 +1332,7 @@ mod tests {
12911332
};
12921333

12931334
// Changing from `Growing` to `Paging`.
1294-
list.set_sync_mode(SlidingSyncMode::new_paging(10, None)).unwrap();
1335+
list.set_sync_mode(SlidingSyncMode::new_paging(10)).unwrap();
12951336

12961337
assert_ranges! {
12971338
list = list,
@@ -1490,15 +1531,15 @@ mod tests {
14901531
#[test]
14911532
fn test_sliding_sync_mode_serialization() {
14921533
assert_json_roundtrip!(
1493-
from SlidingSyncMode: SlidingSyncMode::new_paging(1, Some(2)) => json!({
1534+
from SlidingSyncMode: SlidingSyncMode::from(SlidingSyncMode::new_paging(1).maximum_number_of_rooms_to_fetch(2)) => json!({
14941535
"Paging": {
14951536
"batch_size": 1,
14961537
"maximum_number_of_rooms_to_fetch": 2
14971538
}
14981539
})
14991540
);
15001541
assert_json_roundtrip!(
1501-
from SlidingSyncMode: SlidingSyncMode::new_growing(1, Some(2)) => json!({
1542+
from SlidingSyncMode: SlidingSyncMode::from(SlidingSyncMode::new_growing(1).maximum_number_of_rooms_to_fetch(2)) => json!({
15021543
"Growing": {
15031544
"batch_size": 1,
15041545
"maximum_number_of_rooms_to_fetch": 2

crates/matrix-sdk/src/sliding_sync/list/request_generator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ mod tests {
387387

388388
#[test]
389389
fn test_request_generator_paging_from_sync_mode() {
390-
let sync_mode = SlidingSyncMode::new_paging(1, Some(2));
391-
let request_generator = SlidingSyncListRequestGenerator::new(sync_mode);
390+
let sync_mode = SlidingSyncMode::new_paging(1).maximum_number_of_rooms_to_fetch(2);
391+
let request_generator = SlidingSyncListRequestGenerator::new(sync_mode.into());
392392

393393
assert!(request_generator.ranges.is_empty());
394394
assert_eq!(
@@ -405,8 +405,8 @@ mod tests {
405405

406406
#[test]
407407
fn test_request_generator_growing_from_sync_mode() {
408-
let sync_mode = SlidingSyncMode::new_growing(1, Some(2));
409-
let request_generator = SlidingSyncListRequestGenerator::new(sync_mode);
408+
let sync_mode = SlidingSyncMode::new_growing(1).maximum_number_of_rooms_to_fetch(2);
409+
let request_generator = SlidingSyncListRequestGenerator::new(sync_mode.into());
410410

411411
assert!(request_generator.ranges.is_empty());
412412
assert_eq!(

0 commit comments

Comments
 (0)