Skip to content

Commit

Permalink
fix: signal_for must indicate when the action was not emitted
Browse files Browse the repository at this point in the history
Also refactor some code.
  • Loading branch information
srid committed Nov 7, 2023
1 parent c54d8bd commit be3e4a4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
23 changes: 9 additions & 14 deletions src/app/state/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(cx: Scope, sig: Signal<(usize, Action)>, f: F) -> Signal<usize>
/// 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<F>(cx: Scope, sig: Signal<(usize, Action)>, f: F) -> Signal<Option<usize>>
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
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/state/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<T> Datum<T> {
/// Refresh the datum [Signal] using the given function
///
/// If a previous refresh is still running, it will be cancelled.
pub async fn refresh_with<F>(signal: Signal<Self>, f: F)
pub async fn refresh_with<F>(signal: Signal<Datum<T>>, f: F)
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
Expand Down
19 changes: 10 additions & 9 deletions src/app/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<LocalStorage, _>(cx, "recent_flakes".to_string(), FlakeUrl::suggestions);
AppState {
Expand Down Expand Up @@ -94,21 +94,22 @@ 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<FlakeUrl>, Option<usize>)| 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
.map_err(|e| Into::<SystemError>::into(e.to_string()))
})
.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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit be3e4a4

Please sign in to comment.