dioxus
dioxus copied to clipboard
Ability to derive a `Signal` while dropping some values
Specific Demand
Analogous to map (#1537), I'd like a way to produce a new signal that contains only a subset of values set in the original signal, potentially mapped over to a new type, U. This analogous to Rust's filter_map semantics
cf. discussion at https://github.com/DioxusLabs/dioxus/pull/1537#issuecomment-1769205030
Implement Suggestion
fn signal_filter_map<T, U, F>(cx: Scope, sig: Signal<T>, initial: U, f: F) -> Signal<U>
where
F: Fn(T) -> Option<U> + 'static,
T: Copy,
{
let res: Signal<U> = use_signal(cx, || initial);
dioxus_signals::use_effect(cx, move || {
let value = *sig.read();
if let Some(value) = f(value) {
res.set(value);
}
});
res
}