2022-02-11 02:00:15 +00:00
|
|
|
#![allow(non_snake_case)]
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
use std::rc::Rc;
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-03-03 03:48:22 +00:00
|
|
|
use dioxus::prelude::*;
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
/// This test checks that we should release all memory used by the virtualdom when it exits.
|
|
|
|
///
|
|
|
|
/// When miri runs, it'll let us know if we leaked or aliased.
|
2021-12-19 04:03:59 +00:00
|
|
|
#[test]
|
|
|
|
fn test_memory_leak() {
|
2024-01-11 16:48:04 +00:00
|
|
|
fn app() -> Element {
|
2024-01-11 01:21:15 +00:00
|
|
|
let val = generation();
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2024-01-11 21:18:11 +00:00
|
|
|
spawn(async {});
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
if val == 2 || val == 4 {
|
2024-01-11 01:21:15 +00:00
|
|
|
return render!(());
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 21:18:11 +00:00
|
|
|
let mut name = once(|| String::from("numbers: "));
|
2022-11-29 21:31:04 +00:00
|
|
|
|
|
|
|
name.push_str("123 ");
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2024-01-11 01:21:15 +00:00
|
|
|
render!(
|
2021-12-19 04:03:59 +00:00
|
|
|
div { "Hello, world!" }
|
2022-09-13 03:01:03 +00:00
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
2024-01-11 21:18:11 +00:00
|
|
|
BorrowedChild { name: name.clone() }
|
|
|
|
BorrowedChild { name: name.clone() }
|
|
|
|
BorrowedChild { name: name.clone() }
|
|
|
|
BorrowedChild { name: name.clone() }
|
|
|
|
BorrowedChild { name: name.clone() }
|
2024-01-11 01:21:15 +00:00
|
|
|
)
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 17:11:44 +00:00
|
|
|
#[derive(Props, Clone, PartialEq)]
|
|
|
|
struct BorrowedProps {
|
|
|
|
name: String,
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 17:11:44 +00:00
|
|
|
fn BorrowedChild(cx: BorrowedProps) -> Element {
|
2024-01-11 01:21:15 +00:00
|
|
|
render! {
|
2022-11-29 21:31:04 +00:00
|
|
|
div {
|
2024-01-11 17:11:44 +00:00
|
|
|
"goodbye {cx.name}"
|
2022-11-29 21:31:04 +00:00
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
}
|
2024-01-11 01:21:15 +00:00
|
|
|
}
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 17:11:44 +00:00
|
|
|
fn Child() -> Element {
|
2024-01-11 01:21:15 +00:00
|
|
|
render!( div { "goodbye world" } )
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 07:06:04 +00:00
|
|
|
let mut dom = VirtualDom::new(app);
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2024-01-11 18:40:36 +00:00
|
|
|
_ = dom.rebuild(&mut dioxus_core::NoOpMutations);
|
2022-11-29 21:31:04 +00:00
|
|
|
|
|
|
|
for _ in 0..5 {
|
2023-08-25 13:31:23 +00:00
|
|
|
dom.mark_dirty(ScopeId::ROOT);
|
2024-01-11 01:21:15 +00:00
|
|
|
_ = dom.render_immediate_to_vec();
|
2022-11-29 21:31:04 +00:00
|
|
|
}
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn memo_works_properly() {
|
2024-01-11 16:48:04 +00:00
|
|
|
fn app() -> Element {
|
2024-01-11 01:21:15 +00:00
|
|
|
let val = generation();
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
if val == 2 || val == 4 {
|
2024-01-11 01:21:15 +00:00
|
|
|
return render!(());
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 21:18:11 +00:00
|
|
|
let name = once(|| String::from("asd"));
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2024-01-11 01:21:15 +00:00
|
|
|
render!(
|
2022-01-03 06:12:39 +00:00
|
|
|
div { "Hello, world! {name}" }
|
2022-09-13 03:01:03 +00:00
|
|
|
Child { na: "asdfg".to_string() }
|
2024-01-11 01:21:15 +00:00
|
|
|
)
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 18:40:36 +00:00
|
|
|
#[derive(PartialEq, Clone, Props)]
|
2021-12-19 04:03:59 +00:00
|
|
|
struct ChildProps {
|
|
|
|
na: String,
|
|
|
|
}
|
|
|
|
|
2024-01-11 21:18:11 +00:00
|
|
|
fn Child(cx: ChildProps) -> Element {
|
2024-01-11 01:21:15 +00:00
|
|
|
render!( div { "goodbye world" } )
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
let mut dom = VirtualDom::new(app);
|
2021-12-21 03:33:13 +00:00
|
|
|
|
2024-01-11 18:40:36 +00:00
|
|
|
_ = dom.rebuild(&mut dioxus_core::NoOpMutations);
|
2022-12-05 22:16:54 +00:00
|
|
|
// todo!()
|
2023-08-25 13:31:23 +00:00
|
|
|
// dom.hard_diff(ScopeId::ROOT);
|
|
|
|
// dom.hard_diff(ScopeId::ROOT);
|
|
|
|
// dom.hard_diff(ScopeId::ROOT);
|
|
|
|
// dom.hard_diff(ScopeId::ROOT);
|
|
|
|
// dom.hard_diff(ScopeId::ROOT);
|
|
|
|
// dom.hard_diff(ScopeId::ROOT);
|
|
|
|
// dom.hard_diff(ScopeId::ROOT);
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn free_works_on_root_hooks() {
|
|
|
|
/*
|
2022-11-29 21:31:04 +00:00
|
|
|
On Drop, scopearena drops all the hook contents. and props
|
2021-12-21 03:33:13 +00:00
|
|
|
*/
|
2022-11-29 21:31:04 +00:00
|
|
|
#[derive(PartialEq, Clone, Props)]
|
|
|
|
struct AppProps {
|
|
|
|
inner: Rc<String>,
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 21:18:11 +00:00
|
|
|
fn app(cx: AppProps) -> Element {
|
|
|
|
let name: AppProps = once(|| cx.clone());
|
2022-11-29 21:31:04 +00:00
|
|
|
render!(child_component { inner: name.inner.clone() })
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 18:40:36 +00:00
|
|
|
fn child_component(props: AppProps) -> Element {
|
|
|
|
render!( div { "{props.inner}" } )
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
let ptr = Rc::new("asdasd".to_string());
|
|
|
|
let mut dom = VirtualDom::new_with_props(app, AppProps { inner: ptr.clone() });
|
2024-01-11 18:40:36 +00:00
|
|
|
let _ = dom.rebuild(&mut dioxus_core::NoOpMutations);
|
2021-12-21 03:33:13 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
// ptr gets cloned into props and then into the hook
|
|
|
|
assert_eq!(Rc::strong_count(&ptr), 4);
|
2021-12-21 03:33:13 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
drop(dom);
|
2021-12-21 03:33:13 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
assert_eq!(Rc::strong_count(&ptr), 1);
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 19:34:44 +00:00
|
|
|
#[test]
|
|
|
|
fn supports_async() {
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::time::sleep;
|
|
|
|
|
2024-01-11 16:48:04 +00:00
|
|
|
fn app() -> Element {
|
2024-01-11 17:11:44 +00:00
|
|
|
let colors = use_signal(|| vec!["green", "blue", "red"]);
|
|
|
|
let padding = use_signal(|| 10);
|
2022-12-30 19:34:44 +00:00
|
|
|
|
2024-01-11 21:18:11 +00:00
|
|
|
once(|| {
|
|
|
|
spawn(async move {
|
|
|
|
sleep(Duration::from_millis(1000)).await;
|
|
|
|
colors.with_mut(|colors| colors.reverse());
|
|
|
|
})
|
2022-12-30 19:34:44 +00:00
|
|
|
});
|
|
|
|
|
2024-01-11 21:18:11 +00:00
|
|
|
once(|| {
|
|
|
|
spawn(async move {
|
|
|
|
sleep(Duration::from_millis(10)).await;
|
|
|
|
padding.with_mut(|padding| {
|
|
|
|
if *padding < 65 {
|
|
|
|
*padding += 1;
|
|
|
|
} else {
|
|
|
|
*padding = 5;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
2022-12-30 19:34:44 +00:00
|
|
|
});
|
|
|
|
|
2024-01-11 18:40:36 +00:00
|
|
|
let colors = colors();
|
2022-12-30 19:34:44 +00:00
|
|
|
let big = colors[0];
|
|
|
|
let mid = colors[1];
|
|
|
|
let small = colors[2];
|
|
|
|
|
2024-01-11 01:21:15 +00:00
|
|
|
render! {
|
|
|
|
div { background: "{big}", height: "stretch", width: "stretch", padding: "50",
|
|
|
|
label { "hello" }
|
|
|
|
div { background: "{mid}", height: "auto", width: "stretch", padding: "{padding}",
|
|
|
|
label { "World" }
|
|
|
|
div { background: "{small}", height: "auto", width: "stretch", padding: "20", label { "ddddddd" } }
|
2022-12-30 19:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-11 01:21:15 +00:00
|
|
|
}
|
2022-12-30 19:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let rt = tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_time()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
rt.block_on(async {
|
|
|
|
let mut dom = VirtualDom::new(app);
|
2024-01-11 18:40:36 +00:00
|
|
|
let _ = dom.rebuild(&mut dioxus_core::NoOpMutations);
|
2022-12-30 19:34:44 +00:00
|
|
|
|
2023-01-12 00:13:41 +00:00
|
|
|
for _ in 0..10 {
|
|
|
|
dom.wait_for_work().await;
|
2024-01-11 01:21:15 +00:00
|
|
|
let _edits = dom.render_immediate_to_vec();
|
2022-12-30 19:34:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|