Skip to content

Commit 0a1317d

Browse files
committed
Initialize synchronizers only once
1 parent 2892671 commit 0a1317d

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

src/asynch/dropbox.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ lazy_static! {
7979
}
8080

8181
/// A Dropbox synchronizer
82+
#[derive(Clone)]
8283
pub(crate) struct Synchronizer {
8384
/// The configuration needed for this synchronizer
8485
conf: DropboxConfiguration,
@@ -90,24 +91,21 @@ pub(crate) struct Synchronizer {
9091
version_local: Option<i64>,
9192
/// The version that was set during the last sync
9293
last_sync_version: Option<i64>,
93-
/// The factory that creates HTTP clients
94-
client_factory: Box<dyn RklHttpAsyncFactory<ClientResType = Vec<u8>>>,
9594
}
9695

9796
impl Synchronizer {
9897
pub(crate) fn new(
9998
dbc: &DropboxConfiguration,
10099
sys_conf: &SystemConfiguration,
101100
f: &str,
102-
client_factory: Box<dyn RklHttpAsyncFactory<ClientResType = Vec<u8>>>,
101+
//client_factory: Box<dyn RklHttpAsyncFactory<ClientResType = Vec<u8>>>,
103102
) -> errors::Result<Synchronizer> {
104103
let s = Synchronizer {
105104
conf: dbc.clone(),
106105
file_name: f.to_string(),
107106
saved_at_local: sys_conf.saved_at,
108107
version_local: sys_conf.version,
109108
last_sync_version: sys_conf.last_sync_version,
110-
client_factory,
111109
};
112110
Ok(s)
113111
}
@@ -127,7 +125,8 @@ impl Synchronizer {
127125
let saved_at_local = self.saved_at_local.clone();
128126
let version_local = self.version_local.clone();
129127
let last_sync_version = self.last_sync_version.clone();
130-
let mut client = self.client_factory.create();
128+
let client_factory = Box::new(ReqwestClientFactory::new());
129+
let mut client = client_factory.create();
131130
client.header(
132131
"Authorization",
133132
&format!("Bearer {}", self.use_short_lived_token()?.as_str()),
@@ -151,7 +150,8 @@ impl Synchronizer {
151150
}
152151

153152
async fn get_short_lived_token(&self) -> errors::Result<Zeroizing<String>> {
154-
let mut client = self.client_factory.create();
153+
let client_factory = Box::new(ReqwestClientFactory::new());
154+
let mut client = client_factory.create();
155155
debug!("DBX: Retrieving a short-lived token");
156156

157157
let post_body: Vec<u8> = format!(

src/asynch/nextcloud.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ lazy_static! {
4141
}
4242

4343
/// A (Next/Own)cloud synchronizer
44+
#[derive(Clone)]
4445
pub(crate) struct Synchronizer {
4546
/// The configuration needed for this synchronizer
4647
conf: NextcloudConfiguration,

src/lib.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use std::time::Duration;
4545

4646
pub use api::GeneralConfiguration;
4747
use async_trait::async_trait;
48-
use asynch::{AsyncTask, ReqwestClientFactory, SyncStatus};
48+
use asynch::{AsyncTask, SyncStatus};
4949
use futures::{future, select, FutureExt};
5050
use log::*;
5151

@@ -143,6 +143,8 @@ struct CoreLogicHandler {
143143
// Keeps the sensitive data
144144
safe: Safe,
145145
configuration: RklConfiguration,
146+
dbx_synchronizer: dropbox::Synchronizer,
147+
nc_synchronizer: nextcloud::Synchronizer,
146148
// Signals changes that are not saved
147149
contents_changed: bool,
148150
cryptor: datacrypt::BcryptAes,
@@ -187,12 +189,30 @@ impl CoreLogicHandler {
187189
user_selection = us;
188190
cr
189191
};
192+
193+
// Initialize the synchronizers
194+
let mut nc_synchronizer = nextcloud::Synchronizer::new(
195+
&configuration.nextcloud,
196+
&configuration.system,
197+
FILENAME
198+
).unwrap();
199+
nc_synchronizer.init().await;
200+
201+
let mut dbx_synchronizer = dropbox::Synchronizer::new(
202+
&configuration.dropbox,
203+
&configuration.system,
204+
FILENAME
205+
).unwrap();
206+
dbx_synchronizer.init().await;
207+
190208
CoreLogicHandler {
191209
editor,
192210
props: props,
193211
user_selection,
194212
safe,
195213
configuration,
214+
dbx_synchronizer,
215+
nc_synchronizer,
196216
contents_changed,
197217
cryptor,
198218
}
@@ -788,25 +808,11 @@ Warning: Saving will discard all the entries that could not be recovered.
788808
}
789809
};
790810

791-
// Initialize the synchronizers
792-
let mut nc_synchronizer = nextcloud::Synchronizer::new(
793-
&s.configuration.nextcloud,
794-
&s.configuration.system,
795-
FILENAME
796-
).unwrap();
797-
nc_synchronizer.init().await;
798-
799-
let mut dbx_synchronizer = dropbox::Synchronizer::new(
800-
&s.configuration.dropbox,
801-
&s.configuration.system,
802-
FILENAME,
803-
Box::new(ReqwestClientFactory::new())
804-
).unwrap();
805-
dbx_synchronizer.init().await;
806-
807811
// Prepare all the possible futures from which we expect possible user_selection
808-
let mut nc_future = nc_synchronizer.execute().fuse();
809-
let mut dbx_future = dbx_synchronizer.execute().fuse();
812+
let nc_synchronizer_clonne = s.nc_synchronizer.clone();
813+
let dbx_synchronizer_clone = s.dbx_synchronizer.clone();
814+
let mut nc_future = nc_synchronizer_clonne.execute().fuse();
815+
let mut dbx_future = dbx_synchronizer_clone.execute().fuse();
810816
let mut fused_user_selection_future = user_selection_future.fuse();
811817
let mut inactivity_timeout_future = Box::pin(sleep(Duration::from_secs(s.props.idle_timeout_seconds() as u64))).fuse();
812818

0 commit comments

Comments
 (0)