config: don't spawn useless extra thread

This commit is contained in:
Alex Janka 2024-08-02 13:57:00 +10:00
parent 8322bbc051
commit 3e1c4df585
5 changed files with 30 additions and 31 deletions

2
Cargo.lock generated
View file

@ -789,7 +789,7 @@ dependencies = [
[[package]] [[package]]
name = "sway-flash-indicator" name = "sway-flash-indicator"
version = "0.4.0" version = "0.4.1"
dependencies = [ dependencies = [
"directories", "directories",
"futures-util", "futures-util",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "sway-flash-indicator" name = "sway-flash-indicator"
version = "0.4.0" version = "0.4.1"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com> # Maintainer: Alex Janka <alex@alexjanka.com>
pkgname=sway-flash-indicator pkgname=sway-flash-indicator
pkgver=0.4.0 pkgver=0.4.1
pkgrel=1 pkgrel=1
pkgdesc="flashes sway indicator border rather than always showing it" pkgdesc="flashes sway indicator border rather than always showing it"
arch=('x86_64' 'aarch64') arch=('x86_64' 'aarch64')

View file

@ -49,7 +49,10 @@ impl ConfigHandle {
} }
} }
pub async fn parse_config() -> Res<()> { #[allow(dead_code)]
pub struct WatchHandle(notify_debouncer_mini::Debouncer<notify::INotifyWatcher>);
pub async fn parse_config() -> Res<WatchHandle> {
let dirs = directories::ProjectDirs::from("com", "alexjanka", "sway-flash-indicator") let dirs = directories::ProjectDirs::from("com", "alexjanka", "sway-flash-indicator")
.ok_or(Error::NoMatchingConfig)?; .ok_or(Error::NoMatchingConfig)?;
@ -72,36 +75,32 @@ pub async fn parse_config() -> Res<()> {
c c
}; };
let path = config_path.clone(); let path = config_path.clone();
tokio::task::spawn(async move {
let mut debouncer = notify_debouncer_mini::new_debouncer(
std::time::Duration::from_secs(1),
move |res: notify_debouncer_mini::DebounceEventResult| match res {
Ok(events) => events.iter().for_each(|e| {
if e.kind == notify_debouncer_mini::DebouncedEventKind::Any {
if let Some(config) = read_config(&path) {
CONFIG.set_blocking(config);
} else {
log::error!("failed to parse config");
}
}
}),
Err(e) => println!("Error {:?}", e),
},
)
.unwrap();
debouncer let mut debouncer = notify_debouncer_mini::new_debouncer(
.watcher() std::time::Duration::from_secs(1),
.watch(&config_path, notify::RecursiveMode::Recursive) move |res: notify_debouncer_mini::DebounceEventResult| match res {
.unwrap(); Ok(events) => events.iter().for_each(|e| {
loop { if e.kind == notify_debouncer_mini::DebouncedEventKind::Any {
tokio::time::sleep(std::time::Duration::from_secs(10)).await; if let Some(config) = read_config(&path) {
} CONFIG.set_blocking(config);
}); } else {
log::error!("failed to parse config");
}
}
}),
Err(e) => println!("Error {:?}", e),
},
)
.unwrap();
debouncer
.watcher()
.watch(&config_path, notify::RecursiveMode::Recursive)
.unwrap();
crate::CONFIG.set(config).await; crate::CONFIG.set(config).await;
Ok(()) Ok(WatchHandle(debouncer))
} }
fn read_config(path: &std::path::PathBuf) -> Option<Config> { fn read_config(path: &std::path::PathBuf) -> Option<Config> {

View file

@ -26,7 +26,7 @@ async fn main() -> Res<()> {
} }
pretty_env_logger::init(); pretty_env_logger::init();
config::parse_config().await?; let _watch_handle = config::parse_config().await?;
let mut event_connection = swayipc_async::Connection::new().await?; let mut event_connection = swayipc_async::Connection::new().await?;
let mut autosplit_connection = swayipc_async::Connection::new().await?; let mut autosplit_connection = swayipc_async::Connection::new().await?;