Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify provider initialization and cleanup #84

Merged
merged 7 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/desktop/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::HashMap, env, sync::Arc};

use clap::Parser;
use providers::{
config::ProviderConfig, provider_manager::init_provider_manager,
};
use serde::Serialize;
use tauri::{
AppHandle, Manager, State, WebviewUrl, WebviewWindowBuilder, Window,
Expand All @@ -18,7 +21,7 @@ use tracing_subscriber::EnvFilter;
use crate::{
cli::{Cli, CliCommand},
monitors::get_monitors_str,
providers::{config::ProviderConfig, manager::ProviderManager},
providers::provider_manager::ProviderManager,
sys_tray::setup_sys_tray,
util::window_ext::WindowExt,
};
Expand Down Expand Up @@ -72,7 +75,7 @@ async fn listen_provider(
provider_manager: State<'_, ProviderManager>,
) -> anyhow::Result<(), String> {
provider_manager
.listen(config_hash, config, tracked_access)
.create(config_hash, config, tracked_access)
.await
.map_err(|err| err.to_string())
}
Expand All @@ -83,7 +86,7 @@ async fn unlisten_provider(
provider_manager: State<'_, ProviderManager>,
) -> anyhow::Result<(), String> {
provider_manager
.unlisten(config_hash)
.destroy(config_hash)
.await
.map_err(|err| err.to_string())
}
Expand Down Expand Up @@ -169,7 +172,7 @@ async fn main() {
// Add application icon to system tray.
setup_sys_tray(app)?;

providers::manager::init(app)?;
init_provider_manager(app);

let args_map = OpenWindowArgsMap(Default::default());
let args_map_ref = args_map.0.clone();
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/providers/battery/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::task::AbortHandle;

use super::{BatteryProviderConfig, BatteryVariables};
use crate::providers::{
interval_provider::IntervalProvider, variables::ProviderVariables,
provider::IntervalProvider, variables::ProviderVariables,
};

pub struct BatteryProvider {
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/src/providers/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;

#[cfg(all(windows, target_arch = "x86_64"))]
#[cfg(windows)]
use super::komorebi::KomorebiProviderConfig;
use super::{
battery::BatteryProviderConfig, cpu::CpuProviderConfig,
Expand All @@ -16,7 +16,7 @@ pub enum ProviderConfig {
Cpu(CpuProviderConfig),
Host(HostProviderConfig),
Ip(IpProviderConfig),
#[cfg(all(windows, target_arch = "x86_64"))]
#[cfg(windows)]
Komorebi(KomorebiProviderConfig),
Memory(MemoryProviderConfig),
Network(NetworkProviderConfig),
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/providers/cpu/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::{sync::Mutex, task::AbortHandle};

use super::{CpuProviderConfig, CpuVariables};
use crate::providers::{
interval_provider::IntervalProvider, variables::ProviderVariables,
provider::IntervalProvider, variables::ProviderVariables,
};

pub struct CpuProvider {
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/providers/host/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tokio::{sync::Mutex, task::AbortHandle};

use super::{HostProviderConfig, HostVariables};
use crate::providers::{
interval_provider::IntervalProvider, variables::ProviderVariables,
provider::IntervalProvider, variables::ProviderVariables,
};

pub struct HostProvider {
Expand Down
124 changes: 0 additions & 124 deletions packages/desktop/src/providers/interval_provider.rs

This file was deleted.

2 changes: 1 addition & 1 deletion packages/desktop/src/providers/ip/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::task::AbortHandle;

use super::{ipinfo_res::IpinfoRes, IpProviderConfig, IpVariables};
use crate::providers::{
interval_provider::IntervalProvider, variables::ProviderVariables,
provider::IntervalProvider, variables::ProviderVariables,
};

pub struct IpProvider {
Expand Down
16 changes: 9 additions & 7 deletions packages/desktop/src/providers/komorebi/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use super::{
};
use crate::providers::{
komorebi::KomorebiVariables,
manager::{ProviderOutput, VariablesResult},
provider::Provider,
provider_ref::{ProviderOutput, VariablesResult},
variables::ProviderVariables,
};

Expand Down Expand Up @@ -123,16 +123,18 @@ impl KomorebiProvider {

#[async_trait]
impl Provider for KomorebiProvider {
// State should always be up to date.
fn min_refresh_interval(&self) -> Duration {
Duration::MAX
fn min_refresh_interval(&self) -> Option<Duration> {
// State should always be up to date.
None
}

async fn on_start(
&mut self,
config_hash: String,
config_hash: &str,
emit_output_tx: Sender<ProviderOutput>,
) {
let config_hash = config_hash.to_string();

let task_handle = task::spawn(async move {
let socket = komorebi_client::subscribe(SOCKET_NAME).unwrap();
debug!("Connected to Komorebi socket.");
Expand Down Expand Up @@ -166,7 +168,7 @@ impl Provider for KomorebiProvider {
Err(error) => {
_ = emit_output_tx
.send(ProviderOutput {
config_hash: config_hash.clone(),
config_hash: config_hash.to_string(),
variables: VariablesResult::Error(error.to_string()),
})
.await;
Expand All @@ -181,7 +183,7 @@ impl Provider for KomorebiProvider {

async fn on_refresh(
&mut self,
_config_hash: String,
_config_hash: &str,
_emit_output_tx: Sender<ProviderOutput>,
) {
// No-op.
Expand Down
Loading