From 2ffac286edb3331fa6f46b7e61e902907b9068fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 6 Oct 2020 15:04:43 +0200 Subject: [PATCH] matrix-sdk: Switch to using an enum for the sync loop callback return value. --- matrix_sdk/Cargo.toml | 1 + matrix_sdk/examples/command_bot.rs | 6 ++-- matrix_sdk/examples/emoji_verification.rs | 5 +-- .../examples/wasm_command_bot/src/lib.rs | 10 +++--- matrix_sdk/src/client.rs | 36 ++++++++++++------- matrix_sdk/src/lib.rs | 2 +- 6 files changed, 38 insertions(+), 22 deletions(-) diff --git a/matrix_sdk/Cargo.toml b/matrix_sdk/Cargo.toml index 805283fe..645c4a25 100644 --- a/matrix_sdk/Cargo.toml +++ b/matrix_sdk/Cargo.toml @@ -68,6 +68,7 @@ features = ["wasm-bindgen"] [dev-dependencies] async-trait = "0.1.40" +async-std = { version = "*", features = ["unstable"] } dirs = "3.0.1" matrix-sdk-test = { version = "0.1.0", path = "../matrix_sdk_test" } tokio = { version = "0.2.22", features = ["rt-threaded", "macros"] } diff --git a/matrix_sdk/examples/command_bot.rs b/matrix_sdk/examples/command_bot.rs index c2508440..4f2d9fea 100644 --- a/matrix_sdk/examples/command_bot.rs +++ b/matrix_sdk/examples/command_bot.rs @@ -13,7 +13,7 @@ use url::Url; struct CommandBot { /// This clone of the `Client` will send requests to the server, - /// while the other keeps us in sync with the server using `sync_forever`. + /// while the other keeps us in sync with the server using `sync`. client: Client, } @@ -98,8 +98,8 @@ async fn login_and_sync( .add_event_emitter(Box::new(CommandBot::new(client.clone()))) .await; - // since we called sync before we `sync_forever` we must pass that sync token to - // `sync_forever` + // since we called `sync_once` before we entered our sync loop we must pass + // that sync token to `sync` let settings = SyncSettings::default().token(client.sync_token().await.unwrap()); // this keeps state from the server streaming in to CommandBot via the EventEmitter trait client.sync(settings).await; diff --git a/matrix_sdk/examples/emoji_verification.rs b/matrix_sdk/examples/emoji_verification.rs index 1f831653..509e7ea4 100644 --- a/matrix_sdk/examples/emoji_verification.rs +++ b/matrix_sdk/examples/emoji_verification.rs @@ -2,7 +2,8 @@ use std::{env, io, process::exit}; use url::Url; use matrix_sdk::{ - self, events::AnyToDeviceEvent, identifiers::UserId, Client, ClientConfig, Sas, SyncSettings, + self, events::AnyToDeviceEvent, identifiers::UserId, Client, ClientConfig, LoopCtrl, Sas, + SyncSettings, }; async fn wait_for_confirmation(client: Client, sas: Sas) { @@ -117,7 +118,7 @@ async fn login( } } - false + LoopCtrl::Continue }) .await; diff --git a/matrix_sdk/examples/wasm_command_bot/src/lib.rs b/matrix_sdk/examples/wasm_command_bot/src/lib.rs index 40b178d4..4de325d4 100644 --- a/matrix_sdk/examples/wasm_command_bot/src/lib.rs +++ b/matrix_sdk/examples/wasm_command_bot/src/lib.rs @@ -7,7 +7,7 @@ use matrix_sdk::{ AnyMessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, SyncMessageEvent, }, identifiers::RoomId, - Client, ClientConfig, SyncSettings, + Client, ClientConfig, LoopCtrl, SyncSettings, }; use url::Url; use wasm_bindgen::prelude::*; @@ -41,7 +41,7 @@ impl WasmBot { self.0.room_send(&room_id, content, None).await.unwrap(); } } - async fn on_sync_response(&self, response: SyncResponse) { + async fn on_sync_response(&self, response: SyncResponse) -> LoopCtrl { console::log_1(&"Synced".to_string().into()); for (room_id, room) in response.rooms.join { @@ -53,6 +53,8 @@ impl WasmBot { } } } + + LoopCtrl::Continue } } @@ -73,11 +75,11 @@ pub async fn run() -> Result { let bot = WasmBot(client.clone()); - client.sync(SyncSettings::default()).await.unwrap(); + client.sync_once(SyncSettings::default()).await.unwrap(); let settings = SyncSettings::default().token(client.sync_token().await.unwrap()); client - .sync_forever(settings, |response| bot.on_sync_response(response)) + .sync_with_callback(settings, |response| bot.on_sync_response(response)) .await; Ok(JsValue::NULL) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 0c08c1a0..4a994098 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -47,6 +47,20 @@ use matrix_sdk_base::crypto::{ AttachmentEncryptor, OutgoingRequests, ToDeviceRequest, }; +/// Enum controlling if a loop running callbacks should continue or abort. +/// +/// This is mainly used in the [`sync_with_callback`] method, the return value +/// of the provided callback controls if the sync loop should be exited. +/// +/// [`sync_with_callback`]: #method.sync_with_callback +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LoopCtrl { + /// Continue running the loop. + Continue, + /// Break out of the loop. + Break, +} + use matrix_sdk_common::{ api::r0::{ account::register, @@ -1410,7 +1424,7 @@ impl Client { /// /// [`sync_with_callback`]: #method.sync_with_callback pub async fn sync(&self, sync_settings: SyncSettings<'_>) { - self.sync_with_callback(sync_settings, |_| async { false }) + self.sync_with_callback(sync_settings, |_| async { LoopCtrl::Continue }) .await } @@ -1435,18 +1449,17 @@ impl Client { /// /// ```compile_fail,E0658 /// # use matrix_sdk::events::{ - /// # collections::all::RoomEvent, /// # room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, - /// # EventResult, /// # }; /// # use matrix_sdk::Room; /// # use std::sync::{Arc, RwLock}; - /// # use matrix_sdk::{Client, SyncSettings}; + /// # use std::time::Duration; + /// # use matrix_sdk::{Client, SyncSettings, LoopCtrl}; /// # use url::Url; /// # use futures::executor::block_on; /// # block_on(async { /// # let homeserver = Url::parse("http://localhost:8080").unwrap(); - /// # let mut client = Client::new(homeserver, None).unwrap(); + /// # let mut client = Client::new(homeserver).unwrap(); /// /// use async_std::sync::channel; /// @@ -1454,22 +1467,21 @@ impl Client { /// /// let sync_channel = &tx; /// let sync_settings = SyncSettings::new() - /// .timeout(30_000) - /// .unwrap(); + /// .timeout(Duration::from_secs(30)); /// /// client - /// .sync_forever(sync_settings, async move |response| { + /// .sync_with_callback(sync_settings, async move |response| { /// let channel = sync_channel; /// /// for (room_id, room) in response.rooms.join { /// for event in room.timeline.events { - /// if let EventResult::Ok(e) = event { + /// if let Ok(e) = event.deserialize() { /// channel.send(e).await; /// } /// } /// } /// - /// false + /// LoopCtrl::Continue /// }) /// .await; /// }) @@ -1480,7 +1492,7 @@ impl Client { sync_settings: SyncSettings<'_>, callback: impl Fn(sync_events::Response) -> C, ) where - C: Future, + C: Future, { let mut sync_settings = sync_settings; let filter = sync_settings.filter; @@ -1531,7 +1543,7 @@ impl Client { } } - if callback(response).await { + if callback(response).await == LoopCtrl::Break { return; } diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index 3dd243b5..078de2e1 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -71,7 +71,7 @@ mod device; #[cfg(feature = "encryption")] mod sas; -pub use client::{Client, ClientConfig, SyncSettings}; +pub use client::{Client, ClientConfig, LoopCtrl, SyncSettings}; #[cfg(feature = "encryption")] #[cfg_attr(feature = "docs", doc(cfg(encryption)))] pub use device::Device;