From 08ba7cffade84b86d9aae89405dc02246775c0b1 Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Sun, 17 Nov 2024 02:49:56 +0800 Subject: [PATCH] refactor: return task handle from `create_instance` --- .../providers/komorebi/komorebi_provider.rs | 2 +- .../desktop/src/providers/provider_manager.rs | 33 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/desktop/src/providers/komorebi/komorebi_provider.rs b/packages/desktop/src/providers/komorebi/komorebi_provider.rs index 34893ed9..048cf23f 100644 --- a/packages/desktop/src/providers/komorebi/komorebi_provider.rs +++ b/packages/desktop/src/providers/komorebi/komorebi_provider.rs @@ -83,7 +83,7 @@ impl KomorebiProvider { notification.state, ))) .into(), - ) + ); } } Err(_) => emit_result_tx.send( diff --git a/packages/desktop/src/providers/provider_manager.rs b/packages/desktop/src/providers/provider_manager.rs index 5fc697ba..393873f0 100644 --- a/packages/desktop/src/providers/provider_manager.rs +++ b/packages/desktop/src/providers/provider_manager.rs @@ -124,20 +124,8 @@ impl ProviderManager { sysinfo: self.sysinfo.clone(), }; - let mut provider = self.create_instance(config, common)?; - - // Spawn the provider's task based on its runtime type. - let config_hash_clone = config_hash.clone(); - let task_handle = match provider.runtime_type() { - RuntimeType::Async => task::spawn(async move { - provider.start_async().await; - info!("Provider stopped: {}", config_hash_clone); - }), - RuntimeType::Sync => task::spawn_blocking(move || { - provider.start_sync(); - info!("Provider stopped: {}", config_hash_clone); - }), - }; + let task_handle = + self.create_instance(config, config_hash.clone(), common)?; let provider_ref = ProviderRef { stop_tx, @@ -155,8 +143,9 @@ impl ProviderManager { fn create_instance( &self, config: ProviderConfig, + config_hash: String, common: CommonProviderState, - ) -> anyhow::Result> { + ) -> anyhow::Result> { let provider: Box = match config { ProviderConfig::Battery(config) => { Box::new(BatteryProvider::new(config, common)) @@ -198,7 +187,19 @@ impl ProviderManager { _ => bail!("Provider not supported on this operating system."), }; - Ok(provider) + // Spawn the provider's task based on its runtime type. + let task_handle = match provider.runtime_type() { + RuntimeType::Async => task::spawn(async move { + provider.start_async().await; + info!("Provider stopped: {}", config_hash); + }), + RuntimeType::Sync => task::spawn_blocking(move || { + provider.start_sync(); + info!("Provider stopped: {}", config_hash); + }), + }; + + Ok(task_handle) } /// Sends a function call through a channel to be executed by the