diff --git a/crates/sauron-core/src/dom/cmd.rs b/crates/sauron-core/src/dom/cmd.rs index 299387f12..eff86b99d 100644 --- a/crates/sauron-core/src/dom/cmd.rs +++ b/crates/sauron-core/src/dom/cmd.rs @@ -3,7 +3,7 @@ //! use crate::dom::{Application, Effects, Modifier, Task}; use wasm_bindgen_futures::spawn_local; -use crate::dom::program::WeakProgram; +use crate::dom::Program; /// Cmd is a command to be executed by the system. /// This is returned at the init function of a component and is executed right @@ -17,7 +17,7 @@ where { /// the functions that would be executed when this Cmd is emited #[allow(clippy::type_complexity)] - pub(crate) commands: Vec)>>, + pub(crate) commands: Vec)>>, pub(crate) modifier: Modifier, } @@ -29,7 +29,7 @@ where /// creates a new Cmd from a function pub fn new(f: F) -> Self where - F: FnOnce(WeakProgram) + 'static, + F: FnOnce(Program) + 'static, { Self { commands: vec![Box::new(f)], @@ -97,7 +97,7 @@ where } /// Executes the Cmd - pub(crate) fn emit(self, program: WeakProgram) { + pub(crate) fn emit(self, program: Program) { for cb in self.commands { let program_clone = program.clone(); cb(program_clone); @@ -109,8 +109,7 @@ where /// pub fn batch_msg(msg_list: impl IntoIterator) -> Self { let msg_list: Vec = msg_list.into_iter().collect(); - Cmd::new(move |program| { - let mut program = program.upgrade().expect("must upgrade"); + Cmd::new(move |mut program| { program.dispatch_multiple(msg_list); }) } @@ -155,14 +154,10 @@ where { fn from(task: Task) -> Self { let task = task.task; - Cmd::new(move |program| { + Cmd::new(move |mut program| { spawn_local(async move { let msg = task.await; - if let Some(mut program) = program.upgrade(){ - program.dispatch(msg) - }else{ - log::error!("unable to upgrade program"); - } + program.dispatch(msg) }); }) } diff --git a/crates/sauron-core/src/dom/program.rs b/crates/sauron-core/src/dom/program.rs index d7fb6bc3b..594fa0e96 100644 --- a/crates/sauron-core/src/dom/program.rs +++ b/crates/sauron-core/src/dom/program.rs @@ -188,6 +188,25 @@ where } } +impl Clone for Program +where + MSG: 'static, +{ + fn clone(&self) -> Self { + Program { + app_context: self.app_context.clone(), + root_node: Rc::clone(&self.root_node), + mount_node: Rc::clone(&self.mount_node), + node_closures: Rc::clone(&self.node_closures), + mount_procedure: self.mount_procedure, + pending_patches: Rc::clone(&self.pending_patches), + idle_callback_handles: Rc::clone(&self.idle_callback_handles), + animation_frame_handles: Rc::clone(&self.animation_frame_handles), + event_closures: Rc::clone(&self.event_closures), + } + } +} + impl Drop for Program where MSG: 'static, @@ -236,7 +255,7 @@ where fn after_mounted(&mut self) { // call the init of the component let cmd = self.app_context.init_app(); - cmd.emit(Program::downgrade(&self)); + cmd.emit(self.clone()); // inject the app's dynamic style after the emitting the init function and it's effects self.inject_dynamic_style(); @@ -543,7 +562,7 @@ where // tell the app about the performance measurement and only if there was patches applied if modifier.log_measurements && measurements.total_patches > 0 { let cmd_measurement = self.app_context.measurements(measurements); - cmd_measurement.emit(Program::downgrade(&self)); + cmd_measurement.emit(self.clone()); } } @@ -643,7 +662,7 @@ where ); } - cmd.emit(Program::downgrade(&self)); + cmd.emit(self.clone()); } /// Inject a style to the global document diff --git a/crates/sauron-core/src/dom/program/app_context.rs b/crates/sauron-core/src/dom/program/app_context.rs index c1fdb4fc0..f2a222bae 100644 --- a/crates/sauron-core/src/dom/program/app_context.rs +++ b/crates/sauron-core/src/dom/program/app_context.rs @@ -104,6 +104,20 @@ where } } +impl Clone for AppContext +where + MSG: 'static, +{ + fn clone(&self) -> Self { + Self { + app: Rc::clone(&self.app), + current_vdom: Rc::clone(&self.current_vdom), + pending_msgs: Rc::clone(&self.pending_msgs), + pending_cmds: Rc::clone(&self.pending_cmds), + } + } +} + impl AppContext where MSG: 'static,