Skip to content

Commit

Permalink
fix: Cmd should use Program instead of WeakProgram
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Aug 17, 2023
1 parent cccc18c commit f95c7a1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
19 changes: 7 additions & 12 deletions crates/sauron-core/src/dom/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,7 +17,7 @@ where
{
/// the functions that would be executed when this Cmd is emited
#[allow(clippy::type_complexity)]
pub(crate) commands: Vec<Box<dyn FnOnce(WeakProgram<APP, MSG>)>>,
pub(crate) commands: Vec<Box<dyn FnOnce(Program<APP, MSG>)>>,
pub(crate) modifier: Modifier,
}

Expand All @@ -29,7 +29,7 @@ where
/// creates a new Cmd from a function
pub fn new<F>(f: F) -> Self
where
F: FnOnce(WeakProgram<APP, MSG>) + 'static,
F: FnOnce(Program<APP, MSG>) + 'static,
{
Self {
commands: vec![Box::new(f)],
Expand Down Expand Up @@ -97,7 +97,7 @@ where
}

/// Executes the Cmd
pub(crate) fn emit(self, program: WeakProgram<APP, MSG>) {
pub(crate) fn emit(self, program: Program<APP, MSG>) {
for cb in self.commands {
let program_clone = program.clone();
cb(program_clone);
Expand All @@ -109,8 +109,7 @@ where
///
pub fn batch_msg(msg_list: impl IntoIterator<Item = MSG>) -> Self {
let msg_list: Vec<MSG> = 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);
})
}
Expand Down Expand Up @@ -155,14 +154,10 @@ where
{
fn from(task: Task<MSG>) -> 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)
});
})
}
Expand Down
25 changes: 22 additions & 3 deletions crates/sauron-core/src/dom/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ where
}
}

impl<APP, MSG> Clone for Program<APP, MSG>
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<APP, MSG> Drop for Program<APP, MSG>
where
MSG: 'static,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -643,7 +662,7 @@ where
);
}

cmd.emit(Program::downgrade(&self));
cmd.emit(self.clone());
}

/// Inject a style to the global document
Expand Down
14 changes: 14 additions & 0 deletions crates/sauron-core/src/dom/program/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ where
}
}

impl<APP, MSG> Clone for AppContext<APP, MSG>
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<APP, MSG> AppContext<APP, MSG>
where
MSG: 'static,
Expand Down

0 comments on commit f95c7a1

Please sign in to comment.