Add timeout parameter to sync, update doctest
This commit is contained in:
parent
c253265fd6
commit
a207807f59
@ -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 futures_util::stream::{StreamExt as _, TryStreamExt as _};
|
||||||
use ruma_client::{
|
use ruma_client::{
|
||||||
@ -21,11 +21,18 @@ async fn log_messages(
|
|||||||
|
|
||||||
client.log_in(username, password, None, None).await?;
|
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
|
let mut sync_stream = Box::pin(
|
||||||
// much server load and network traffic. Fix this!
|
client
|
||||||
|
.sync(
|
||||||
// Skip initial sync reponse vvvvvvvv
|
None,
|
||||||
let mut sync_stream = Box::pin(client.sync(None, None, SetPresence::Online).skip(1));
|
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? {
|
while let Some(res) = sync_stream.try_next().await? {
|
||||||
// Only look at rooms the user hasn't left yet
|
// Only look at rooms the user hasn't left yet
|
||||||
|
14
src/lib.rs
14
src/lib.rs
@ -29,12 +29,20 @@
|
|||||||
//! events), use the `Client::sync`:
|
//! events), use the `Client::sync`:
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
|
//! use std::time::Duration;
|
||||||
|
//!
|
||||||
//! # use futures_util::stream::{StreamExt as _, TryStreamExt as _};
|
//! # use futures_util::stream::{StreamExt as _, TryStreamExt as _};
|
||||||
//! # use ruma_client::{api::r0::sync::sync_events::SetPresence, Client};
|
//! # use ruma_client::{api::r0::sync::sync_events::SetPresence, Client};
|
||||||
//! # let homeserver_url = "https://example.com".parse().unwrap();
|
//! # let homeserver_url = "https://example.com".parse().unwrap();
|
||||||
//! # let client = Client::https(homeserver_url, None);
|
//! # let client = Client::https(homeserver_url, None);
|
||||||
|
//! # let next_batch_token = String::new();
|
||||||
//! # async {
|
//! # 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? {
|
//! while let Some(response) = sync_stream.try_next().await? {
|
||||||
//! // Do something with the data in the response...
|
//! // Do something with the data in the response...
|
||||||
//! }
|
//! }
|
||||||
@ -86,6 +94,7 @@ use std::{
|
|||||||
convert::TryFrom,
|
convert::TryFrom,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures_core::{
|
use futures_core::{
|
||||||
@ -298,6 +307,7 @@ where
|
|||||||
filter: Option<api::r0::sync::sync_events::Filter>,
|
filter: Option<api::r0::sync::sync_events::Filter>,
|
||||||
since: Option<String>,
|
since: Option<String>,
|
||||||
set_presence: api::r0::sync::sync_events::SetPresence,
|
set_presence: api::r0::sync::sync_events::SetPresence,
|
||||||
|
timeout: Option<Duration>,
|
||||||
) -> impl Stream<Item = Result<api::r0::sync::sync_events::IncomingResponse, Error>>
|
) -> impl Stream<Item = Result<api::r0::sync::sync_events::IncomingResponse, Error>>
|
||||||
+ TryStream<Ok = api::r0::sync::sync_events::IncomingResponse, Error = Error> {
|
+ TryStream<Ok = api::r0::sync::sync_events::IncomingResponse, Error = Error> {
|
||||||
use api::r0::sync::sync_events;
|
use api::r0::sync::sync_events;
|
||||||
@ -314,7 +324,7 @@ where
|
|||||||
since,
|
since,
|
||||||
full_state: false,
|
full_state: false,
|
||||||
set_presence,
|
set_presence,
|
||||||
timeout: None,
|
timeout,
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user