matrix-sdk: Switch to using an enum for the sync loop callback return value.
parent
83b48fb53c
commit
2ffac286ed
|
@ -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"] }
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<JsValue, JsValue> {
|
|||
|
||||
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)
|
||||
|
|
|
@ -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<Output = bool>,
|
||||
C: Future<Output = LoopCtrl>,
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue