Browse Source

Start adding state manager interface

master
Alex Mikhalev 4 years ago
parent
commit
898f5c7580
  1. 1
      sprinklers_actors/Cargo.toml
  2. 2
      sprinklers_actors/src/lib.rs
  3. 50
      sprinklers_actors/src/state_manager.rs

1
sprinklers_actors/Cargo.toml

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

2
sprinklers_actors/src/lib.rs

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

50
sprinklers_actors/src/state_manager.rs

@ -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()
}
}
Loading…
Cancel
Save