diff --git a/packages/desktop/src/providers/battery/battery_provider.rs b/packages/desktop/src/providers/battery/battery_provider.rs index 567425b3..4f0b33d6 100644 --- a/packages/desktop/src/providers/battery/battery_provider.rs +++ b/packages/desktop/src/providers/battery/battery_provider.rs @@ -10,9 +10,7 @@ use starship_battery::{ use crate::{ common::SyncInterval, - providers::{ - CommonProviderState, Provider, ProviderOutput, RuntimeType, - }, + providers::{CommonProviderState, Provider, RuntimeType}, }; #[derive(Deserialize, Debug)] @@ -83,11 +81,7 @@ impl Provider for BatteryProvider { loop { interval.tick(); - - self - .common - .emit_tx - .send(ProviderOutput::Battery(self.run_interval())); + self.common.emit_output(self.run_interval()); } } } diff --git a/packages/desktop/src/providers/cpu/cpu_provider.rs b/packages/desktop/src/providers/cpu/cpu_provider.rs index d1f5954e..bb280d5f 100644 --- a/packages/desktop/src/providers/cpu/cpu_provider.rs +++ b/packages/desktop/src/providers/cpu/cpu_provider.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; use crate::{ - - providers::{CommonProviderState, ProviderOutput}, + common::SyncInterval, + providers::{CommonProviderState, Provider, RuntimeType}, }; #[derive(Deserialize, Debug)] @@ -34,13 +34,11 @@ impl CpuProvider { CpuProvider { config, common } } - - - async fn run_interval(&self) -> anyhow::Result { - let mut sysinfo = self.common.sysinfo.lock().await; + fn run_interval(&self) -> anyhow::Result { + let mut sysinfo = self.common.sysinfo.blocking_lock(); sysinfo.refresh_cpu(); - Ok(ProviderOutput::Cpu(CpuOutput { + Ok(CpuOutput { usage: sysinfo.global_cpu_info().cpu_usage(), frequency: sysinfo.global_cpu_info().frequency(), logical_core_count: sysinfo.cpus().len(), @@ -48,8 +46,21 @@ impl CpuProvider { .physical_core_count() .unwrap_or(sysinfo.cpus().len()), vendor: sysinfo.global_cpu_info().vendor_id().into(), - })) + }) } } -impl_interval_provider!(CpuProvider, true); +impl Provider for CpuProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Sync + } + + fn start_sync(&mut self) { + let mut interval = SyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick(); + self.common.emit_output(self.run_interval()); + } + } +} diff --git a/packages/desktop/src/providers/disk/disk_provider.rs b/packages/desktop/src/providers/disk/disk_provider.rs index 5f214f5d..dfd1d6fb 100644 --- a/packages/desktop/src/providers/disk/disk_provider.rs +++ b/packages/desktop/src/providers/disk/disk_provider.rs @@ -1,13 +1,9 @@ -use std::sync::Arc; - use serde::{Deserialize, Serialize}; use sysinfo::Disks; -use tokio::sync::Mutex; use crate::{ - common::{to_iec_bytes, to_si_bytes}, - - providers::{CommonProviderState, ProviderOutput}, + common::{to_iec_bytes, to_si_bytes, SyncInterval}, + providers::{CommonProviderState, Provider, RuntimeType}, }; #[derive(Deserialize, Debug)] @@ -37,7 +33,7 @@ pub struct Disk { pub struct DiskProvider { config: DiskProviderConfig, common: CommonProviderState, - disks: Arc>, + disks: Disks, } #[derive(Debug, Clone, PartialEq, Serialize)] @@ -58,17 +54,15 @@ impl DiskProvider { DiskProvider { config, common, - disks: Arc::new(Mutex::new(Disks::new_with_refreshed_list())), + disks: Disks::new_with_refreshed_list(), } } + fn run_interval(&mut self) -> anyhow::Result { + self.disks.refresh(); - - async fn run_interval(&self) -> anyhow::Result { - let mut disks = self.disks.lock().await; - disks.refresh(); - - let disks = disks + let disks = self + .disks .iter() .map(|disk| -> anyhow::Result { let name = disk.name().to_string_lossy().to_string(); @@ -87,7 +81,7 @@ impl DiskProvider { }) .collect::>>()?; - Ok(ProviderOutput::Disk(DiskOutput { disks })) + Ok(DiskOutput { disks }) } fn to_disk_size_measure(bytes: u64) -> anyhow::Result { @@ -104,4 +98,18 @@ impl DiskProvider { } } -impl_interval_provider!(DiskProvider, true); +impl Provider for DiskProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Sync + } + + fn start_sync(&mut self) { + let mut interval = SyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick(); + let output = self.run_interval(); + self.common.emit_output(output); + } + } +} diff --git a/packages/desktop/src/providers/host/host_provider.rs b/packages/desktop/src/providers/host/host_provider.rs index 4ddf4436..adf9df95 100644 --- a/packages/desktop/src/providers/host/host_provider.rs +++ b/packages/desktop/src/providers/host/host_provider.rs @@ -2,8 +2,8 @@ use serde::{Deserialize, Serialize}; use sysinfo::System; use crate::{ - - providers::{CommonProviderState, ProviderOutput}, + common::SyncInterval, + providers::{CommonProviderState, Provider, RuntimeType}, }; #[derive(Deserialize, Debug)] @@ -36,18 +36,31 @@ impl HostProvider { HostProvider { config, common } } - - - async fn run_interval(&self) -> anyhow::Result { - Ok(ProviderOutput::Host(HostOutput { + fn run_interval(&mut self) -> anyhow::Result { + Ok(HostOutput { hostname: System::host_name(), os_name: System::name(), os_version: System::os_version(), friendly_os_version: System::long_os_version(), boot_time: System::boot_time() * 1000, uptime: System::uptime() * 1000, - })) + }) } } -impl_interval_provider!(HostProvider, false); +impl Provider for HostProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Sync + } + + fn start_sync(&mut self) { + let mut interval = SyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick(); + + let output = self.run_interval(); + self.common.emit_output(output); + } + } +} diff --git a/packages/desktop/src/providers/ip/ip_provider.rs b/packages/desktop/src/providers/ip/ip_provider.rs index 1ea48f27..a0b6164b 100644 --- a/packages/desktop/src/providers/ip/ip_provider.rs +++ b/packages/desktop/src/providers/ip/ip_provider.rs @@ -3,7 +3,10 @@ use reqwest::Client; use serde::{Deserialize, Serialize}; use super::ipinfo_res::IpinfoRes; -use crate::{ providers::CommonProviderState}; +use crate::{ + common::AsyncInterval, + providers::{CommonProviderState, Provider, RuntimeType}, +}; #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -39,9 +42,7 @@ impl IpProvider { } } - - - async fn run_interval(&self) -> anyhow::Result { + async fn run_interval(&mut self) -> anyhow::Result { Self::query_ip(&self.http_client).await } @@ -71,4 +72,19 @@ impl IpProvider { } } -impl_interval_provider!(IpProvider, false); +impl Provider for IpProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Async + } + + async fn start_async(&mut self) { + let mut interval = AsyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick().await; + + let output = self.run_interval().await; + self.common.emit_output(output); + } + } +} diff --git a/packages/desktop/src/providers/keyboard/keyboard_provider.rs b/packages/desktop/src/providers/keyboard/keyboard_provider.rs index fe321a54..68d2a654 100644 --- a/packages/desktop/src/providers/keyboard/keyboard_provider.rs +++ b/packages/desktop/src/providers/keyboard/keyboard_provider.rs @@ -10,8 +10,8 @@ use windows::Win32::{ }; use crate::{ - - providers::{CommonProviderState, ProviderOutput}, + common::SyncInterval, + providers::{CommonProviderState, Provider, RuntimeType}, }; #[derive(Deserialize, Debug)] @@ -39,9 +39,7 @@ impl KeyboardProvider { KeyboardProvider { config, common } } - - - async fn run_interval(&self) -> anyhow::Result { + fn run_interval(&mut self) -> anyhow::Result { let keyboard_layout = unsafe { GetKeyboardLayout(GetWindowThreadProcessId( GetForegroundWindow(), @@ -67,10 +65,25 @@ impl KeyboardProvider { let layout_name = String::from_utf16_lossy(&locale_name[..result as usize]); - Ok(ProviderOutput::Keyboard(KeyboardOutput { + Ok(KeyboardOutput { layout: layout_name, - })) + }) } } -impl_interval_provider!(KeyboardProvider, false); +impl Provider for KeyboardProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Sync + } + + fn start_sync(&mut self) { + let mut interval = SyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick(); + + let output = self.run_interval(); + self.common.emit_output(output); + } + } +} diff --git a/packages/desktop/src/providers/komorebi/komorebi_provider.rs b/packages/desktop/src/providers/komorebi/komorebi_provider.rs index a17c0497..3b808b73 100644 --- a/packages/desktop/src/providers/komorebi/komorebi_provider.rs +++ b/packages/desktop/src/providers/komorebi/komorebi_provider.rs @@ -15,9 +15,7 @@ use super::{ KomorebiContainer, KomorebiLayout, KomorebiLayoutFlip, KomorebiMonitor, KomorebiWindow, KomorebiWorkspace, }; -use crate::providers::{ - CommonProviderState, Provider, ProviderOutput, RuntimeType, -}; +use crate::providers::{CommonProviderState, Provider, RuntimeType}; const SOCKET_NAME: &str = "zebar.sock"; @@ -79,17 +77,18 @@ impl KomorebiProvider { &String::from_utf8(buffer).unwrap(), ) { - self.common.emit_tx.send( - Ok(ProviderOutput::Komorebi(Self::transform_response( - notification.state, - ))) - .into(), - ); + self.common.emit_output(Ok(Self::transform_response( + notification.state, + ))); } } - Err(_) => self.common.emit_tx.send( - Err(anyhow::anyhow!("Failed to read Komorebi stream.")).into(), - ), + Err(_) => { + self + .common + .emit_output::(Err(anyhow::anyhow!( + "Failed to read Komorebi stream." + ))) + } } } @@ -185,7 +184,7 @@ impl Provider for KomorebiProvider { fn start_sync(&mut self) { if let Err(err) = self.create_socket() { - self.common.emit_tx.try_send(Err(err).into()); + self.common.emit_output::(Err(err)); } } } diff --git a/packages/desktop/src/providers/media/media_provider.rs b/packages/desktop/src/providers/media/media_provider.rs index 30d93f55..89b8ef7f 100644 --- a/packages/desktop/src/providers/media/media_provider.rs +++ b/packages/desktop/src/providers/media/media_provider.rs @@ -155,8 +155,8 @@ impl MediaProvider { let event_tokens = Arc::new(Mutex::new(event_tokens)); // Clean up & rebind listeners when session changes. - let session_changed_handler = TypedEventHandler::new( - move |session_manager: &Option, _| { + let session_changed_handler = + TypedEventHandler::new(move |_: &Option, _| { { let mut current_session = current_session.lock().unwrap(); let mut event_tokens = event_tokens.lock().unwrap(); @@ -191,8 +191,7 @@ impl MediaProvider { } Ok(()) - }, - ); + }); session_manager.CurrentSessionChanged(&session_changed_handler)?; @@ -286,7 +285,7 @@ impl Provider for MediaProvider { fn start_sync(&mut self) { if let Err(err) = self.create_session_manager() { - let _ = self.common.emit_tx.send(Err(err).into()); + self.common.emit_output::(Err(err)); } } } diff --git a/packages/desktop/src/providers/memory/memory_provider.rs b/packages/desktop/src/providers/memory/memory_provider.rs index efd1a36e..cabfe112 100644 --- a/packages/desktop/src/providers/memory/memory_provider.rs +++ b/packages/desktop/src/providers/memory/memory_provider.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; use crate::{ - - providers::{CommonProviderState, ProviderOutput}, + common::SyncInterval, + providers::{CommonProviderState, Provider, RuntimeType}, }; #[derive(Deserialize, Debug)] @@ -36,17 +36,15 @@ impl MemoryProvider { MemoryProvider { config, common } } - - - async fn run_interval(&self) -> anyhow::Result { - let mut sysinfo = self.common.sysinfo.lock().await; + fn run_interval(&mut self) -> anyhow::Result { + let mut sysinfo = self.common.sysinfo.blocking_lock(); sysinfo.refresh_memory(); let usage = (sysinfo.used_memory() as f32 / sysinfo.total_memory() as f32) * 100.0; - Ok(ProviderOutput::Memory(MemoryOutput { + Ok(MemoryOutput { usage, free_memory: sysinfo.free_memory(), used_memory: sysinfo.used_memory(), @@ -54,8 +52,23 @@ impl MemoryProvider { free_swap: sysinfo.free_swap(), used_swap: sysinfo.used_swap(), total_swap: sysinfo.total_swap(), - })) + }) } } -impl_interval_provider!(MemoryProvider, true); +impl Provider for MemoryProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Sync + } + + fn start_sync(&mut self) { + let mut interval = SyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick(); + + let output = self.run_interval(); + self.common.emit_output(output); + } + } +} diff --git a/packages/desktop/src/providers/network/network_provider.rs b/packages/desktop/src/providers/network/network_provider.rs index b4d3d193..b5bdecee 100644 --- a/packages/desktop/src/providers/network/network_provider.rs +++ b/packages/desktop/src/providers/network/network_provider.rs @@ -1,8 +1,5 @@ -use std::sync::Arc; - use serde::{Deserialize, Serialize}; use sysinfo::Networks; -use tokio::sync::Mutex; use super::{ wifi_hotspot::{default_gateway_wifi, WifiHotstop}, @@ -10,9 +7,8 @@ use super::{ NetworkTrafficMeasure, }; use crate::{ - common::{to_iec_bytes, to_si_bytes}, - - providers::{CommonProviderState, ProviderOutput}, + common::{to_iec_bytes, to_si_bytes, SyncInterval}, + providers::{CommonProviderState, Provider, RuntimeType}, }; #[derive(Deserialize, Debug)] @@ -33,7 +29,7 @@ pub struct NetworkOutput { pub struct NetworkProvider { config: NetworkProviderConfig, common: CommonProviderState, - netinfo: Arc>, + netinfo: Networks, } impl NetworkProvider { @@ -44,28 +40,25 @@ impl NetworkProvider { NetworkProvider { config, common, - netinfo: Arc::new(Mutex::new(Networks::new_with_refreshed_list())), + netinfo: Networks::new_with_refreshed_list(), } } - - - async fn run_interval(&self) -> anyhow::Result { - let mut netinfo = self.netinfo.lock().await; - netinfo.refresh(); + fn run_interval(&mut self) -> anyhow::Result { + self.netinfo.refresh(); let interfaces = netdev::get_interfaces(); let default_interface = netdev::get_default_interface().ok(); - let (received, total_received) = Self::bytes_received(&netinfo); + let (received, total_received) = Self::bytes_received(&self.netinfo); let received_per_sec = received / self.config.refresh_interval * 1000; let (transmitted, total_transmitted) = - Self::bytes_transmitted(&netinfo); + Self::bytes_transmitted(&self.netinfo); let transmitted_per_sec = transmitted / self.config.refresh_interval * 1000; - Ok(ProviderOutput::Network(NetworkOutput { + Ok(NetworkOutput { default_interface: default_interface .as_ref() .map(Self::transform_interface), @@ -90,7 +83,7 @@ impl NetworkProvider { total_transmitted, )?, }, - })) + }) } fn to_network_traffic_measure( @@ -200,4 +193,19 @@ impl NetworkProvider { } } -impl_interval_provider!(NetworkProvider, true); +impl Provider for NetworkProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Sync + } + + fn start_sync(&mut self) { + let mut interval = SyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick(); + + let output = self.run_interval(); + self.common.emit_output(output); + } + } +} diff --git a/packages/desktop/src/providers/provider.rs b/packages/desktop/src/providers/provider.rs index 189eaa57..324c4931 100644 --- a/packages/desktop/src/providers/provider.rs +++ b/packages/desktop/src/providers/provider.rs @@ -45,8 +45,9 @@ pub trait Provider: Send + Sync { /// Panics if wrong runtime type is used. fn call_function_sync( &self, - #[allow(unused_variables)] function: ProviderFunction, + function: ProviderFunction, ) -> anyhow::Result { + let _function = function; match self.runtime_type() { RuntimeType::Sync => { unreachable!("Sync providers must implement `call_function_sync`.") @@ -64,8 +65,9 @@ pub trait Provider: Send + Sync { /// Panics if wrong runtime type is used. async fn call_function_async( &self, - #[allow(unused_variables)] function: ProviderFunction, + function: ProviderFunction, ) -> anyhow::Result { + let _function = function; match self.runtime_type() { RuntimeType::Async => { unreachable!( diff --git a/packages/desktop/src/providers/provider_manager.rs b/packages/desktop/src/providers/provider_manager.rs index 2e216e6d..f996198d 100644 --- a/packages/desktop/src/providers/provider_manager.rs +++ b/packages/desktop/src/providers/provider_manager.rs @@ -44,11 +44,7 @@ pub struct CommonProviderState { } impl CommonProviderState { - pub fn emit(&self, emission: ProviderEmission) { - self.emit_tx.send(emission); - } - - pub async fn emit_provider_output(&self, output: anyhow::Result) + pub fn emit_output(&self, output: anyhow::Result) where T: Into, { diff --git a/packages/desktop/src/providers/weather/weather_provider.rs b/packages/desktop/src/providers/weather/weather_provider.rs index df71dc05..58d4eec5 100644 --- a/packages/desktop/src/providers/weather/weather_provider.rs +++ b/packages/desktop/src/providers/weather/weather_provider.rs @@ -2,9 +2,11 @@ use reqwest::Client; use serde::{Deserialize, Serialize}; use super::open_meteo_res::OpenMeteoRes; -use crate::providers::{ - ip::{IpProvider, IpProviderConfig}, - CommonProviderState, ProviderOutput, +use crate::{ + common::AsyncInterval, + providers::{ + ip::IpProvider, CommonProviderState, Provider, RuntimeType, + }, }; #[derive(Deserialize, Debug)] @@ -60,7 +62,7 @@ impl WeatherProvider { } } - async fn run_interval(&self) -> anyhow::Result { + async fn run_interval(&self) -> anyhow::Result { let (latitude, longitude) = { match (self.config.latitude, self.config.longitude) { (Some(lat), Some(lon)) => (lat, lon), @@ -90,7 +92,7 @@ impl WeatherProvider { let current_weather = res.current_weather; let is_daytime = current_weather.is_day == 1; - Ok(ProviderOutput::Weather(WeatherOutput { + Ok(WeatherOutput { is_daytime, status: Self::get_weather_status( current_weather.weather_code, @@ -101,7 +103,7 @@ impl WeatherProvider { current_weather.temperature, ), wind_speed: current_weather.wind_speed, - })) + }) } fn celsius_to_fahrenheit(celsius_temp: f32) -> f32 { @@ -147,4 +149,19 @@ impl WeatherProvider { } } -impl_interval_provider!(WeatherProvider, true); +impl Provider for WeatherProvider { + fn runtime_type(&self) -> RuntimeType { + RuntimeType::Async + } + + async fn start_async(&mut self) { + let mut interval = AsyncInterval::new(self.config.refresh_interval); + + loop { + interval.tick().await; + + let output = self.run_interval().await; + self.common.emit_output(output); + } + } +}