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() {
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-11-29 21:31:04 +00:00
|
|
|
let val = cx.generation();
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-12-14 00:04:41 +00:00
|
|
|
cx.spawn(async {});
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
if val == 2 || val == 4 {
|
2022-11-27 07:06:04 +00:00
|
|
|
return cx.render(rsx!(()));
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
let name = cx.use_hook(|| String::from("numbers: "));
|
|
|
|
|
|
|
|
name.push_str("123 ");
|
2021-12-19 04:03:59 +00:00
|
|
|
|
|
|
|
cx.render(rsx!(
|
|
|
|
div { "Hello, world!" }
|
2022-09-13 03:01:03 +00:00
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
Child {}
|
2022-11-29 21:31:04 +00:00
|
|
|
BorrowedChild { name: name }
|
|
|
|
BorrowedChild { name: name }
|
|
|
|
BorrowedChild { name: name }
|
|
|
|
BorrowedChild { name: name }
|
|
|
|
BorrowedChild { name: name }
|
2021-12-19 04:03:59 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
struct BorrowedProps<'a> {
|
2022-11-29 21:31:04 +00:00
|
|
|
name: &'a str,
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 03:01:03 +00:00
|
|
|
fn BorrowedChild<'a>(cx: Scope<'a, BorrowedProps<'a>>) -> Element {
|
2022-11-29 21:31:04 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
"goodbye {cx.props.name}"
|
|
|
|
Child {}
|
|
|
|
Child {}
|
|
|
|
}
|
2021-12-19 04:03:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-13 03:01:03 +00:00
|
|
|
fn Child(cx: Scope) -> Element {
|
2022-09-25 08:05:16 +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
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
_ = dom.rebuild();
|
|
|
|
|
|
|
|
for _ in 0..5 {
|
2023-08-25 13:31:23 +00:00
|
|
|
dom.mark_dirty(ScopeId::ROOT);
|
2022-11-29 21:31:04 +00:00
|
|
|
_ = dom.render_immediate();
|
|
|
|
}
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn memo_works_properly() {
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-11-29 21:31:04 +00:00
|
|
|
let val = cx.generation();
|
2021-12-19 04:03:59 +00:00
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
if val == 2 || val == 4 {
|
|
|
|
return cx.render(rsx!(()));
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 19:50:56 +00:00
|
|
|
let name = cx.use_hook(|| String::from("asd"));
|
2021-12-19 04:03:59 +00:00
|
|
|
|
|
|
|
cx.render(rsx!(
|
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() }
|
2021-12-19 04:03:59 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Props)]
|
|
|
|
struct ChildProps {
|
|
|
|
na: String,
|
|
|
|
}
|
|
|
|
|
2022-09-13 03:01:03 +00:00
|
|
|
fn Child(cx: Scope<ChildProps>) -> Element {
|
2022-09-25 08:05:16 +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
|
|
|
|
2022-12-05 21:03:52 +00:00
|
|
|
_ = dom.rebuild();
|
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
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
fn app(cx: Scope<AppProps>) -> Element {
|
|
|
|
let name: &AppProps = cx.use_hook(|| cx.props.clone());
|
|
|
|
render!(child_component { inner: name.inner.clone() })
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:31:04 +00:00
|
|
|
fn child_component(cx: Scope<AppProps>) -> Element {
|
|
|
|
render!(div { "{cx.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() });
|
2021-12-21 03:33:13 +00:00
|
|
|
let _ = dom.rebuild();
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
2023-01-12 00:13:41 +00:00
|
|
|
let colors = use_state(cx, || vec!["green", "blue", "red"]);
|
|
|
|
let padding = use_state(cx, || 10);
|
2022-12-30 19:34:44 +00:00
|
|
|
|
2023-01-12 00:13:41 +00:00
|
|
|
use_effect(cx, colors, |colors| async move {
|
2022-12-30 19:34:44 +00:00
|
|
|
sleep(Duration::from_millis(1000)).await;
|
|
|
|
colors.with_mut(|colors| colors.reverse());
|
|
|
|
});
|
|
|
|
|
2023-01-12 00:13:41 +00:00
|
|
|
use_effect(cx, padding, |padding| async move {
|
2022-12-30 19:34:44 +00:00
|
|
|
sleep(Duration::from_millis(10)).await;
|
|
|
|
padding.with_mut(|padding| {
|
|
|
|
if *padding < 65 {
|
|
|
|
*padding += 1;
|
|
|
|
} else {
|
|
|
|
*padding = 5;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let big = colors[0];
|
|
|
|
let mid = colors[1];
|
|
|
|
let small = colors[2];
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let rt = tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_time()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
rt.block_on(async {
|
|
|
|
let mut dom = VirtualDom::new(app);
|
|
|
|
let _ = dom.rebuild();
|
|
|
|
|
2023-01-12 00:13:41 +00:00
|
|
|
for _ in 0..10 {
|
|
|
|
dom.wait_for_work().await;
|
|
|
|
let _edits = dom.render_immediate();
|
2022-12-30 19:34:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|