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
93 lines
2.8 KiB
Rust
93 lines
2.8 KiB
Rust
#![allow(non_snake_case)]
|
|
use dioxus::prelude::dioxus_core::NoOpMutations;
|
|
use dioxus::prelude::*;
|
|
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
|
|
|
|
// Regression test for https://github.com/DioxusLabs/dioxus/issues/3421
|
|
#[tokio::test]
|
|
async fn test_for_memory_leaks() {
|
|
fn app() -> Element {
|
|
let mut count = use_signal(|| 0);
|
|
|
|
use_hook(|| {
|
|
spawn(async move {
|
|
loop {
|
|
tokio::time::sleep(std::time::Duration::from_nanos(1)).await;
|
|
let val = *count.peek_unchecked();
|
|
if val == 70 {
|
|
count.set(0);
|
|
} else {
|
|
count.set(val + 1);
|
|
}
|
|
}
|
|
})
|
|
});
|
|
|
|
rsx! {
|
|
for el in 0..*count.read() {
|
|
div {
|
|
key: "{el}",
|
|
div {
|
|
onclick: move |_| { println!("click"); },
|
|
}
|
|
AcceptsEventHandlerAndReadOnlySignal {
|
|
event_handler: move |_| { println!("click"); },
|
|
signal: el,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Event handlers and ReadOnlySignals have extra logic on component boundaries that has caused memory leaks
|
|
// in the past
|
|
#[component]
|
|
fn AcceptsEventHandlerAndReadOnlySignal(
|
|
event_handler: EventHandler<MouseEvent>,
|
|
signal: ReadSignal<i32>,
|
|
) -> Element {
|
|
rsx! {
|
|
div {
|
|
onclick: event_handler,
|
|
"{signal}"
|
|
}
|
|
}
|
|
}
|
|
|
|
// create the vdom, the real_dom, and the binding layer between them
|
|
let mut vdom = VirtualDom::new(app);
|
|
|
|
vdom.rebuild(&mut NoOpMutations);
|
|
|
|
let pid = sysinfo::get_current_pid().expect("failed to get PID");
|
|
|
|
let refresh =
|
|
RefreshKind::nothing().with_processes(ProcessRefreshKind::nothing().with_memory());
|
|
let mut system = System::new_with_specifics(refresh);
|
|
|
|
let mut get_memory_usage = || {
|
|
system.refresh_specifics(refresh);
|
|
let this_process = system.process(pid).expect("failed to get process");
|
|
this_process.memory()
|
|
};
|
|
|
|
let initial_memory_usage = get_memory_usage();
|
|
|
|
// we need to run the vdom in a async runtime
|
|
for i in 0..=10000 {
|
|
// wait for the vdom to update
|
|
vdom.wait_for_work().await;
|
|
|
|
// get the mutations from the vdom
|
|
vdom.render_immediate(&mut NoOpMutations);
|
|
|
|
if i % 1000 == 0 {
|
|
let new_memory_usage = get_memory_usage();
|
|
println!("iteration: {} memory usage: {}", i, new_memory_usage);
|
|
|
|
// Memory usage might increase as arenas fill up, but it shouldn't double from the initial render
|
|
assert!(new_memory_usage < initial_memory_usage * 2);
|
|
}
|
|
}
|
|
}
|