From 99bbf2a42bf50183598f6f34e384c8008ada8f27 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 24 May 2023 08:41:16 +0200 Subject: [PATCH] feat(sdk): Implement `SlidingSync::stop_sync`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In case it's not obvious to drop the `Stream` returned by `SlidingSync::sync` immediately to “stop” the sync-loop, one can use the new `stop_sync` method to do achieve the same result. --- crates/matrix-sdk/src/sliding_sync/mod.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk/src/sliding_sync/mod.rs b/crates/matrix-sdk/src/sliding_sync/mod.rs index 36f9c75ba..67430ea26 100644 --- a/crates/matrix-sdk/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk/src/sliding_sync/mod.rs @@ -665,6 +665,18 @@ impl SlidingSync { } } + /// Force to stop the sync-loop ([`Self::sync`]) if it's running. + /// + /// Usually, dropping the `Stream` returned by [`Self::sync`] should be + /// enough to “stop” it, but depending of how this `Stream` is used, it + /// might not be obvious to drop it immediately (thinking of using this API + /// over FFI; the foreign-language might not be able to drop a value + /// immediately). Thus, calling this method will ensure that the sync-loop + /// stops gracefully and as soon as it returns. + pub async fn stop_sync(&self) -> Result<(), Error> { + self.inner.internal_channel_send(SlidingSyncInternalMessage::SyncLoopStop).await + } + /// Resets the lists. pub fn reset_lists(&self) -> Result<(), Error> { let lists = self.inner.lists.read().unwrap(); @@ -679,7 +691,10 @@ impl SlidingSync { impl SlidingSyncInner { /// Send a message over the internal channel. - async fn internal_channel_send(&self, message: SlidingSyncInternalMessage) -> Result<()> { + async fn internal_channel_send( + &self, + message: SlidingSyncInternalMessage, + ) -> Result<(), Error> { Ok(self .internal_channel .0