Alex Mikhalev
4 years ago
3 changed files with 53 additions and 0 deletions
@ -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; |
||||||
|
@ -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…
Reference in new issue