Skip to content

Commit

Permalink
Delay register event for avoid hydrate bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Nov 13, 2023
1 parent e24ea2b commit e2bc222
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license = { workspace = true }
categories = { workspace = true }

[features]
default = ["web-csr"]
default = []
web-csr = ["__single_holder", "dep:wasm-bindgen"]
web-ssr = ["dep:tokio", "dep:tokio-util", "dep:futures-channel", "dep:base64"]
salvo = ["dep:salvo", "web-ssr"]
Expand Down
71 changes: 36 additions & 35 deletions crates/core/src/web/widgets/csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::ops::Deref;

use educe::Educe;
// #[cfg(all(target_arch = "wasm32", feature = "web-csr"))]
use wasm_bindgen::{JsCast, UnwrapThrowExt};
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast, JsValue, UnwrapThrowExt};

use crate::reflow::{Bond, Lotus};
use crate::view::{ViewId, ViewPosition};
Expand All @@ -30,6 +31,9 @@ where
#[educe(Debug(ignore))]
pub fillers: Vec<Filler>,

#[educe(Debug(ignore))]
listeners: Vec<Box<dyn FnOnce(&T)>>,

pub(crate) node: T,
}

Expand Down Expand Up @@ -99,6 +103,10 @@ where
value.inject_to(&ctx.view_id, &mut node.clone(), name, true);
}
self.classes.inject_to(&ctx.view_id, &mut node.clone(), "class", true);

for listener in std::mem::take(&mut self.listeners) {
(listener)(&self.node);
}
}
fn detach(&mut self, ctx: &mut Scope) {
if let Some(parent_node) = ctx.parent_node.as_ref() {
Expand Down Expand Up @@ -153,6 +161,7 @@ where
props: BTreeMap::new(),
fillers: vec![],
node,
listeners: vec![],
}
}

Expand Down Expand Up @@ -273,50 +282,42 @@ where

/// Adds an event listener to this element.
#[track_caller]
pub fn add_event_listener<E: EventDescriptor>(
&self,
event: E,
#[allow(unused_mut)] // used for tracing in debug
mut event_handler: impl FnMut(E::EventType) + 'static,
) {
#[cfg(all(target_arch = "wasm32", feature = "web-csr"))]
{
cfg_if! {
if #[cfg(debug_assertions)] {
let onspan = ::tracing::span!(
// parent: &self.span,
::tracing::Level::TRACE,
"on",
event = %event.name()
);
let _onguard = onspan.enter();
}
pub fn add_event_listener<E, H>(&mut self, event: E, handler: H)
where
E: EventDescriptor + 'static,
H: FnMut(E::EventType) + 'static,
{
cfg_if! {
if #[cfg(debug_assertions)] {
let onspan = ::tracing::span!(
// parent: &self.span,
::tracing::Level::TRACE,
"on",
event = %event.name()
);
let _onguard = onspan.enter();
}
}

self.listeners.push(Box::new(move |node| {
let event_name = event.name();

if event.bubbles() {
crate::web::add_event_listener(self.node.as_ref(), event_name, event_handler);
crate::web::add_event_listener(node.as_ref(), event_name, handler);
} else {
crate::web::add_event_listener_undelegated(self.node.as_ref(), &event_name, event_handler);
crate::web::add_event_listener_undelegated(node.as_ref(), &event_name, handler);
}
}

#[cfg(not(all(target_arch = "wasm32", feature = "web-csr")))]
{
_ = event;
_ = event_handler;
}
}));
}

/// Adds an event listener to this element.
#[track_caller]
pub fn on<E: EventDescriptor>(
self,
event: E,
#[allow(unused_mut)] // used for tracing in debug
mut event_handler: impl FnMut(E::EventType) + 'static,
) -> Self {
self.add_event_listener(event, event_handler);
pub fn on<E, H>(mut self, event: E, handler: H) -> Self
where
E: EventDescriptor + 'static,
H: FnMut(E::EventType) + 'static,
{
self.add_event_listener(event, handler);
self
}

Expand Down
13 changes: 6 additions & 7 deletions crates/core/src/web/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,14 @@ macro_rules! generate_tags {

/// Adds an event listener to this element.
#[track_caller]
pub fn on<E: EventDescriptor>(
self,
event: E,
#[allow(unused_mut)] // used for tracing in debug
mut event_handler: impl FnMut(E::EventType) + 'static,
) -> Self {
self.0.add_event_listener(event, event_handler);
pub fn on<E, H>(#[allow(unused_mut)]mut self, event: E, handler: H) -> Self
where
E: EventDescriptor + 'static,
H: FnMut(E::EventType) + 'static, {
self.0.add_event_listener(event, handler);
self
}

pub fn text<V>(mut self, text: V) -> Self
where
V: AttrValue + 'static,
Expand Down

0 comments on commit e2bc222

Please sign in to comment.