From 34be01ecf2925a24ec3fa864bd3036230ff8d321 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 2 Aug 2023 11:57:49 +0200 Subject: [PATCH] Revert "ffi: Remove async from a few functions" This reverts commit fc3883d08e3ce9d04be39b1fc2867c21b973dbd7. --- bindings/matrix-sdk-ffi/src/room.rs | 61 ++++++++++++++++------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/room.rs b/bindings/matrix-sdk-ffi/src/room.rs index 3f3d63905..d1bd41805 100644 --- a/bindings/matrix-sdk-ffi/src/room.rs +++ b/bindings/matrix-sdk-ffi/src/room.rs @@ -226,38 +226,40 @@ impl Room { }) } - pub fn add_timeline_listener( + pub async fn add_timeline_listener( self: Arc, listener: Box, ) -> RoomTimelineListenerResult { - RUNTIME.block_on(async move { - let timeline = self - .timeline - .write() - .await - .get_or_insert_with(|| { - let timeline = RUNTIME.block_on(self.inner.timeline()); - Arc::new(timeline) - }) - .clone(); + RUNTIME + .spawn(async move { + let timeline = self + .timeline + .write() + .await + .get_or_insert_with(|| { + let timeline = RUNTIME.block_on(self.inner.timeline()); + Arc::new(timeline) + }) + .clone(); - let (timeline_items, timeline_stream) = timeline.subscribe_batched().await; + let (timeline_items, timeline_stream) = timeline.subscribe_batched().await; + let timeline_stream = TaskHandle::new(RUNTIME.spawn(async move { + pin_mut!(timeline_stream); - let timeline_stream = TaskHandle::new(RUNTIME.spawn(async move { - pin_mut!(timeline_stream); + while let Some(diffs) = timeline_stream.next().await { + listener.on_update( + diffs.into_iter().map(|d| Arc::new(TimelineDiff::new(d))).collect(), + ); + } + })); - while let Some(diffs) = timeline_stream.next().await { - listener.on_update( - diffs.into_iter().map(|d| Arc::new(TimelineDiff::new(d))).collect(), - ); + RoomTimelineListenerResult { + items: timeline_items.into_iter().map(TimelineItem::from_arc).collect(), + items_stream: Arc::new(timeline_stream), } - })); - - RoomTimelineListenerResult { - items: timeline_items.into_iter().map(TimelineItem::from_arc).collect(), - items_stream: Arc::new(timeline_stream), - } - }) + }) + .await + .unwrap() } pub fn subscribe_to_back_pagination_status( @@ -959,10 +961,13 @@ impl SendAttachmentJoinHandle { } } -#[uniffi::export] +#[uniffi::export(async_runtime = "tokio")] impl SendAttachmentJoinHandle { - pub fn join(self: Arc) -> Result<(), RoomError> { - RUNTIME.block_on(async move { (&mut *self.join_hdl.lock().await).await.unwrap() }) + pub async fn join(self: Arc) -> Result<(), RoomError> { + RUNTIME + .spawn(async move { (&mut *self.join_hdl.lock().await).await.unwrap() }) + .await + .unwrap() } pub fn cancel(&self) {