This PR contains just the breaking changes required for the upcoming store implementation. It adds two new primitives: Mutable mapped signals (without a different subscriber list). This lets you scope a writable value and pass it to a child component. It requires two closures to map the immutable and mutable references to the inner signal. With stores, these closures will be derived Boxed readable and writable values (ReadSignal, and WriteSignal). Any readable or writable types can be converted to a read signal or write signal with the boxed method or built in types are automatically converted into the boxed variants when passing into props * map signals mutably * boxed signals * remove mutable borrow type from boxed writable * improve map mut example * restore boxed_mut helper * make boxed readable/writable types copy * Move away from the ReadOnlySignal alias in examples * automatically convert Write * Fix some tests * ReadSignal and WriteSignal * move read only signal alias * make maps copy * store prototype * implement derive macro * Handle generics in the derive macro * Make Selector boxable * derive boxed conversion * Get rid of store type * Remove hardcoded type from derive macro * selector -> store * Refactor store implementation * more vec methods * todo mvc store example * Implement Storable for HashMap * fix write * restore sorting logic in todomvc * derive readable and writable * improve todomvc store example * Result store * Option store * collect dead subscribers * fix clippy and formatting * recognize some common foreign types automatically * fix foreign stores in props * slice and array selectors * clean up store example * remove stores * writable is child owned * fix typo * fix clippy * fix doc tests
66 lines
1.7 KiB
Rust
66 lines
1.7 KiB
Rust
//! Backgrounded futures example
|
|
//!
|
|
//! This showcases how use_future, use_memo, and use_effect will stop running if the component returns early.
|
|
//! Generally you should avoid using early returns around hooks since most hooks are not properly designed to
|
|
//! handle early returns. However, use_future *does* pause the future when the component returns early, and so
|
|
//! hooks that build on top of it like use_memo and use_effect will also pause.
|
|
//!
|
|
//! This example is more of a demonstration of the behavior than a practical use case, but it's still interesting to see.
|
|
|
|
use async_std::task::sleep;
|
|
use dioxus::prelude::*;
|
|
|
|
fn main() {
|
|
dioxus::launch(app);
|
|
}
|
|
|
|
fn app() -> Element {
|
|
let mut show_child = use_signal(|| true);
|
|
let mut count = use_signal(|| 0);
|
|
|
|
let child = use_memo(move || {
|
|
rsx! {
|
|
Child { count }
|
|
}
|
|
});
|
|
|
|
rsx! {
|
|
// Some toggle/controls to show the child or increment the count
|
|
button { onclick: move |_| show_child.toggle(), "Toggle child" }
|
|
button { onclick: move |_| count += 1, "Increment count" }
|
|
|
|
if show_child() {
|
|
{child()}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[component]
|
|
fn Child(count: WriteSignal<i32>) -> Element {
|
|
let mut early_return = use_signal(|| false);
|
|
|
|
let early = rsx! {
|
|
button { onclick: move |_| early_return.toggle(), "Toggle {early_return} early return" }
|
|
};
|
|
|
|
if early_return() {
|
|
return early;
|
|
}
|
|
|
|
use_future(move || async move {
|
|
loop {
|
|
sleep(std::time::Duration::from_millis(100)).await;
|
|
println!("Child")
|
|
}
|
|
});
|
|
|
|
use_effect(move || println!("Child count: {}", count()));
|
|
|
|
rsx! {
|
|
div {
|
|
"Child component"
|
|
{early}
|
|
}
|
|
}
|
|
}
|