Skip to content

Commit

Permalink
fix: cleanup stateful component
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 15, 2024
1 parent 84f0d5f commit 1cfe8e8
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 37 deletions.
19 changes: 2 additions & 17 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{any::TypeId, cell::RefCell, fmt, rc::Rc};
use crate::vdom::TreePath;

use crate::{
dom::{
events::on_component_mount, program::MountProcedure, Application, Cmd, Component, DomAttrValue,
Expand All @@ -27,19 +25,10 @@ pub trait StatefulComponent {
fn remove_attribute(&mut self, _attr_name: AttributeName) {}


/// return the root node of this stateful component
fn root_node(&self) -> Option<DomNode>;

/// return the DomNode which contains the children DomNode
fn child_container(&self) -> Option<DomNode>;

/// return the TreePath to be able to traverse from root node to child container
fn traverse_child_container(&self) -> Option<TreePath>{
let root_node = self.root_node()?;
let child_container = self.child_container()?;
root_node.find_child(&child_container, TreePath::root())
}

/// append a child into this component
fn append_children(&mut self, _children: Vec<DomNode>) {}

Expand Down Expand Up @@ -165,18 +154,14 @@ where
let mut program = Program::from_rc_app(Rc::clone(&app));
let children: Vec<Node<MSG>> = children.into_iter().collect();
let mount_event = on_component_mount(move |me| {
log::info!("stateful component is now mounted...");
program.mount(&me.target_node.as_node(), MountProcedure::append());
});
let node = Node::Leaf(Leaf::StatefulComponent(StatefulModel {
Node::Leaf(Leaf::StatefulComponent(StatefulModel {
comp: app,
type_id,
attrs: attrs.into_iter().chain([mount_event]).collect(),
children: children.into_iter().collect(),
}));
log::info!("stateful component node: {}", node.render_to_string());
log::info!("stateful component node: {:#?}", node);
node
}))
}

#[cfg(feature = "with-dom")]
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ impl DomNode {
.expect("must be ok");
}

#[allow(unused)]
pub(crate) fn find_child(&self, target_child: &DomNode, path: TreePath) -> Option<TreePath>{
if self == target_child {
Some(path)
Expand Down
3 changes: 1 addition & 2 deletions crates/core/src/dom/dom_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ impl DomNode{
} else {
let idx = path.remove_first();
if let Some(children) = self.children(){
log::info!("has children..");
if let Some(child) = children.get(idx) {
child.find_node(path)
} else {
log::info!("There is no child at index: {idx}");
log::warn!("There is no child at index: {idx}");
None
}
}else{
Expand Down
1 change: 0 additions & 1 deletion crates/core/src/dom/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ where
pub(crate) fn create_initial_view(&self) -> DomNode {
let current_view = self.app_context.current_vdom();
let real_view = current_view.unwrap_template_ref();
log::info!("initial view: {}", real_view.render_to_string());
self.create_dom_node(Rc::new(None), real_view)
}

Expand Down
4 changes: 0 additions & 4 deletions crates/core/src/vdom/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,7 @@ pub fn diff_recursive<'a, MSG>(
patches.extend(patch);
}
(Leaf::StatefulComponent(old_comp), Leaf::StatefulComponent(new_comp)) => {
log::info!("old container: {:#?}", old_comp.comp.borrow().child_container());
log::info!("new container: {:#?}", new_comp.comp.borrow().child_container());
log::info!("path to child container: {:#?}", old_comp.comp.borrow().traverse_child_container());
let patch = diff_nodes(None, &old_comp.children, &new_comp.children, path);
log::info!("component patch: {:#?}", patch);
patches.extend(patch);
}
(Leaf::TemplatedView(_old_view), _) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/experimentals/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Application for App {
{component(&self.btn).map_msg(Msg::BtnMsg)}
</div>
<div>
//{stateful_component(Button::default(), [], [text!("External child of btn stateful_component: {}", self.click_count)])}
{stateful_component(Button::default(), [], [text!("External child of btn stateful_component: {}", self.click_count)])}
</div>
<div>
{stateful_component(DateTimeWidget::default(), [],[text("External child of date widget")])}
Expand Down
5 changes: 3 additions & 2 deletions examples/experimentals/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl StatefulComponent for Button {
self.children.extend(children);
}
}
fn root_node(&self) -> Option<DomNode> { todo!() }
fn child_container(&self) -> Option<DomNode> { todo!()}
fn child_container(&self) -> Option<DomNode> {
self.external_children_node.clone()
}
}
1 change: 0 additions & 1 deletion examples/experimentals/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ impl StatefulComponent for DateTimeWidget<()> {
self.children.extend(children);
}
}
fn root_node(&self) -> Option<DomNode> { todo!() }
fn child_container(&self) -> Option<DomNode> { todo!()}
}

Expand Down
9 changes: 0 additions & 9 deletions examples/fancy-ui/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,15 +645,6 @@ impl<XMSG> StatefulComponent for Frame<XMSG> {
self.children.extend(children);
}

fn root_node(&self) -> Option<DomNode>{
if let Some(root_node) = self.root_node.as_ref(){
Some(root_node.clone())
}else{
log::warn!("Root node is not set");
None
}
}

fn child_container(&self) -> Option<DomNode>{
if let Some(content_target_node) = self.content_target_node.as_ref(){
Some(content_target_node.clone())
Expand Down

0 comments on commit 1cfe8e8

Please sign in to comment.