Skip to content

Commit e761648

Browse files
committed
change(spaces): publish VectorDiffs instead of a full vectors for joined space subscriptions
1 parent 7b6cf7e commit e761648

File tree

3 files changed

+552
-462
lines changed

3 files changed

+552
-462
lines changed

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

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use std::{fmt::Debug, sync::Arc};
1616

17+
use eyeball_im::VectorDiff;
1718
use futures_util::{pin_mut, StreamExt};
1819
use matrix_sdk_common::{SendOutsideWasm, SyncOutsideWasm};
1920
use matrix_sdk_ui::spaces::{
@@ -57,17 +58,18 @@ impl SpaceService {
5758
&self,
5859
listener: Box<dyn SpaceServiceJoinedSpacesListener>,
5960
) -> Arc<TaskHandle> {
60-
let entries_stream = self.inner.subscribe_to_joined_spaces();
61+
let (initial_values, mut stream) = self.inner.subscribe_to_joined_spaces();
6162

62-
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move {
63-
pin_mut!(entries_stream);
63+
listener.on_update(vec![SpaceListUpdate::Reset {
64+
values: initial_values.into_iter().map(Into::into).collect(),
65+
}]);
6466

65-
while let Some(rooms) = entries_stream.next().await {
66-
listener.on_update(rooms.into_iter().map(Into::into).collect());
67+
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move {
68+
while let Some(diffs) = stream.next().await {
69+
listener.on_update(diffs.into_iter().map(Into::into).collect());
6770
}
6871
})))
6972
}
70-
7173
#[allow(clippy::unused_async)]
7274
// This method doesn't need to be async but if its not the FFI layer panics
7375
// with "there is no no reactor running, must be called from the context
@@ -122,13 +124,15 @@ impl SpaceRoomList {
122124
&self,
123125
listener: Box<dyn SpaceRoomListEntriesListener>,
124126
) -> Arc<TaskHandle> {
125-
let entries_stream = self.inner.subscribe_to_room_updates();
127+
let (initial_values, mut stream) = self.inner.subscribe_to_room_updates();
126128

127-
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move {
128-
pin_mut!(entries_stream);
129+
listener.on_update(vec![SpaceListUpdate::Reset {
130+
values: initial_values.into_iter().map(Into::into).collect(),
131+
}]);
129132

130-
while let Some(rooms) = entries_stream.next().await {
131-
listener.on_update(rooms.into_iter().map(Into::into).collect());
133+
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move {
134+
while let Some(diffs) = stream.next().await {
135+
listener.on_update(diffs.into_iter().map(Into::into).collect());
132136
}
133137
})))
134138
}
@@ -145,12 +149,12 @@ pub trait SpaceRoomListPaginationStateListener: SendOutsideWasm + SyncOutsideWas
145149

146150
#[matrix_sdk_ffi_macros::export(callback_interface)]
147151
pub trait SpaceRoomListEntriesListener: SendOutsideWasm + SyncOutsideWasm + Debug {
148-
fn on_update(&self, rooms: Vec<SpaceRoom>);
152+
fn on_update(&self, rooms: Vec<SpaceListUpdate>);
149153
}
150154

151155
#[matrix_sdk_ffi_macros::export(callback_interface)]
152156
pub trait SpaceServiceJoinedSpacesListener: SendOutsideWasm + SyncOutsideWasm + Debug {
153-
fn on_update(&self, rooms: Vec<SpaceRoom>);
157+
fn on_update(&self, room_updates: Vec<SpaceListUpdate>);
154158
}
155159

156160
#[derive(uniffi::Record)]
@@ -190,3 +194,44 @@ impl From<UISpaceRoom> for SpaceRoom {
190194
}
191195
}
192196
}
197+
198+
#[derive(uniffi::Enum)]
199+
pub enum SpaceListUpdate {
200+
Append { values: Vec<SpaceRoom> },
201+
Clear,
202+
PushFront { value: SpaceRoom },
203+
PushBack { value: SpaceRoom },
204+
PopFront,
205+
PopBack,
206+
Insert { index: u32, value: SpaceRoom },
207+
Set { index: u32, value: SpaceRoom },
208+
Remove { index: u32 },
209+
Truncate { length: u32 },
210+
Reset { values: Vec<SpaceRoom> },
211+
}
212+
213+
impl From<VectorDiff<UISpaceRoom>> for SpaceListUpdate {
214+
fn from(diff: VectorDiff<UISpaceRoom>) -> Self {
215+
match diff {
216+
VectorDiff::Append { values } => {
217+
Self::Append { values: values.into_iter().map(|v| v.into()).collect() }
218+
}
219+
VectorDiff::Clear => Self::Clear,
220+
VectorDiff::PushFront { value } => Self::PushFront { value: value.into() },
221+
VectorDiff::PushBack { value } => Self::PushBack { value: value.into() },
222+
VectorDiff::PopFront => Self::PopFront,
223+
VectorDiff::PopBack => Self::PopBack,
224+
VectorDiff::Insert { index, value } => {
225+
Self::Insert { index: index as u32, value: value.into() }
226+
}
227+
VectorDiff::Set { index, value } => {
228+
Self::Set { index: index as u32, value: value.into() }
229+
}
230+
VectorDiff::Remove { index } => Self::Remove { index: index as u32 },
231+
VectorDiff::Truncate { length } => Self::Truncate { length: length as u32 },
232+
VectorDiff::Reset { values } => {
233+
Self::Reset { values: values.into_iter().map(|v| v.into()).collect() }
234+
}
235+
}
236+
}
237+
}

0 commit comments

Comments
 (0)