Skip to content

Commit

Permalink
Preserve reduce_mut return value (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
intendednull authored Dec 10, 2023
1 parent b57cf7a commit b1028d1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions crates/yewdux/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,18 @@ impl<S: Store> Dispatch<S> {
/// dispatch.reduce_mut(|state| state.count += 1);
/// # }
/// ```
pub fn reduce_mut<F, R>(&self, f: F)
pub fn reduce_mut<F, R>(&self, f: F) -> R
where
S: Clone,
F: FnOnce(&mut S) -> R,
{
let mut result = None;

reduce_mut(|x| {
f(x);
result = Some(f(x));
});

result.expect("result not initialized")
}

/// Mutate state with given function, in an async context.
Expand Down
2 changes: 1 addition & 1 deletion examples/history/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn Controls() -> Html {
.enumerate()
.map(|(i, x)| {
let matches = i == state.index();
let match_text = matches.then_some("<<<");
let match_text = matches.then_some("<<<").unwrap_or("");
let text = format!("{x:?}");

let onclick = dispatch.apply_callback(move |_| HistoryMessage::JumpTo(i));
Expand Down

0 comments on commit b1028d1

Please sign in to comment.