fix: ssr + hydration event listeners

This commit is contained in:
Jonathan Kelley 2022-01-20 04:01:38 -05:00
parent 8c7473d194
commit c1a5d4e11f
2 changed files with 17 additions and 14 deletions

View file

@ -75,10 +75,8 @@ impl WebsysDom {
}
DomEdit::CreatePlaceholder { root: id } => self.create_placeholder(id),
DomEdit::NewEventListener {
event_name,
scope,
root: mounted_node_id,
} => self.new_event_listener(event_name, scope, mounted_node_id),
event_name, root, ..
} => self.new_event_listener(event_name, root),
DomEdit::RemoveEventListener { event, root } => {
self.remove_event_listener(event, root)
@ -172,10 +170,8 @@ impl WebsysDom {
let node = self.nodes[root as usize].as_ref().unwrap();
if let Some(element) = node.dyn_ref::<Element>() {
element.remove();
} else {
if let Some(parent) = node.parent_node() {
parent.remove_child(&node).unwrap();
}
} else if let Some(parent) = node.parent_node() {
parent.remove_child(node).unwrap();
}
}
@ -227,7 +223,7 @@ impl WebsysDom {
self.nodes[(id as usize)] = Some(el);
}
fn new_event_listener(&mut self, event: &'static str, _scope: ScopeId, _real_id: u64) {
pub fn new_event_listener(&mut self, event: &'static str, node: u64) {
let event = wasm_bindgen::intern(event);
// attach the correct attributes to the element
@ -235,7 +231,7 @@ impl WebsysDom {
// This ensures we only ever have one handler attached to the root, but decide
// dynamically when we want to call a listener.
let el = self.stack.top();
let el = self.nodes[(node as usize)].as_ref().unwrap();
let el = el.dyn_ref::<Element>().unwrap();
@ -278,7 +274,7 @@ impl WebsysDom {
.unwrap();
// Increment the listeners
self.listeners.insert(event.into(), (1, handler));
self.listeners.insert(event, (1, handler));
}
}
@ -442,7 +438,7 @@ impl WebsysDom {
anchor
.parent_node()
.unwrap()
.insert_before(&before, Some(&anchor))
.insert_before(&before, Some(anchor))
.unwrap();
} else {
let arr: js_sys::Array = self

View file

@ -116,7 +116,14 @@ impl WebsysDom {
// we cant have the last node be text
let mut last_node_was_text = false;
for child in vel.children {
self.rehydrate_single(nodes, place, dom, &child, &mut last_node_was_text)?;
self.rehydrate_single(nodes, place, dom, child, &mut last_node_was_text)?;
}
for listener in vel.listeners {
self.new_event_listener(
listener.event,
listener.mounted_node.get().unwrap().as_u64(),
);
}
place.pop();
@ -142,7 +149,7 @@ impl WebsysDom {
VNode::Fragment(el) => {
for el in el.children {
self.rehydrate_single(nodes, place, dom, &el, last_node_was_text)?;
self.rehydrate_single(nodes, place, dom, el, last_node_was_text)?;
}
}