14
14
15
15
use std:: { fmt:: Debug , sync:: Arc } ;
16
16
17
+ use eyeball_im:: VectorDiff ;
17
18
use futures_util:: { pin_mut, StreamExt } ;
18
19
use matrix_sdk_common:: { SendOutsideWasm , SyncOutsideWasm } ;
19
20
use matrix_sdk_ui:: spaces:: {
@@ -57,17 +58,18 @@ impl SpaceService {
57
58
& self ,
58
59
listener : Box < dyn SpaceServiceJoinedSpacesListener > ,
59
60
) -> Arc < TaskHandle > {
60
- let entries_stream = self . inner . subscribe_to_joined_spaces ( ) ;
61
+ let ( initial_values , mut stream ) = self . inner . subscribe_to_joined_spaces ( ) ;
61
62
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
+ } ] ) ;
64
66
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 ( ) ) ;
67
70
}
68
71
} ) ) )
69
72
}
70
-
71
73
#[ allow( clippy:: unused_async) ]
72
74
// This method doesn't need to be async but if its not the FFI layer panics
73
75
// with "there is no no reactor running, must be called from the context
@@ -122,13 +124,15 @@ impl SpaceRoomList {
122
124
& self ,
123
125
listener : Box < dyn SpaceRoomListEntriesListener > ,
124
126
) -> Arc < TaskHandle > {
125
- let entries_stream = self . inner . subscribe_to_room_updates ( ) ;
127
+ let ( initial_values , mut stream ) = self . inner . subscribe_to_room_updates ( ) ;
126
128
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
+ } ] ) ;
129
132
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 ( ) ) ;
132
136
}
133
137
} ) ) )
134
138
}
@@ -145,12 +149,12 @@ pub trait SpaceRoomListPaginationStateListener: SendOutsideWasm + SyncOutsideWas
145
149
146
150
#[ matrix_sdk_ffi_macros:: export( callback_interface) ]
147
151
pub trait SpaceRoomListEntriesListener : SendOutsideWasm + SyncOutsideWasm + Debug {
148
- fn on_update ( & self , rooms : Vec < SpaceRoom > ) ;
152
+ fn on_update ( & self , rooms : Vec < SpaceListUpdate > ) ;
149
153
}
150
154
151
155
#[ matrix_sdk_ffi_macros:: export( callback_interface) ]
152
156
pub trait SpaceServiceJoinedSpacesListener : SendOutsideWasm + SyncOutsideWasm + Debug {
153
- fn on_update ( & self , rooms : Vec < SpaceRoom > ) ;
157
+ fn on_update ( & self , room_updates : Vec < SpaceListUpdate > ) ;
154
158
}
155
159
156
160
#[ derive( uniffi:: Record ) ]
@@ -190,3 +194,44 @@ impl From<UISpaceRoom> for SpaceRoom {
190
194
}
191
195
}
192
196
}
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