wip: adjust memoization

This commit is contained in:
Jonathan Kelley committed 2021-12-20 22:33:13 -05:00
1 parent 934de21dd6
commit e2e4d431e1
15 files changed
+932 -703

No files matched your search

+1 -1
View File
@@ -15,7 +15,7 @@ mod test_logging;
fn new_dom() -> VirtualDom {
const IS_LOGGING_ENABLED: bool = false;
test_logging::set_up_logging(IS_LOGGING_ENABLED);
VirtualDom::new(|cx| todo!())
VirtualDom::new(|cx| rsx!(cx, "hi"))
}
use DomEdit::*;
+2 -2
View File
@@ -55,13 +55,13 @@ fn test_early_abort() {
]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[CreatePlaceholder { root: 3 }, ReplaceWith { root: 1, m: 1 },],
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[
+13 -13
View File
@@ -46,13 +46,13 @@ fn manual_diffing() {
#[test]
fn events_generate() {
static App: Component<()> = |cx| {
let mut count = use_state(&cx, || 0);
let count = cx.use_hook(|_| 0, |f| f);
let inner = match *count {
0 => {
rsx! {
div {
onclick: move |_| count += 1,
onclick: move |_| *count += 1,
div {
"nested"
}
@@ -105,8 +105,8 @@ fn events_generate() {
#[test]
fn components_generate() {
static App: Component<()> = |cx| {
let mut render_phase = use_state(&cx, || 0);
render_phase += 1;
let render_phase = cx.use_hook(|_| 0, |f| f);
*render_phase += 1;
cx.render(match *render_phase {
0 => rsx!("Text0"),
@@ -140,7 +140,7 @@ fn components_generate() {
]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[
@@ -152,7 +152,7 @@ fn components_generate() {
]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[
@@ -164,7 +164,7 @@ fn components_generate() {
]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[
@@ -173,13 +173,13 @@ fn components_generate() {
]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[CreatePlaceholder { root: 5 }, ReplaceWith { root: 4, m: 1 },]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[
@@ -191,7 +191,7 @@ fn components_generate() {
]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[
@@ -207,7 +207,7 @@ fn components_generate() {
]
);
let edits = dom.hard_diff(ScopeId(0)).unwrap();
let edits = dom.hard_diff(ScopeId(0));
assert_eq!(
edits.edits,
[
@@ -222,8 +222,8 @@ fn components_generate() {
fn component_swap() {
// simple_logger::init();
static App: Component<()> = |cx| {
let mut render_phase = use_state(&cx, || 0);
render_phase += 1;
let render_phase = cx.use_hook(|_| 0, |f| f);
*render_phase += 1;
cx.render(match *render_phase {
0 => rsx!(
+173 -11
View File
@@ -9,7 +9,7 @@ Specifically:
- [ ] Async isn't busted
*/
use dioxus::{prelude::*, DomEdit, ScopeId};
use dioxus::{prelude::*, DomEdit, SchedulerMsg, ScopeId};
use dioxus_core as dioxus;
use dioxus_core_macro::*;
use dioxus_html as dioxus_elements;
@@ -55,12 +55,6 @@ fn test_memory_leak() {
))
}
fn child(cx: Scope<()>) -> Element {
rsx!(cx, div {
"goodbye world"
})
}
#[derive(Props)]
struct BorrowedProps<'a> {
na: &'a str,
@@ -74,6 +68,10 @@ fn test_memory_leak() {
})
}
fn child(cx: Scope<()>) -> Element {
rsx!(cx, div { "goodbye world" })
}
let mut dom = new_dom(app, ());
dom.rebuild();
@@ -100,7 +98,7 @@ fn memo_works_properly() {
cx.render(rsx!(
div { "Hello, world!" }
child(na: "asd".to_string())
child(na: "asdfg".to_string())
))
}
@@ -110,9 +108,7 @@ fn memo_works_properly() {
}
fn child(cx: Scope<ChildProps>) -> Element {
rsx!(cx, div {
"goodbye world"
})
rsx!(cx, div { "goodbye world" })
}
let mut dom = new_dom(app, ());
@@ -123,4 +119,170 @@ fn memo_works_properly() {
dom.hard_diff(ScopeId(0));
dom.hard_diff(ScopeId(0));
dom.hard_diff(ScopeId(0));
dom.hard_diff(ScopeId(0));
dom.hard_diff(ScopeId(0));
}
#[test]
fn free_works_on_root_props() {
fn app(cx: Scope<Custom>) -> Element {
cx.render(rsx! {
child(a: "alpha")
child(a: "beta")
child(a: "gamma")
child(a: "delta")
})
}
#[derive(Props, PartialEq)]
struct ChildProps {
a: &'static str,
}
fn child(cx: Scope<ChildProps>) -> Element {
rsx!(cx, "child {cx.props.a}")
}
struct Custom {
val: String,
}
impl Drop for Custom {
fn drop(&mut self) {
dbg!("dropped!");
}
}
let mut dom = new_dom(
app,
Custom {
val: String::from("asd"),
},
);
dom.rebuild();
}
#[test]
fn free_works_on_borrowed() {
fn app(cx: Scope<()>) -> Element {
cx.render(rsx! {
child(a: "alpha", b: "asd".to_string())
})
}
#[derive(Props)]
struct ChildProps<'a> {
a: &'a str,
b: String,
}
fn child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
dbg!("rendering child");
rsx!(cx, "child {cx.props.a}, {cx.props.b}")
}
impl Drop for ChildProps<'_> {
fn drop(&mut self) {
dbg!("dropped child!");
}
}
let mut dom = new_dom(app, ());
let _ = dom.rebuild();
}
#[test]
fn free_works_on_root_hooks() {
/*
On Drop, scopearena drops all the hook contents.
*/
struct Droppable<T>(T);
impl<T> Drop for Droppable<T> {
fn drop(&mut self) {
dbg!("dropping droppable");
}
}
fn app(cx: Scope<()>) -> Element {
let name = cx.use_hook(|_| Droppable(String::from("asd")), |f| f);
rsx!(cx, div { "{name.0}" })
}
let mut dom = new_dom(app, ());
let _ = dom.rebuild();
}
#[test]
fn old_props_arent_stale() {
fn app(cx: Scope<()>) -> Element {
dbg!("rendering parent");
let cnt = cx.use_hook(|_| 0, |f| f);
*cnt += 1;
if *cnt == 1 {
rsx!(cx, div { child(a: "abcdef".to_string()) })
} else {
rsx!(cx, div { child(a: "abcdef".to_string()) })
}
}
#[derive(Props, PartialEq)]
struct ChildProps {
a: String,
}
fn child(cx: Scope<ChildProps>) -> Element {
dbg!("rendering child", &cx.props.a);
rsx!(cx, div { "child {cx.props.a}" })
}
let mut dom = new_dom(app, ());
let _ = dom.rebuild();
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
dom.work_with_deadline(|| false);
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
dom.work_with_deadline(|| false);
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
dom.work_with_deadline(|| false);
dbg!("forcing update to child");
dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
dom.work_with_deadline(|| false);
dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
dom.work_with_deadline(|| false);
dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
dom.work_with_deadline(|| false);
}
#[test]
fn basic() {
fn app(cx: Scope<()>) -> Element {
rsx!(cx, div {
child(a: "abcdef".to_string())
})
}
#[derive(Props, PartialEq)]
struct ChildProps {
a: String,
}
fn child(cx: Scope<ChildProps>) -> Element {
dbg!("rendering child", &cx.props.a);
rsx!(cx, div { "child {cx.props.a}" })
}
let mut dom = new_dom(app, ());
let _ = dom.rebuild();
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
dom.work_with_deadline(|| false);
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
dom.work_with_deadline(|| false);
}