config: not oncelock

This commit is contained in:
Alex Janka 2024-07-30 16:15:15 +10:00
parent 488f2fb99d
commit 943f371cf1
3 changed files with 37 additions and 10 deletions

View file

@ -15,7 +15,37 @@ pub struct Config {
pub output_blocklist: Vec<String>,
}
pub fn parse_config() -> Res<()> {
pub struct ConfigHandle {
inner: tokio::sync::RwLock<Option<Config>>,
}
pub struct ConfigHandleHandle<'a>(tokio::sync::RwLockReadGuard<'a, Option<Config>>);
impl std::ops::Deref for ConfigHandleHandle<'_> {
type Target = Config;
fn deref(&self) -> &Self::Target {
self.0.as_ref().unwrap()
}
}
impl ConfigHandle {
pub(super) const fn new() -> Self {
Self {
inner: tokio::sync::RwLock::const_new(None),
}
}
async fn set(&self, config: Config) {
*self.inner.write().await = Some(config)
}
pub async fn get(&self) -> ConfigHandleHandle {
ConfigHandleHandle(self.inner.read().await)
}
}
pub async fn parse_config() -> Res<()> {
let dirs = directories::ProjectDirs::from("com", "alexjanka", "sway-flash-indicator")
.ok_or(Error::NoMatchingConfig)?;
@ -38,7 +68,7 @@ pub fn parse_config() -> Res<()> {
c
};
crate::CONFIG.set(config)?;
crate::CONFIG.set(config).await;
Ok(())
}

View file

@ -4,7 +4,7 @@ pub async fn interpolate_task() -> Res<()> {
let mut connection = swayipc_async::Connection::new().await?;
let to = crate::DEFAULT_BORDER.get().ok_or(Error::NoMatchingConfig)?;
let config = CONFIG.get().ok_or(Error::NoMatchingConfig)?;
let config = CONFIG.get().await;
let per_frame = 1.0 / config.frames_anim as f32;
let (d_l, d_a, d_b) = (
(to.l - config.flash_colour.l) * per_frame,

View file

@ -1,4 +1,3 @@
use config::Config;
use futures_util::StreamExt;
use lab::Lab;
@ -13,8 +12,7 @@ pub mod prelude {
}
use prelude::*;
pub static CONFIG: tokio::sync::OnceCell<Config> = tokio::sync::OnceCell::const_new();
pub static CONFIG: config::ConfigHandle = config::ConfigHandle::new();
static DEFAULT_COLOURS: tokio::sync::OnceCell<String> = tokio::sync::OnceCell::const_new();
static DEFAULT_COLOURS_NO_INDICATOR: tokio::sync::OnceCell<String> =
@ -28,7 +26,7 @@ async fn main() -> Res<()> {
}
pretty_env_logger::init();
config::parse_config()?;
config::parse_config().await?;
let mut event_connection = swayipc_async::Connection::new().await?;
let mut autosplit_connection = swayipc_async::Connection::new().await?;
@ -84,7 +82,7 @@ async fn main() -> Res<()> {
if let Err(e) = code_trigger(
&mut autosplit_connection,
id,
&CONFIG.get().unwrap().output_blocklist,
&CONFIG.get().await.output_blocklist,
)
.await
{
@ -101,8 +99,7 @@ async fn main() -> Res<()> {
continue;
}
let ratio = (width as f64) / (height as f64);
let autosplit_ratio =
CONFIG.get().ok_or(Error::NoMatchingConfig)?.autosplit_ratio;
let autosplit_ratio = CONFIG.get().await.autosplit_ratio;
if let Err(e) = if ratio > autosplit_ratio {
autosplit_connection.run_command("splith").await
} else {