Skip to content

Commit 5390fd2

Browse files
committed
core: error if duplication insert detected on debug build
1 parent 0f52c53 commit 5390fd2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

async_ui_web_core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async_ui_internal_utils = { version = "0.0.2", path = "../async_ui_internal_util
2222
[dependencies.web-sys]
2323
version = "0.3.64"
2424
features = [
25+
'console',
2526
'Node',
2627
'Window',
2728
'Document',

async_ui_web_core/src/context.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'p> DomContext<'p> {
5050
container
5151
.insert_before(&new_child, reference_sibling)
5252
.unwrap_throw();
53-
group.insert(position, new_child);
53+
panic_if_duplicate_node(group.insert(position, new_child));
5454
}
5555
DomContext::Sibling {
5656
parent,
@@ -67,7 +67,7 @@ impl<'p> DomContext<'p> {
6767
.get_containing_node()
6868
.insert_before(&new_child, Some(reference_sibling))
6969
.unwrap_throw();
70-
group.insert(position, new_child);
70+
panic_if_duplicate_node(group.insert(position, new_child));
7171
}
7272
DomContext::Child { parent, index } => {
7373
position.wrap(*index);
@@ -117,3 +117,22 @@ fn remove_children_here(
117117
}
118118
}
119119
}
120+
121+
#[cfg(debug_assertions)]
122+
fn panic_if_duplicate_node(node: Option<web_sys::Node>) {
123+
if let Some(node) = node {
124+
web_sys::console::error_2(
125+
&"Attempted to insert two nodes at the same position.\n\
126+
You probably either used a `join` implementation from outside Async UI,\
127+
or tried to render something in a spawned Future.\n\
128+
This message is only shown in debug builds.\n\
129+
Check the code where you render this node:\
130+
"
131+
.into(),
132+
node.as_ref(),
133+
);
134+
panic!()
135+
}
136+
}
137+
#[cfg(not(debug_assertions))]
138+
fn panic_duplicate_node(_node: Option<web_sys::Node>) {}

0 commit comments

Comments
 (0)