Using config in engine
This commit is contained in:
+13
-8
@@ -1,18 +1,20 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::terminal::TerminalServer;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
const WATCH_LIST_SYMBOLS: &[&str] = &["BTC", "ETH", "SOL", "XRP"];
|
||||
use crate::{config::Config, terminal::TerminalServer};
|
||||
|
||||
pub struct Engine {
|
||||
pub terminal_server: Arc<TerminalServer>,
|
||||
pub config: Arc<Mutex<Config>>,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pub async fn new() -> tokio::io::Result<Self> {
|
||||
Ok(Self {
|
||||
terminal_server: TerminalServer::new(),
|
||||
}
|
||||
config: Arc::new(Mutex::new(Config::new().await?)),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn spawn_terminal_server(&self) {
|
||||
@@ -26,8 +28,9 @@ impl Engine {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn spawn_broadcaster(&mut self) {
|
||||
pub fn spawn_broadcaster(&self) {
|
||||
let terminal_server = self.terminal_server.clone();
|
||||
let config = self.config.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut refresh = tokio::time::interval(tokio::time::Duration::from_secs(5));
|
||||
@@ -35,7 +38,9 @@ impl Engine {
|
||||
loop {
|
||||
refresh.tick().await;
|
||||
|
||||
match crate::fetch::fetch_watch_list(WATCH_LIST_SYMBOLS).await {
|
||||
let watch_list = &config.lock().await.watchlist.symbols;
|
||||
|
||||
match crate::fetch::fetch_watch_list(watch_list).await {
|
||||
Ok(watch_list) => {
|
||||
if let Err(error) = terminal_server
|
||||
.broadcast(
|
||||
@@ -54,7 +59,7 @@ impl Engine {
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn run_engine(&mut self) -> tokio::io::Result<()> {
|
||||
pub async fn run_engine(&self) -> tokio::io::Result<()> {
|
||||
loop {
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user