Start adding state manager interface

This commit is contained in:
Alex Mikhalev 2020-10-01 19:12:21 -06:00
parent 1abdad7113
commit 898f5c7580
3 changed files with 53 additions and 0 deletions

View File

@ -14,6 +14,7 @@ tracing = "0.1.19"
chrono = { version = "0.4.15" }
serde = { version = "1.0.116", features = ["derive"] }
im = "15.0.0"
eyre = "0.6.0"
[dependencies.tokio]
version = "0.2.22"

View File

@ -1,8 +1,10 @@
pub mod program_runner;
pub mod section_runner;
pub mod state_manager;
#[cfg(test)]
mod trace_listeners;
pub use program_runner::ProgramRunner;
pub use section_runner::SectionRunner;
pub use state_manager::StateManager;

View File

@ -0,0 +1,50 @@
use eyre::Result;
use sprinklers_core::model::{ProgramId, ProgramRef, ProgramUpdateData, Programs};
use tokio::sync::{mpsc, oneshot, watch};
#[derive(Debug)]
pub enum Request {
UpdateProgram {
id: ProgramId,
update: ProgramUpdateData,
resp_tx: oneshot::Sender<Result<ProgramRef>>,
},
}
#[derive(Clone)]
pub struct StateManager {
request_tx: mpsc::Sender<Request>,
programs_watch: watch::Receiver<Programs>,
}
impl StateManager {
pub fn new(
request_tx: mpsc::Sender<Request>,
programs_watch: watch::Receiver<Programs>,
) -> Self {
Self {
request_tx,
programs_watch,
}
}
pub async fn update_program(
&mut self,
id: ProgramId,
update: ProgramUpdateData,
) -> Result<ProgramRef> {
let (resp_tx, resp_rx) = oneshot::channel();
self.request_tx
.send(Request::UpdateProgram {
id,
update,
resp_tx,
})
.await?;
resp_rx.await?
}
pub fn get_programs(&self) -> watch::Receiver<Programs> {
self.programs_watch.clone()
}
}