Skip to content

Commit

Permalink
Merge branch 'new-sync-methods' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
poljar committed Oct 6, 2020
2 parents b58d88e + 09f4b07 commit bc48674
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 50 deletions.
1 change: 1 addition & 0 deletions matrix_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ features = ["wasm-bindgen"]

[dev-dependencies]
async-trait = "0.1.40"
async-std = { version = "1.6.5", 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"] }
Expand Down
4 changes: 1 addition & 3 deletions matrix_sdk/examples/autojoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ async fn login_and_sync(
.add_event_emitter(Box::new(AutoJoinBot::new(client.clone())))
.await;

client
.sync_forever(SyncSettings::default(), |_| async {})
.await;
client.sync(SyncSettings::default()).await;

Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions matrix_sdk/examples/command_bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -91,18 +91,18 @@ async fn login_and_sync(
// An initial sync to set up state and so our bot doesn't respond to old messages.
// If the `StateStore` finds saved state in the location given the initial sync will
// be skipped in favor of loading state from the store
client.sync(SyncSettings::default()).await.unwrap();
client.sync_once(SyncSettings::default()).await.unwrap();
// add our CommandBot to be notified of incoming messages, we do this after the initial
// sync to avoid responding to messages before the bot was running.
client
.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_forever(settings, |_| async {}).await;
client.sync(settings).await;

Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions matrix_sdk/examples/emoji_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -69,7 +70,7 @@ async fn login(
let client_ref = &client;

client
.sync_forever(SyncSettings::new(), |response| async move {
.sync_with_callback(SyncSettings::new(), |response| async move {
let client = &client_ref;

for event in &response.to_device.events {
Expand Down Expand Up @@ -116,6 +117,8 @@ async fn login(
_ => (),
}
}

LoopCtrl::Continue
})
.await;

Expand Down
4 changes: 2 additions & 2 deletions matrix_sdk/examples/image_bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ async fn login_and_sync(
.login(&username, &password, None, Some("command bot"))
.await?;

client.sync(SyncSettings::default()).await.unwrap();
client.sync_once(SyncSettings::default()).await.unwrap();
client
.add_event_emitter(Box::new(ImageBot::new(client.clone(), image)))
.await;

let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
client.sync_forever(settings, |_| async {}).await;
client.sync(settings).await;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion matrix_sdk/examples/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn login(
client
.login(username, password, None, Some("rust-sdk"))
.await?;
client.sync_forever(SyncSettings::new(), |_| async {}).await;
client.sync(SyncSettings::new()).await;

Ok(())
}
Expand Down
10 changes: 6 additions & 4 deletions matrix_sdk/examples/wasm_command_bot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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 {
Expand All @@ -53,6 +53,8 @@ impl WasmBot {
}
}
}

LoopCtrl::Continue
}
}

Expand All @@ -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)
Expand Down
Loading

0 comments on commit bc48674

Please sign in to comment.