feat(manager) add `start_polling` function
@@ -4,6 +4,8 @@ use crate::account::{Account, AccountIdentifier, AccountInitialiser};
use crate::client::ClientOptions;
use api::{AccountSynchronizer, SyncedAccount};
use std::path::Path;
+use std::thread;
+use std::time::Duration;
/// The account manager.
///
@@ -17,6 +19,15 @@ impl<'a> AccountManager {
Default::default()
}
+ /// Starts the polling mechanism.
+ pub fn start_polling(&self) -> &Self {
+ thread::spawn(move || {
+ let _ = sync_accounts();
+ thread::sleep(Duration::from_millis(5000));
+ });
+ self
+ }
+
/// Adds a new account.
pub fn create_account(&self, client_options: ClientOptions) -> AccountInitialiser<'a> {
AccountInitialiser::new(client_options)
@@ -29,14 +40,7 @@ impl<'a> AccountManager {
/// Syncs all accounts.
pub fn sync_accounts(&self) -> crate::Result<Vec<SyncedAccount>> {
- let accounts = crate::storage::get_adapter()?.get_all()?;
- let mut synced_accounts = vec![];
- for account_str in accounts {
- let account: Account<'_> = serde_json::from_str(&account_str)?;
- let synced_account = AccountSynchronizer::new(&account).execute()?;
- synced_accounts.push(synced_account);
- }
- Ok(synced_accounts)
+ sync_accounts()
}
/// Transfers an amount from an account to another.
@@ -72,6 +76,17 @@ impl<'a> AccountManager {
}
}
+fn sync_accounts() -> crate::Result<Vec<SyncedAccount>> {
+ let accounts = crate::storage::get_adapter()?.get_all()?;
+ let mut synced_accounts = vec![];
+ for account_str in accounts {
+ let account: Account<'_> = serde_json::from_str(&account_str)?;
+ let synced_account = AccountSynchronizer::new(&account).execute()?;
+ synced_accounts.push(synced_account);
+ }
+ Ok(synced_accounts)
+}
+
#[cfg(test)]
mod tests {
use super::AccountManager;
