Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyed reuse nodes #77

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions maple-core/src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,44 @@ where
let templates = Rc::clone(&templates);
let marker = marker.clone();
move || {
// Fast path for empty array. Remove all nodes from DOM in templates.
if iterable.get().is_empty() {
for (_, (owner, _value, template, _i)) in templates.borrow_mut().drain() {
drop(owner); // destroy owner
template.node.unchecked_into::<HtmlElement>().remove();
}
return;
}

// Remove old nodes not in iterable.
{
let mut templates = templates.borrow_mut();
let new_keys: HashSet<Key> =
iterable.get().iter().map(|item| key_fn(item)).collect();

let excess_nodes = templates
.iter()
.filter(|item| new_keys.get(item.0).is_none())
.map(|x| (x.0.clone(), (x.1 .2.clone(), x.1 .3)))
.collect::<Vec<_>>();

for node in &excess_nodes {
let removed_index = node.1 .1;
templates.remove(&node.0);

// Offset indexes of other templates by 1.
for (_, _, _, i) in templates.values_mut() {
if *i > removed_index {
*i -= 1;
}
}
}

for node in excess_nodes {
node.1 .0.node.unchecked_into::<Element>().remove();
}
}

struct PreviousData<T> {
value: T,
index: usize,
Expand Down Expand Up @@ -202,28 +240,6 @@ where
.unwrap();
}
}

if templates.borrow().len() > iterable.get().len() {
// remove extra templates

let mut templates = templates.borrow_mut();
let new_keys: HashSet<Key> =
iterable.get().iter().map(|item| key_fn(item)).collect();

let excess_nodes = templates
.iter()
.filter(|item| new_keys.get(item.0).is_none())
.map(|x| (x.0.clone(), x.1 .2.clone()))
.collect::<Vec<_>>();

for node in &excess_nodes {
templates.remove(&node.0);
}

for node in excess_nodes {
node.1.node.unchecked_into::<Element>().remove();
}
}
}
});

Expand Down Expand Up @@ -294,13 +310,22 @@ where
let templates = Rc::clone(&templates);
let marker = marker.clone();
move || {
// Fast path for empty array. Remove all nodes from DOM in templates.
if props.iterable.get().is_empty() {
for (owner, template) in templates.borrow_mut().drain(..) {
drop(owner); // destroy owner
template.node.unchecked_into::<HtmlElement>().remove();
}
return;
}

// Find values that changed by comparing to previous_values.
for (i, item) in props.iterable.get().iter().enumerate() {
let previous_values = previous_values.borrow();
let previous_value = previous_values.get(i);

if previous_value.is_none() || previous_value.unwrap() != item {
// value changed, re-render item
// Value changed, re-render item.

templates.borrow_mut().get_mut(i).and_then(|(owner, _)| {
// destroy old owner
Expand Down
54 changes: 54 additions & 0 deletions maple-core/tests/integration/keyed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,60 @@ fn swap_rows() {
assert_eq!(p.text_content().unwrap(), "123");
}

#[wasm_bindgen_test]
fn delete_row() {
let count = Signal::new(vec![1, 2, 3]);

let node = cloned!((count) => template! {
ul {
Keyed(KeyedProps {
iterable: count.handle(),
template: |item| template! {
li { (item) }
},
key: |item| *item,
})
}
});

render_to(|| node, &test_div());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");

count.set({
let mut tmp = (*count.get()).clone();
tmp.remove(1);
tmp
});
assert_eq!(p.text_content().unwrap(), "13");
}

#[wasm_bindgen_test]
fn clear() {
let count = Signal::new(vec![1, 2, 3]);

let node = cloned!((count) => template! {
ul {
Keyed(KeyedProps {
iterable: count.handle(),
template: |item| template! {
li { (item) }
},
key: |item| *item,
})
}
});

render_to(|| node, &test_div());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");

count.set(Vec::new());
assert_eq!(p.text_content().unwrap(), "");
}

#[wasm_bindgen_test]
fn insert_front() {
let count = Signal::new(vec![1, 2, 3]);
Expand Down
52 changes: 52 additions & 0 deletions maple-core/tests/integration/non_keyed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,58 @@ fn swap_rows() {
assert_eq!(p.text_content().unwrap(), "123");
}

#[wasm_bindgen_test]
fn delete_row() {
let count = Signal::new(vec![1, 2, 3]);

let node = cloned!((count) => template! {
ul {
Indexed(IndexedProps {
iterable: count.handle(),
template: |item| template! {
li { (item) }
},
})
}
});

render_to(|| node, &test_div());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");

count.set({
let mut tmp = (*count.get()).clone();
tmp.remove(1);
tmp
});
assert_eq!(p.text_content().unwrap(), "13");
}

#[wasm_bindgen_test]
fn clear() {
let count = Signal::new(vec![1, 2, 3]);

let node = cloned!((count) => template! {
ul {
Indexed(IndexedProps {
iterable: count.handle(),
template: |item| template! {
li { (item) }
},
})
}
});

render_to(|| node, &test_div());

let p = document().query_selector("ul").unwrap().unwrap();
assert_eq!(p.text_content().unwrap(), "123");

count.set(Vec::new());
assert_eq!(p.text_content().unwrap(), "");
}

#[wasm_bindgen_test]
fn insert_front() {
let count = Signal::new(vec![1, 2, 3]);
Expand Down