Add timeout parameter to sync, update doctest

This commit is contained in:
Jonas Platte 2020-04-14 19:49:29 +02:00
parent c253265fd6
commit a207807f59
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 25 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use std::{env, process::exit};
use std::{env, process::exit, time::Duration};
use futures_util::stream::{StreamExt as _, TryStreamExt as _};
use ruma_client::{
@ -21,11 +21,18 @@ async fn log_messages(
client.log_in(username, password, None, None).await?;
// TODO: This is a horrible way to obtain an initial next_batch token that generates way too
// much server load and network traffic. Fix this!
// Skip initial sync reponse vvvvvvvv
let mut sync_stream = Box::pin(client.sync(None, None, SetPresence::Online).skip(1));
let mut sync_stream = Box::pin(
client
.sync(
None,
None,
SetPresence::Online,
Some(Duration::from_secs(30)),
)
// TODO: This is a horrible way to obtain an initial next_batch token that generates way
// too much server load and network traffic. Fix this!
.skip(1),
);
while let Some(res) = sync_stream.try_next().await? {
// Only look at rooms the user hasn't left yet

View File

@ -29,12 +29,20 @@
//! events), use the `Client::sync`:
//!
//! ```no_run
//! use std::time::Duration;
//!
//! # use futures_util::stream::{StreamExt as _, TryStreamExt as _};
//! # use ruma_client::{api::r0::sync::sync_events::SetPresence, Client};
//! # let homeserver_url = "https://example.com".parse().unwrap();
//! # let client = Client::https(homeserver_url, None);
//! # let next_batch_token = String::new();
//! # async {
//! let mut sync_stream = Box::pin(client.sync(None, None, SetPresence::Online));
//! let mut sync_stream = Box::pin(client.sync(
//! None,
//! Some(next_batch_token),
//! SetPresence::Online,
//! Some(Duration::from_secs(30)),
//! ));
//! while let Some(response) = sync_stream.try_next().await? {
//! // Do something with the data in the response...
//! }
@ -86,6 +94,7 @@ use std::{
convert::TryFrom,
str::FromStr,
sync::{Arc, Mutex},
time::Duration,
};
use futures_core::{
@ -298,6 +307,7 @@ where
filter: Option<api::r0::sync::sync_events::Filter>,
since: Option<String>,
set_presence: api::r0::sync::sync_events::SetPresence,
timeout: Option<Duration>,
) -> impl Stream<Item = Result<api::r0::sync::sync_events::IncomingResponse, Error>>
+ TryStream<Ok = api::r0::sync::sync_events::IncomingResponse, Error = Error> {
use api::r0::sync::sync_events;
@ -314,7 +324,7 @@ where
since,
full_state: false,
set_presence,
timeout: None,
timeout,
})
.await?;