Skip to content

Commit

Permalink
fix: only store the Event closures
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Mar 31, 2024
1 parent 2b2d365 commit 06b335a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
4 changes: 0 additions & 4 deletions crates/core/src/dom/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use futures::channel::mpsc::UnboundedReceiver;
use futures::StreamExt;
use std::future::Future;
use std::pin::Pin;
use wasm_bindgen::closure::Closure;

/// encapsulate anything a component can do
pub enum Task<MSG> {
Expand Down Expand Up @@ -113,7 +112,6 @@ pub struct RecurringTask<MSG> {
pub(crate) receiver: UnboundedReceiver<MSG>,
/// store the associated closures so it is not dropped before being event executed
pub(crate) event_closures: Vec<EventClosure>,
pub(crate) closures: Vec<Closure<dyn FnMut()>>,
}

impl<MSG> RecurringTask<MSG>
Expand All @@ -134,7 +132,6 @@ where
let RecurringTask {
mut receiver,
event_closures,
closures,
} = self;
spawn_local(async move {
while let Some(msg) = receiver.next().await {
Expand All @@ -144,7 +141,6 @@ where
RecurringTask {
receiver: rx,
event_closures,
closures,
}
}
}
12 changes: 5 additions & 7 deletions crates/core/src/dom/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl Window {
Task::Recurring(RecurringTask {
receiver: rx,
event_closures: vec![resize_callback],
closures: vec![],
})
}

Expand All @@ -60,7 +59,6 @@ impl Window {
Task::Recurring(RecurringTask {
receiver: rx,
event_closures: vec![mousemove_cb],
closures: vec![],
})
}

Expand All @@ -86,7 +84,6 @@ impl Window {
Task::Recurring(RecurringTask {
receiver: rx,
event_closures: vec![mousemove_cb],
closures: vec![],
})
}

Expand All @@ -97,7 +94,10 @@ impl Window {
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
let closure_cb: Closure<dyn FnMut()> = Closure::new(move || {
//The web_sys::Event here is undefined, it is just used here to make storing the closure
//uniform
let closure_cb: Closure<dyn FnMut(web_sys::Event)> = Closure::new(move |_event| {
log::info!("event: {:?}", _event);
let msg = cb();
tx.start_send(msg).unwrap();
});
Expand All @@ -109,8 +109,7 @@ impl Window {
.expect("Unable to start interval");
Task::Recurring(RecurringTask {
receiver: rx,
event_closures: vec![],
closures: vec![closure_cb],
event_closures: vec![closure_cb],
})
}

Expand Down Expand Up @@ -149,7 +148,6 @@ impl Window {
Task::Recurring(RecurringTask {
receiver: rx,
event_closures: vec![closure_cb],
closures: vec![],
})
}
}
2 changes: 1 addition & 1 deletion examples/interactive-macro-syntax/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Self> {
Cmd::from(Window::every_interval(10_000, || Msg::Clock))
Cmd::from(Window::every_interval(1_000, || Msg::Clock))
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
Expand Down

0 comments on commit 06b335a

Please sign in to comment.