Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add add_cached_list to SlidingSyncBuilder and SlidingSync #1876

Merged
merged 18 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions bindings/matrix-sdk-ffi/src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,21 @@ impl SlidingSync {
self.inner.add_list(list.inner.clone()).map(|inner| Arc::new(SlidingSyncList { inner }))
}

pub fn pop_list(&self, name: String) -> Option<Arc<SlidingSyncList>> {
self.inner.pop_list(&name).map(|inner| Arc::new(SlidingSyncList { inner }))
pub fn add_cached_list(
&self,
list_builder: Arc<SlidingSyncListBuilder>,
) -> Result<Option<Arc<SlidingSyncList>>, ClientError> {
RUNTIME.block_on(async move {
Ok(self
.inner
.add_cached_list(list_builder.inner.clone())
.await?
.map(|inner| Arc::new(SlidingSyncList { inner })))
})
}

pub fn remove_list(&self, name: String) -> Option<Arc<SlidingSyncList>> {
self.inner.remove_list(&name).map(|inner| Arc::new(SlidingSyncList { inner }))
}

pub fn add_common_extensions(&self) {
Expand Down Expand Up @@ -816,6 +829,17 @@ impl SlidingSyncBuilder {
Arc::new(builder)
}

pub fn add_cached_list(
self: Arc<Self>,
v: Arc<SlidingSyncListBuilder>,
bnjbvr marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<Arc<Self>, ClientError> {
let mut builder = unwrap_or_clone_arc(self);
let list_builder = unwrap_or_clone_arc(v);
builder.inner = RUNTIME
.block_on(async move { builder.inner.add_cached_list(list_builder.inner).await })?;
Ok(Arc::new(builder))
}

pub fn with_common_extensions(self: Arc<Self>) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.inner = builder.inner.with_common_extensions();
Expand Down
31 changes: 25 additions & 6 deletions crates/matrix-sdk/src/sliding_sync/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::{
cache::restore_sliding_sync_state, SlidingSync, SlidingSyncInner, SlidingSyncList,
SlidingSyncPositionMarkers, SlidingSyncRoom,
};
use crate::{Client, Result};
use crate::{Client, Result, SlidingSyncListBuilder};

/// Configuration for a Sliding Sync instance.
///
Expand All @@ -34,6 +34,7 @@ pub struct SlidingSyncBuilder {
bump_event_types: Vec<TimelineEventType>,
extensions: Option<ExtensionsConfig>,
subscriptions: BTreeMap<OwnedRoomId, v4::RoomSubscription>,
rooms: BTreeMap<OwnedRoomId, SlidingSyncRoom>,
}

impl SlidingSyncBuilder {
Expand All @@ -46,6 +47,7 @@ impl SlidingSyncBuilder {
bump_event_types: Vec::new(),
extensions: None,
subscriptions: BTreeMap::new(),
rooms: BTreeMap::new(),
}
}

Expand All @@ -63,13 +65,32 @@ impl SlidingSyncBuilder {

/// Add the given list to the lists.
///
/// Replace any list with the name.
/// Replace any list with the same name.
pub fn add_list(mut self, list: SlidingSyncList) -> Self {
self.lists.insert(list.name().to_owned(), list);

self
}

/// Enroll the list in caching, reloads it from the cache if possible, and
/// adds it to the list of lists.
///
/// This will raise an error if a [`storage_key()`] was not set, or if there
/// was a I/O error reading from the cache.
///
/// Replace any list with the same name.
pub async fn add_cached_list(mut self, mut list: SlidingSyncListBuilder) -> Result<Self> {
let Some(ref storage_key) = self.storage_key else {
return Err(super::error::Error::MissingStorageKeyForCaching.into());
};
let reloaded_rooms = list.set_cached_and_reload(&self.client, storage_key).await?;
for (key, frozen) in reloaded_rooms {
bnjbvr marked this conversation as resolved.
Show resolved Hide resolved
self.rooms
.entry(key)
.or_insert_with(|| SlidingSyncRoom::from_frozen(frozen, self.client.clone()));
}
Ok(self.add_list(list.build()))
bnjbvr marked this conversation as resolved.
Show resolved Hide resolved
}

/// Activate e2ee, to-device-message and account data extensions if not yet
/// configured.
///
Expand Down Expand Up @@ -204,7 +225,6 @@ impl SlidingSyncBuilder {
let client = self.client;

let mut delta_token = None;
let mut rooms_found: BTreeMap<OwnedRoomId, SlidingSyncRoom> = BTreeMap::new();

// Load an existing state from the cache.
if let Some(storage_key) = &self.storage_key {
Expand All @@ -213,13 +233,12 @@ impl SlidingSyncBuilder {
storage_key,
&mut self.lists,
&mut delta_token,
&mut rooms_found,
&mut self.extensions,
)
.await?;
}

let rooms = StdRwLock::new(rooms_found);
let rooms = StdRwLock::new(self.rooms);
let lists = StdRwLock::new(self.lists);

Ok(SlidingSync::new(SlidingSyncInner {
Expand Down
Loading