Skip to content

Commit

Permalink
Remove wrapping event handlers in signals
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Oct 6, 2023
1 parent 4f2d95e commit 878948a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
6 changes: 4 additions & 2 deletions examples/js-framework-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ fn App<G: Html>() -> View<G> {
let remove = move |id| data.update(|d| d.retain(|row| row.id != id));

let run = move || {
data.set(build_data(1000));
selected.set(None);
data.set(build_data(1000));
};

let runlots = move || {
data.set(build_data(10000));
selected.set(None);
data.set(build_data(10000));
};

let add = move || {
Expand Down Expand Up @@ -189,5 +189,7 @@ fn main() {

#[cfg(debug_assertions)]
console_error_panic_hook::set_once();
tracing_wasm::set_as_global_default();

sycamore::render_to(App, &mount_el);
}
25 changes: 5 additions & 20 deletions packages/sycamore-web/src/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use web_sys::{Comment, Element, Node, Text};
use crate::dom_node_template::{
add_new_cached_template, execute_walk, try_get_cached_template, WalkResult,
};
use crate::{document, queue_microtask, Html};
use crate::{document, Html};

#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -271,29 +271,14 @@ impl GenericNode for DomNode {
event: Cow<'_, str>,
mut handler: Box<dyn FnMut(Self::AnyEventData) + 'static>,
) {
// Run the handler in the current scope where the event is attached.
let scope = use_global_scope();
let signal = create_signal(None);
let cb = Closure::new(move |x| {
// Take the closure out of the signal so that we can make sure it is alive for at least
// the duration of this call.
let closure = signal.replace(None).expect("recursive event handler");
scope.run_in(|| handler(x));
if signal.is_alive() {
signal.set(Some(closure));
} else {
queue_microtask(move || drop(closure));
}
});
signal.set(Some(cb));
signal.with(|closure| {
self.node
.add_event_listener_with_callback(
&event,
closure.as_ref().unwrap().as_ref().unchecked_ref(),
)
.unwrap_throw();
});
self.node
.add_event_listener_with_callback(&event, cb.as_ref().unchecked_ref())
.unwrap_throw();
on_cleanup(move || drop(cb));
}

fn update_inner_text(&self, text: Cow<'static, str>) {
Expand Down

0 comments on commit 878948a

Please sign in to comment.