From be3e4a43ca92996c0958609bc4a3c93ede777e7d Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Tue, 7 Nov 2023 18:06:01 -0500 Subject: [PATCH] fix: signal_for must indicate when the action was not emitted Also refactor some code. --- src/app/state/action.rs | 23 +++++++++-------------- src/app/state/datum.rs | 2 +- src/app/state/mod.rs | 19 ++++++++++--------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/app/state/action.rs b/src/app/state/action.rs index 8595b9dd..17906d9f 100644 --- a/src/app/state/action.rs +++ b/src/app/state/action.rs @@ -15,23 +15,18 @@ pub enum Action { impl Action { /// Return a [Signal] containing only the given [Action] /// - /// The signal value will be the [Action]'s index in the original signal. - pub fn signal_for(cx: Scope, sig: Signal<(usize, Action)>, f: F) -> Signal + /// The signal value will be the [Action]'s index in the original signal, which index is [None] if the action was never emitted.. + pub fn signal_for(cx: Scope, sig: Signal<(usize, Action)>, f: F) -> Signal> where F: Fn(Action) -> bool + 'static, { - signal_filter_map( - cx, - sig, - 0, - move |(idx, action)| { - if f(action) { - Some(idx) - } else { - None - } - }, - ) + signal_filter_map(cx, sig, None, move |(idx, action)| { + if f(action) { + Some(Some(idx)) + } else { + None + } + }) } } diff --git a/src/app/state/datum.rs b/src/app/state/datum.rs index 7f130a09..7fb89b3f 100644 --- a/src/app/state/datum.rs +++ b/src/app/state/datum.rs @@ -36,7 +36,7 @@ impl Datum { /// Refresh the datum [Signal] using the given function /// /// If a previous refresh is still running, it will be cancelled. - pub async fn refresh_with(signal: Signal, f: F) + pub async fn refresh_with(signal: Signal>, f: F) where F: Future + Send + 'static, T: Send + 'static, diff --git a/src/app/state/mod.rs b/src/app/state/mod.rs index 366b6c42..c2434acb 100644 --- a/src/app/state/mod.rs +++ b/src/app/state/mod.rs @@ -39,7 +39,7 @@ pub struct AppState { impl AppState { fn new(cx: Scope) -> Self { - tracing::debug!("🔨 Creating AppState default value"); + tracing::debug!("🔨 Creating new AppState"); let recent_flakes = new_storage::(cx, "recent_flakes".to_string(), FlakeUrl::suggestions); AppState { @@ -94,13 +94,9 @@ impl AppState { // Build `state.flake` signal when `state.flake_url` changes or the // RefreshFlake action is triggered { - let flake_url = self.flake_url.read().clone(); - let refresh_action = - Action::signal_for(cx, self.action, |act| act == Action::RefreshFlake); - let idx = *refresh_action.read(); - use_future(cx, (&flake_url, &idx), |(flake_url, idx)| async move { + let refresh_flake = |(flake_url, idx): (Option, Option)| async move { if let Some(flake_url) = flake_url { - tracing::info!("Updating flake [{}] {} ...", flake_url, idx); + tracing::info!("Updating flake [{}] {:?} ...", flake_url, idx); Datum::refresh_with(self.flake, async move { Flake::from_nix(&nix_rs::command::NixCmd::default(), flake_url.clone()) .await @@ -108,7 +104,12 @@ impl AppState { }) .await } - }); + }; + let flake_url = self.flake_url.read().clone(); + let refresh_action = + Action::signal_for(cx, self.action, |act| act == Action::RefreshFlake); + let idx = *refresh_action.read(); + use_future(cx, (&flake_url, &idx), refresh_flake); } // Update recent_flakes @@ -147,7 +148,7 @@ impl AppState { Action::signal_for(cx, self.action, |act| act == Action::GetNixInfo); let idx = *get_nix_info_action.read(); use_future(cx, (&idx,), |(idx,)| async move { - tracing::info!("Updating nix info [{}] ...", idx); + tracing::info!("Updating nix info [{:?}] ...", idx); Datum::refresh_with(self.nix_info, async { NixInfo::from_nix(&nix_rs::command::NixCmd::default()) .await