Skip to content

Commit

Permalink
refactor: simplify provider initialization and cleanup (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Aug 17, 2024
1 parent 480613f commit 8e585fa
Show file tree
Hide file tree
Showing 17 changed files with 480 additions and 494 deletions.
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

2 comments on commit 8e585fa

@CtByte
Copy link
Contributor

@CtByte CtByte commented on 8e585fa Aug 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lars-berger I tried to run this commit locally ( pnpm i; pnpm dev ) and ended up getting this error:

packages/desktop dev: 2024-08-19T17:18:59.758948Z  WARN zebar::providers::provider_manager: Error refreshing provider: channel closed
packages/desktop dev: 2024-08-19T17:20:08.989281Z  WARN tao::platform_impl::platform::event_loop::runner: NewEvents emitted without explicit RedrawEventsCleared
packages/desktop dev: 2024-08-19T17:20:08.992675Z  WARN tao::platform_impl::platform::event_loop::runner: RedrawEventsCleared emitted without explicit MainEventsCleared
packages/desktop dev: 2024-08-19T17:20:09.782315Z  WARN zebar::providers::provider_manager: Error refreshing provider: channel closed
packages/desktop dev: 2024-08-19T17:20:09.782521Z  WARN zebar::providers::provider_manager: Error refreshing provider: channel closed
packages/desktop dev: 2024-08-19T17:20:09.782760Z  WARN zebar::providers::provider_manager: Error refreshing provider: channel closed
packages/desktop dev: 2024-08-19T17:20:09.783535Z  WARN zebar::providers::provider_manager: Error refreshing provider: channel closed
packages/desktop dev: 2024-08-19T17:20:09.783821Z  WARN zebar::providers::provider_manager: Error refreshing provider: channel closed

Are there any breaking changes regarding the config file, since none of the providers work. I might be missing something of course.

Edit: Just saw that I am not the only one #85 (comment)

@lars-berger
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the logs on this. There's a fix in main for this now 👍 @CtByte

Please sign in to comment.