Skip to content

Commit

Permalink
feat: remove use of impl_interval_provider; manually implement `Pro…
Browse files Browse the repository at this point in the history
…vider`
  • Loading branch information
lars-berger committed Nov 17, 2024
1 parent c6dbe06 commit 7c4046c
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 113 deletions.
10 changes: 2 additions & 8 deletions packages/desktop/src/providers/battery/battery_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use starship_battery::{

use crate::{
common::SyncInterval,
providers::{
CommonProviderState, Provider, ProviderOutput, RuntimeType,
},
providers::{CommonProviderState, Provider, RuntimeType},
};

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -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());
}
}
}
29 changes: 20 additions & 9 deletions packages/desktop/src/providers/cpu/cpu_provider.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};

use crate::{

providers::{CommonProviderState, ProviderOutput},
common::SyncInterval,
providers::{CommonProviderState, Provider, RuntimeType},
};

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -34,22 +34,33 @@ impl CpuProvider {
CpuProvider { config, common }
}



async fn run_interval(&self) -> anyhow::Result<ProviderOutput> {
let mut sysinfo = self.common.sysinfo.lock().await;
fn run_interval(&self) -> anyhow::Result<CpuOutput> {
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(),
physical_core_count: sysinfo
.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());
}
}
}
40 changes: 24 additions & 16 deletions packages/desktop/src/providers/disk/disk_provider.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -37,7 +33,7 @@ pub struct Disk {
pub struct DiskProvider {
config: DiskProviderConfig,
common: CommonProviderState,
disks: Arc<Mutex<Disks>>,
disks: Disks,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
Expand All @@ -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<DiskOutput> {
self.disks.refresh();


async fn run_interval(&self) -> anyhow::Result<ProviderOutput> {
let mut disks = self.disks.lock().await;
disks.refresh();

let disks = disks
let disks = self
.disks
.iter()
.map(|disk| -> anyhow::Result<Disk> {
let name = disk.name().to_string_lossy().to_string();
Expand All @@ -87,7 +81,7 @@ impl DiskProvider {
})
.collect::<anyhow::Result<Vec<Disk>>>()?;

Ok(ProviderOutput::Disk(DiskOutput { disks }))
Ok(DiskOutput { disks })
}

fn to_disk_size_measure(bytes: u64) -> anyhow::Result<DiskSizeMeasure> {
Expand All @@ -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);
}
}
}
29 changes: 21 additions & 8 deletions packages/desktop/src/providers/host/host_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -36,18 +36,31 @@ impl HostProvider {
HostProvider { config, common }
}



async fn run_interval(&self) -> anyhow::Result<ProviderOutput> {
Ok(ProviderOutput::Host(HostOutput {
fn run_interval(&mut self) -> anyhow::Result<HostOutput> {
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);
}
}
}
26 changes: 21 additions & 5 deletions packages/desktop/src/providers/ip/ip_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -39,9 +42,7 @@ impl IpProvider {
}
}



async fn run_interval(&self) -> anyhow::Result<IpOutput> {
async fn run_interval(&mut self) -> anyhow::Result<IpOutput> {
Self::query_ip(&self.http_client).await
}

Expand Down Expand Up @@ -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);
}
}
}
29 changes: 21 additions & 8 deletions packages/desktop/src/providers/keyboard/keyboard_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use windows::Win32::{
};

use crate::{

providers::{CommonProviderState, ProviderOutput},
common::SyncInterval,
providers::{CommonProviderState, Provider, RuntimeType},
};

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -39,9 +39,7 @@ impl KeyboardProvider {
KeyboardProvider { config, common }
}



async fn run_interval(&self) -> anyhow::Result<ProviderOutput> {
fn run_interval(&mut self) -> anyhow::Result<KeyboardOutput> {
let keyboard_layout = unsafe {
GetKeyboardLayout(GetWindowThreadProcessId(
GetForegroundWindow(),
Expand All @@ -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);
}
}
}
25 changes: 12 additions & 13 deletions packages/desktop/src/providers/komorebi/komorebi_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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::<KomorebiOutput>(Err(anyhow::anyhow!(
"Failed to read Komorebi stream."
)))
}
}
}

Expand Down Expand Up @@ -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::<KomorebiOutput>(Err(err));
}
}
}
9 changes: 4 additions & 5 deletions packages/desktop/src/providers/media/media_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GsmtcManager>, _| {
let session_changed_handler =
TypedEventHandler::new(move |_: &Option<GsmtcManager>, _| {
{
let mut current_session = current_session.lock().unwrap();
let mut event_tokens = event_tokens.lock().unwrap();
Expand Down Expand Up @@ -191,8 +191,7 @@ impl MediaProvider {
}

Ok(())
},
);
});

session_manager.CurrentSessionChanged(&session_changed_handler)?;

Expand Down Expand Up @@ -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::<MediaOutput>(Err(err));
}
}
}
Loading

0 comments on commit 7c4046c

Please sign in to comment.