From e21806f3cc5b10cb9010978d568405d1d01f6ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 4 Dec 2019 22:33:26 +0100 Subject: [PATCH] async_client: Add the sync_forever method. --- src/async_client.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/async_client.rs b/src/async_client.rs index 3db767a8..1d07bb01 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -2,6 +2,9 @@ use futures::future::{BoxFuture, Future, FutureExt}; use std::convert::{TryFrom, TryInto}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex, RwLock}; +use std::time::Duration; + +use async_std::task::sleep; use http::Method as HttpMethod; use http::Response as HttpResponse; @@ -275,7 +278,37 @@ impl AsyncClient { Ok(response) } - async fn sync_forever() {} + pub async fn sync_forever( + &mut self, + sync_settings: SyncSettings, + callback: impl Fn(sync_events::IncomingResponse) -> C + Send, + ) where + C: Future, + { + let mut sync_settings = sync_settings; + + loop { + let response = self.sync(sync_settings.clone()).await; + + // TODO query keys here. + // TODO upload keys here + // TODO send out to-device messages here + + let response = if let Ok(r) = response { + r + } else { + sleep(Duration::from_secs(1)).await; + continue; + }; + + callback(response).await; + + sync_settings = SyncSettings::new() + .timeout(30000) + .unwrap() + .token(self.sync_token().unwrap()); + } + } async fn send( &self,