-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for watching of the settings file
- Loading branch information
Showing
8 changed files
with
78 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::{ | ||
path::PathBuf, | ||
sync::{mpsc::channel, OnceLock, RwLock}, | ||
time::Duration, | ||
}; | ||
|
||
use crate::{app_settings, AppSettings}; | ||
use anyhow::Result; | ||
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher}; | ||
|
||
pub struct SettingsHandle<'a> { | ||
config_path: PathBuf, | ||
app_settings: &'a RwLock<AppSettings>, | ||
} | ||
|
||
const SETTINGS_FILE: &str = "settings.json"; | ||
|
||
impl<'a> SettingsHandle<'a> { | ||
pub fn create(config_dir: impl Into<PathBuf>) -> Result<Self> { | ||
let config_path = config_dir.into().join(SETTINGS_FILE); | ||
let app_settings = app_settings::AppSettings::load(config_path.clone())?; | ||
|
||
static CONFIG: OnceLock<RwLock<AppSettings>> = OnceLock::new(); | ||
let app_settings = CONFIG.get_or_init(|| RwLock::new(app_settings)); | ||
|
||
Ok(Self { | ||
config_path, | ||
app_settings, | ||
}) | ||
} | ||
|
||
pub fn settings(&self) -> &'a RwLock<AppSettings> { | ||
self.app_settings | ||
} | ||
|
||
pub fn watch(&self) -> Result<()> { | ||
let (tx, rx) = channel(); | ||
let mut watcher: RecommendedWatcher = Watcher::new( | ||
tx, | ||
notify::Config::default().with_poll_interval(Duration::from_secs(2)), | ||
)?; | ||
watcher.watch(self.config_path.as_path(), RecursiveMode::NonRecursive)?; | ||
|
||
loop { | ||
match rx.recv() { | ||
Ok(Ok(Event { | ||
kind: notify::event::EventKind::Modify(_), | ||
.. | ||
})) => { | ||
println!("settings.json written; refreshing configuration..."); | ||
// TODO: How can we avoid this unwrap? | ||
let mut write = self.app_settings.write().unwrap(); | ||
*write = app_settings::AppSettings::load(self.config_path.clone())?; | ||
} | ||
|
||
Err(e) => { | ||
tracing::error!("Error watching config file {:?}: {:?}", self.config_path, e) | ||
} | ||
|
||
_ => { | ||
// Noop | ||
} | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters