You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.3 KiB
75 lines
1.3 KiB
4 years ago
|
//! State
|
||
|
use super::*;
|
||
|
use tokio::{
|
||
|
sync::{
|
||
|
watch,
|
||
|
},
|
||
|
};
|
||
|
use config::Config;
|
||
|
|
||
|
#[derive(Debug, Clone)]
|
||
|
pub struct State
|
||
|
{
|
||
|
config: Arc<Config>, //to avoid cloning config
|
||
|
chain: Arc<RwLock<Chain<String>>>,
|
||
|
save: Arc<Notify>,
|
||
|
|
||
|
shutdown: Arc<watch::Sender<bool>>,
|
||
|
shutdown_recv: watch::Receiver<bool>,
|
||
|
}
|
||
|
|
||
|
impl State
|
||
|
{
|
||
|
pub fn new(config: Config, chain: Arc<RwLock<Chain<String>>>, save: Arc<Notify>) -> Self
|
||
|
{
|
||
|
let (shutdown, shutdown_recv) = watch::channel(false);
|
||
|
Self {
|
||
|
config: Arc::new(config),
|
||
|
chain,
|
||
|
save,
|
||
|
shutdown: Arc::new(shutdown),
|
||
|
shutdown_recv,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn config(&self) -> &Config
|
||
|
{
|
||
|
self.config.as_ref()
|
||
|
}
|
||
|
|
||
|
pub fn notify_save(&self)
|
||
|
{
|
||
|
self.save.notify();
|
||
|
}
|
||
|
|
||
|
pub fn chain(&self) -> &RwLock<Chain<String>>
|
||
|
{
|
||
|
&self.chain.as_ref()
|
||
|
}
|
||
|
|
||
|
pub fn when(&self) -> &Arc<Notify>
|
||
|
{
|
||
|
&self.save
|
||
|
}
|
||
|
|
||
|
pub fn shutdown(self)
|
||
|
{
|
||
|
self.shutdown.broadcast(true).expect("Failed to communicate shutdown");
|
||
|
self.save.notify();
|
||
|
}
|
||
|
|
||
|
pub fn has_shutdown(&self) -> bool
|
||
|
{
|
||
|
*self.shutdown_recv.borrow()
|
||
|
}
|
||
|
|
||
|
pub async fn on_shutdown(mut self)
|
||
|
{
|
||
|
if !self.has_shutdown() {
|
||
|
while let Some(false) = self.shutdown_recv.recv().await {
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|