diff --git a/bindings/matrix-sdk-ffi/src/api.udl b/bindings/matrix-sdk-ffi/src/api.udl index ab2e57bac..5610266bb 100644 --- a/bindings/matrix-sdk-ffi/src/api.udl +++ b/bindings/matrix-sdk-ffi/src/api.udl @@ -1,8 +1,7 @@ namespace matrix_sdk_ffi {}; -/// Cancels on drop -interface StoppableSpawn {}; +interface TaskHandle {}; [Error] interface ClientError { @@ -112,10 +111,10 @@ interface SlidingSyncViewBuilder { }; interface SlidingSyncView { - StoppableSpawn observe_room_list(SlidingSyncViewRoomListObserver observer); - StoppableSpawn observe_rooms_count(SlidingSyncViewRoomsCountObserver observer); - StoppableSpawn observe_state(SlidingSyncViewStateObserver observer); - StoppableSpawn observe_room_items(SlidingSyncViewRoomItemsObserver observer); + TaskHandle observe_room_list(SlidingSyncViewRoomListObserver observer); + TaskHandle observe_rooms_count(SlidingSyncViewRoomsCountObserver observer); + TaskHandle observe_state(SlidingSyncViewStateObserver observer); + TaskHandle observe_room_items(SlidingSyncViewRoomItemsObserver observer); }; interface SlidingSyncRoom { @@ -127,7 +126,7 @@ interface SlidingSyncRoom { dictionary SlidingSyncSubscribeResult { sequence items; - StoppableSpawn task_handle; + TaskHandle task_handle; }; interface SlidingSync { diff --git a/bindings/matrix-sdk-ffi/src/lib.rs b/bindings/matrix-sdk-ffi/src/lib.rs index 326034614..16c1b16f0 100644 --- a/bindings/matrix-sdk-ffi/src/lib.rs +++ b/bindings/matrix-sdk-ffi/src/lib.rs @@ -90,7 +90,7 @@ mod uniffi_types { sliding_sync::{ RequiredState, RoomListEntry, SlidingSync, SlidingSyncBuilder, SlidingSyncRequestListFilters, SlidingSyncRoom, SlidingSyncView, - SlidingSyncViewBuilder, StoppableSpawn, UnreadNotificationsCount, + SlidingSyncViewBuilder, TaskHandle, UnreadNotificationsCount, }, timeline::{ AudioInfo, AudioMessageContent, EmoteMessageContent, EncryptedMessage, EventSendState, diff --git a/bindings/matrix-sdk-ffi/src/sliding_sync.rs b/bindings/matrix-sdk-ffi/src/sliding_sync.rs index dbb3439f5..0b79f83a7 100644 --- a/bindings/matrix-sdk-ffi/src/sliding_sync.rs +++ b/bindings/matrix-sdk-ffi/src/sliding_sync.rs @@ -28,34 +28,35 @@ use crate::{ TimelineItem, TimelineListener, }; -type StoppableSpawnCallback = Box; +type TaskHandleCallback = Box; -pub struct StoppableSpawn { +pub struct TaskHandle { handle: Option>, - callback: RwLock>, + callback: RwLock>, } -impl StoppableSpawn { - fn with_handle(handle: JoinHandle<()>) -> StoppableSpawn { - StoppableSpawn { handle: Some(handle), callback: Default::default() } - } - fn with_callback(callback: StoppableSpawnCallback) -> StoppableSpawn { - StoppableSpawn { handle: Default::default(), callback: RwLock::new(Some(callback)) } +impl TaskHandle { + fn with_handle(handle: JoinHandle<()>) -> Self { + Self { handle: Some(handle), callback: Default::default() } } - fn set_callback(&mut self, f: StoppableSpawnCallback) { + fn with_callback(callback: TaskHandleCallback) -> Self { + Self { handle: Default::default(), callback: RwLock::new(Some(callback)) } + } + + fn set_callback(&mut self, f: TaskHandleCallback) { *self.callback.write().unwrap() = Some(f) } } -impl From> for StoppableSpawn { +impl From> for TaskHandle { fn from(value: JoinHandle<()>) -> Self { - StoppableSpawn::with_handle(value) + Self::with_handle(value) } } #[uniffi::export] -impl StoppableSpawn { +impl TaskHandle { pub fn cancel(&self) { debug!("stoppable.cancel() called"); if let Some(handle) = &self.handle { @@ -65,12 +66,13 @@ impl StoppableSpawn { callback(); } } + pub fn is_finished(&self) -> bool { self.handle.as_ref().map(|h| h.is_finished()).unwrap_or_default() } } -impl Drop for StoppableSpawn { +impl Drop for TaskHandle { fn drop(&mut self) { self.cancel(); } @@ -185,7 +187,7 @@ impl SlidingSyncRoom { fn add_timeline_listener_inner( &self, listener: Box, - ) -> anyhow::Result<(Vec>, StoppableSpawn)> { + ) -> anyhow::Result<(Vec>, TaskHandle)> { let mut timeline_lock = self.timeline.write().unwrap(); let timeline = match &*timeline_lock { Some(timeline) => timeline, @@ -230,7 +232,7 @@ impl SlidingSyncRoom { }; let items = timeline_items.into_iter().map(TimelineItem::from_arc).collect(); - let task_handle = StoppableSpawn::with_handle(RUNTIME.spawn(async move { + let task_handle = TaskHandle::with_handle(RUNTIME.spawn(async move { join(handle_events, handle_sliding_sync_reset).await; })); Ok((items, task_handle)) @@ -239,7 +241,7 @@ impl SlidingSyncRoom { pub struct SlidingSyncSubscribeResult { pub items: Vec>, - pub task_handle: Arc, + pub task_handle: Arc, } pub struct UpdateSummary { @@ -516,10 +518,10 @@ impl SlidingSyncView { pub fn observe_state( &self, observer: Box, - ) -> Arc { + ) -> Arc { let mut state_stream = self.inner.state_stream(); - Arc::new(StoppableSpawn::with_handle(RUNTIME.spawn(async move { + Arc::new(TaskHandle::with_handle(RUNTIME.spawn(async move { loop { if let Some(new_state) = state_stream.next().await { observer.did_receive_update(new_state); @@ -531,10 +533,10 @@ impl SlidingSyncView { pub fn observe_room_list( &self, observer: Box, - ) -> Arc { + ) -> Arc { let mut rooms_list_stream = self.inner.rooms_list_stream(); - Arc::new(StoppableSpawn::with_handle(RUNTIME.spawn(async move { + Arc::new(TaskHandle::with_handle(RUNTIME.spawn(async move { loop { if let Some(diff) = rooms_list_stream.next().await { observer.did_receive_update(diff.into()); @@ -546,9 +548,9 @@ impl SlidingSyncView { pub fn observe_room_items( &self, observer: Box, - ) -> Arc { + ) -> Arc { let mut rooms_updated = self.inner.rooms_updated_broadcaster.signal_cloned().to_stream(); - Arc::new(StoppableSpawn::with_handle(RUNTIME.spawn(async move { + Arc::new(TaskHandle::with_handle(RUNTIME.spawn(async move { loop { if rooms_updated.next().await.is_some() { observer.did_receive_update(); @@ -560,10 +562,10 @@ impl SlidingSyncView { pub fn observe_rooms_count( &self, observer: Box, - ) -> Arc { + ) -> Arc { let mut rooms_count_stream = self.inner.rooms_count_stream(); - Arc::new(StoppableSpawn::with_handle(RUNTIME.spawn(async move { + Arc::new(TaskHandle::with_handle(RUNTIME.spawn(async move { loop { if let Some(Some(new)) = rooms_count_stream.next().await { observer.did_receive_update(new); @@ -713,14 +715,14 @@ impl SlidingSync { self.inner.add_common_extensions(); } - pub fn sync(&self) -> Arc { + pub fn sync(&self) -> Arc { let inner = self.inner.clone(); let client = self.client.clone(); let observer = self.observer.clone(); let stop_loop = Arc::new(AtomicBool::new(false)); let remote_stopper = stop_loop.clone(); - let stoppable = Arc::new(StoppableSpawn::with_callback(Box::new(move || { + let stoppable = Arc::new(TaskHandle::with_callback(Box::new(move || { remote_stopper.store(true, Ordering::Relaxed); })));