Skip to content

Commit

Permalink
feat: Use clone for copying the APP, make the interactive use pre_eval
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Feb 14, 2024
1 parent a25fcee commit 83cc886
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
14 changes: 9 additions & 5 deletions crates/sauron-core/src/dom/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,10 @@ where
/// clone the app
pub fn app_clone(&self) -> APP {
/*
//TODO: This doesn't work all the time, maybe use Arc::RwLock as a replacement to
//Rc::RefCell
unsafe{
log::debug!("size APP: {}", std::mem::size_of::<APP>());
let borrowed_app = self.app_context.app.borrow();
log::debug!("size of borrowed_app: {}", std::mem::size_of_val(&*borrowed_app));
let app: APP = std::mem::transmute_copy(&*borrowed_app);
let app: APP = std::mem::transmute_copy(&*self.app_context.app.borrow());
app
}
*/
Expand Down Expand Up @@ -551,6 +550,8 @@ where
let patches = treepath.into_iter().flat_map(|path|{
let new_node = path.find_node_by_path(new_vdom).expect("new_node");
let old_node = path.find_node_by_path(&current_vdom).expect("old_node");
log::debug!("new_node: {new_node:#?}");
log::debug!("old_node: {old_node:#?}");
diff_recursive(
&old_node,
&new_node,
Expand Down Expand Up @@ -704,7 +705,10 @@ where
}


let treepath = self.app().pre_eval(&old_app).map(|eval|Eval::traverse(&eval));
let treepath = self.app().pre_eval(&old_app).map(|eval|{
log::debug!("eval: {eval:#?}");
Eval::traverse(&eval)
});

let cmd = self.app_context.batch_pending_cmds();

Expand Down
4 changes: 3 additions & 1 deletion crates/sauron-core/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ pub fn html_element<MSG>(
let mut corrected_children: Vec<Node<MSG>> = vec![];
for child in children {
if let Some(last) = corrected_children.last() {
if last.is_text() {
//TODO: put this behind a flag: #auto-separator to automatically
//add separator between text nodes
if last.is_text() && child.is_text(){
corrected_children.push(comment("separator"));
}
}
Expand Down
44 changes: 43 additions & 1 deletion examples/interactive/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Application<Msg> for App {
match msg {
Msg::Click => {
self.click_count += 1;
log::info!("click count: {}", self.click_count);
}
Msg::DoubleClick => {
self.double_clicks += 1;
Expand All @@ -78,6 +79,47 @@ impl Application<Msg> for App {
Cmd::none()
}

fn pre_eval(&self, old: &Self) -> Option<Vec<Eval>> {
Some(vec![
eval(false,
[
eval(false, [eval(true, [])]),
eval(false,
[
eval(false, []),
eval(false, []),
eval(false, []),
eval(false,
[eval(self.double_clicks != old.double_clicks, [])],
),
],
),
eval(false,
[
eval(self.name != old.name, []),
eval(false, []), // separator for in between text here
eval(self.click_count != old.click_count, [])
],
),
eval(false,
[
eval(false, []),
eval(false, []),
eval(false, [eval(self.biography != old.biography, [])]),
],
),
eval(false,
[
eval(false, []),
eval(false, []),
eval(self.thought != old.thought, []),
],
),
],
)]
)
}

fn view(&self) -> Node<Msg> {
let date_str: String = self
.date
Expand Down Expand Up @@ -126,7 +168,7 @@ impl Application<Msg> for App {
if self.click_count > 1 { "s" } else { "" }
)
} else {
span([], [])
text("here..")
},
],
),
Expand Down

0 comments on commit 83cc886

Please sign in to comment.