Skip to content

Commit

Permalink
fix: prevent running on_start repeatedly on provider refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Feb 10, 2024
1 parent 4a3ad9e commit bf35254
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/desktop/src/providers/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,29 @@ pub trait Provider {
mut refresh_rx: Receiver<()>,
mut stop_rx: Receiver<()>,
) {
let mut has_started = false;

// Loop to avoid exiting the select on refresh.
loop {
let config_hash = config_hash.clone();
let emit_output_tx = emit_output_tx.clone();

tokio::select! {
_ = self.on_start(config_hash.clone(), emit_output_tx.clone()) => break,
// Default match arm which handles initialization of the provider.
// This has a precondition to avoid running again on refresh.
_ = {
info!("Starting provider: {}", config_hash);
has_started = true;
self.on_start(config_hash.clone(), emit_output_tx.clone())
}, if !has_started => break,

// On refresh, re-emit provider variables and continue looping.
Some(_) = refresh_rx.recv() => {
info!("Refreshing provider: {}", config_hash);
_ = self.on_refresh(config_hash, emit_output_tx).await;
},

// On stop, perform any necessary clean up and exit the loop.
Some(_) = stop_rx.recv() => {
info!("Stopping provider: {}", config_hash);
_ = self.on_stop().await;
Expand Down

0 comments on commit bf35254

Please sign in to comment.