use std::{
sync::{
atomic::{AtomicBool, Ordering},
mpsc::Sender,
Arc,
},
time::Duration,
};
use crate::{error::FmtError, utils::assets::get_all_assets};
use super::Event;
pub async fn watch_assets(transmitter: Sender<Event>, shutdown_signal: Arc<AtomicBool>) {
let query_interval_milli = 2000;
let thread_sleep_duration_milli = 100;
let mut counter = query_interval_milli;
while !shutdown_signal.load(Ordering::Relaxed) {
if counter >= query_interval_milli {
let _ = match get_all_assets().await {
Ok((wallet_address, assets)) => {
transmitter.send(Event::AssetsUpdate(wallet_address, assets))
}
Err(error) => transmitter.send(Event::AssetsUpdateError(
error.fmt_err("AssetsUpdateError"),
matches!(
error,
crate::Error::CurrentAccountNotSet | crate::Error::AlchemyApiKeyNotSet
),
)),
};
counter = 0;
}
counter += thread_sleep_duration_milli;
tokio::time::sleep(Duration::from_millis(thread_sleep_duration_milli)).await;
}
}