Skip to content

Commit

Permalink
Add and fix diffing of adding/removing dynamics inside a for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
OmegaJak committed Sep 29, 2023
1 parent 24b39a1 commit 7444a19
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
18 changes: 8 additions & 10 deletions axum-live-view/src/html/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) enum DynamicFragmentDiff<'a, T> {
#[serde(rename = "f", skip_serializing_if = "empty_slice")]
fixed: &'static [&'static str],
#[serde(rename = "b", skip_serializing_if = "BTreeMap::is_empty")]
dynamic: IndexMap<Option<IndexMap<DynamicFragmentDiff<'a, T>>>>,
dynamic: IndexMap<Option<IndexMap<Option<DynamicFragmentDiff<'a, T>>>>>,
},
}

Expand Down Expand Up @@ -53,7 +53,7 @@ impl<'a, T> From<&'a DynamicFragment<T>> for DynamicFragmentDiff<'a, T> {
*idx,
Some(
map.iter()
.map(|(idx, dynamic)| (*idx, dynamic.into()))
.map(|(idx, dynamic)| (*idx, Some(dynamic.into())))
.collect::<IndexMap<_>>(),
),
)
Expand Down Expand Up @@ -150,26 +150,24 @@ impl<T> DynamicFragment<T> {
Some(
from_other
.iter()
.map(|(idx, c)| (*idx, c.into()))
.map(|(idx, c)| (*idx, Some(c.into())))
.collect::<IndexMap<_>>(),
),
)),
Zipped::Both((from_idx, from_self), (other_idx, from_other)) => {
debug_assert_eq!(from_idx, other_idx);
let map = zip(from_self.iter(), from_other.iter())
.filter_map(|pair| match pair {
Zipped::Left(_) => {
unreachable!("unable to find a way to hit this yolo")
}
Zipped::Right(_) => {
unreachable!("unable to find a way to hit this yolo")
}
Zipped::Left((idx, _)) => Some((*idx, None)),
Zipped::Right((idx, value)) => Some((*idx, Some(value.into()))),
Zipped::Both(
(self_idx, self_value),
(other_idx, other_value),
) => {
debug_assert_eq!(self_idx, other_idx);
self_value.diff(other_value).map(|diff| (*self_idx, diff))
self_value
.diff(other_value)
.map(|diff| (*self_idx, Some(diff)))
}
})
.collect::<BTreeMap<_, _>>();
Expand Down
70 changes: 70 additions & 0 deletions axum-live-view/src/html/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,76 @@ fn diffing_loop_conditional() {
);
}

#[test]
fn diffing_loop_add_remove_inner_dynamic() {
let ns = &[1, 2];
let a: Html<()> = html! {
<ul>
for n in ns {
<li attr={"attr"}>
{n}
</li>
}
</ul>
};
let b: Html<()> = html! {
<ul>
for n in ns {
<li>
{n}
</li>
}
</ul>
};
assert_json_diff::assert_json_eq!(
pretty_print(a.diff(&b)),
json!({
"d": {
"0": {
"f": [
"<li>",
"</li>"
],
"b": {
"0": {
"0": "1",
"1": null
},
"1": {
"0": "2",
"1": null
}
}
}
}
})
);
assert_json_diff::assert_json_eq!(
pretty_print(b.diff(&a)),
json!({
"d": {
"0": {
"f": [
"<li attr=",
">",
"</li>"
],
"b": {
"0": {
"0": "attr",
"1": "1"
},
"1": {
"0": "attr",
"1": "2"
}
}
}
}
})
);
}

#[test]
fn diffing_message() {
fn render(msg: i32) -> Html<i32> {
Expand Down

0 comments on commit 7444a19

Please # to comment.