diff options
Diffstat (limited to 'src/app/mod.rs')
-rw-r--r-- | src/app/mod.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/app/mod.rs b/src/app/mod.rs new file mode 100644 index 00000000..27846065 --- /dev/null +++ b/src/app/mod.rs @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2023 Robin Jarry + +use std::collections::HashMap; +use std::sync::Arc; + +use tokio::sync::mpsc::channel; +use tokio::sync::mpsc::Receiver; +use tokio::sync::mpsc::Sender; +use tokio::sync::Mutex; +use tokio::task::JoinHandle; + +use crate::config::AccountConfig; +use crate::config::Config; +use crate::worker::Worker; +use crate::worker::WorkerMessage; + +pub struct App { + config: Arc<Mutex<Config>>, + worker_tasks: Vec<JoinHandle<()>>, + to_workers: HashMap<String, Sender<WorkerMessage>>, + from_workers: HashMap<String, Receiver<WorkerMessage>>, + // app internal state change notifications for UI + updates: Sender<()>, +} + +impl App { + pub fn new(config: Arc<Mutex<Config>>, updates: Sender<()>) -> Self { + App { + config, + updates, + worker_tasks: Vec::new(), + to_workers: HashMap::new(), + from_workers: HashMap::new(), + } + } + + pub fn invalidate(&self) { + // ignore if a pending tick is already present in the channel + let _ = self.updates.try_send(()); + } + + pub fn start_worker(&mut self, config: AccountConfig) { + let (from_worker_tx, from_worker_rx) = channel::<WorkerMessage>(32); + let (to_worker_tx, to_worker_rx) = channel::<WorkerMessage>(32); + + self.to_workers.insert(config.name.clone(), to_worker_tx); + self.from_workers + .insert(config.name.clone(), from_worker_rx); + + let handle = tokio::spawn(async move { + let worker = Worker::new(config, to_worker_rx, from_worker_tx); + let run = worker.run(); + tokio::pin!(run); + run.await; + }); + self.worker_tasks.push(handle); + } + + pub async fn stop_workers(&self) {} +} |