Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 15, 2024
1 parent 04ff34f commit fdc601e
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 61 deletions.
8 changes: 3 additions & 5 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{any::TypeId, cell::RefCell, fmt, rc::Rc};
use crate::{
dom::{
events::on_component_mount, program::MountProcedure, Application, Cmd, Component, DomAttrValue,
DomNode, Program,
events::on_component_mount, program::MountProcedure, Application, Cmd, Component,
DomAttrValue, DomNode, Program,
},
vdom::{Attribute, AttributeName, Leaf, Node},
};
use std::{any::TypeId, cell::RefCell, fmt, rc::Rc};

/// A component that can be used directly in the view without mapping
pub trait StatefulComponent {
Expand All @@ -24,8 +24,6 @@ pub trait StatefulComponent {
/// remove the attribute with this name
fn remove_attribute(&mut self, _attr_name: AttributeName) {}



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

Expand Down
12 changes: 8 additions & 4 deletions crates/core/src/dom/component/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use wasm_bindgen::JsCast;

use crate::{
dom::{
document, dom_node::intern, dom_node::DomInner, Application, DomAttr, DomAttrValue,
document, dom_node::intern, dom_node::DomInner, Application, DomAttr, DomAttrValue,
DomNode, GroupedDomAttrValues, Program, StatelessModel,
},
vdom::{self, Attribute, AttributeValue, Leaf},
Expand Down Expand Up @@ -324,7 +324,9 @@ where
template
}
_ => {
log::warn!("template and skip_diff is not found, fallback to no template and skip_diff");
log::warn!(
"template and skip_diff is not found, fallback to no template and skip_diff"
);
self.create_stateless_component(parent_node, comp)
}
}
Expand All @@ -349,7 +351,9 @@ where
dom_template
}
_ => {
log::warn!("template and skip_diff is not found, fallback to no template and skip_diff");
log::warn!(
"template and skip_diff is not found, fallback to no template and skip_diff"
);
self.create_initial_view()
}
}
Expand Down Expand Up @@ -389,7 +393,7 @@ impl DomInner {
Self::Symbol(_) => todo!(),
Self::Comment(_) => todo!(),
Self::Fragment { .. } => todo!(),
Self::StatefulComponent{..} => unreachable!("can not deep clone stateful component"),
Self::StatefulComponent { .. } => unreachable!("can not deep clone stateful component"),
}
}
}
38 changes: 18 additions & 20 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::dom::GroupedDomAttrValues;
use crate::dom::StatefulComponent;
use crate::dom::StatefulModel;
use crate::html::lookup;
use crate::vdom::TreePath;
use crate::{
dom::document,
dom::events::MountEvent,
Expand All @@ -19,7 +20,6 @@ use std::fmt;
use std::rc::Rc;
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
use web_sys::{self, Element, Node};
use crate::vdom::TreePath;

pub(crate) type EventClosure = Closure<dyn FnMut(web_sys::Event)>;
pub type NamedEventClosures = IndexMap<&'static str, EventClosure>;
Expand Down Expand Up @@ -60,7 +60,7 @@ pub enum DomInner {
children: Rc<RefCell<Vec<DomNode>>>,
},
/// StatefulComponent
StatefulComponent{
StatefulComponent {
comp: Rc<RefCell<dyn StatefulComponent>>,
dom_node: Rc<DomNode>,
},
Expand Down Expand Up @@ -92,7 +92,7 @@ impl fmt::Debug for DomInner {
Self::Symbol(symbol) => f.debug_tuple("Symbol").field(&symbol).finish(),
Self::Comment(_) => write!(f, "Comment"),
Self::Fragment { .. } => write!(f, "Fragment"),
Self::StatefulComponent{..} => write!(f, "StatefulComponent"),
Self::StatefulComponent { .. } => write!(f, "StatefulComponent"),
}
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ impl PartialEq for DomNode {
(DomInner::Text(v), DomInner::Text(o)) => v == o,
(DomInner::Symbol(v), DomInner::Symbol(o)) => v == o,
(DomInner::Comment(v), DomInner::Comment(o)) => v == o,
(DomInner::StatefulComponent{..}, DomInner::StatefulComponent{..}) => todo!(),
(DomInner::StatefulComponent { .. }, DomInner::StatefulComponent { .. }) => todo!(),
_ => false,
}
}
Expand All @@ -169,7 +169,7 @@ impl DomNode {

#[allow(unused)]
pub(crate) fn is_stateful_component(&self) -> bool {
matches!(&self.inner, DomInner::StatefulComponent{..})
matches!(&self.inner, DomInner::StatefulComponent { .. })
}

pub(crate) fn tag(&self) -> Option<String> {
Expand All @@ -187,9 +187,7 @@ impl DomNode {
DomInner::Text(text_node) => text_node.clone().unchecked_into(),
DomInner::Symbol(_) => panic!("don't know how to deal with symbol"),
DomInner::Comment(comment_node) => comment_node.clone().unchecked_into(),
DomInner::StatefulComponent{dom_node,..} => {
dom_node.as_node()
}
DomInner::StatefulComponent { dom_node, .. } => dom_node.as_node(),
}
}

Expand All @@ -205,7 +203,7 @@ impl DomNode {
DomInner::Text(text_node) => text_node.clone().unchecked_into(),
DomInner::Symbol(_) => panic!("don't know how to deal with symbol"),
DomInner::Comment(comment_node) => comment_node.clone().unchecked_into(),
DomInner::StatefulComponent{dom_node,..} => dom_node.as_element(),
DomInner::StatefulComponent { dom_node, .. } => dom_node.as_element(),
}
}

Expand Down Expand Up @@ -533,15 +531,15 @@ impl DomNode {
}

#[allow(unused)]
pub(crate) fn find_child(&self, target_child: &DomNode, path: TreePath) -> Option<TreePath>{
pub(crate) fn find_child(&self, target_child: &DomNode, path: TreePath) -> Option<TreePath> {
if self == target_child {
Some(path)
}else{
} else {
let children = self.children()?;
for (i,child) in children.iter().enumerate(){
for (i, child) in children.iter().enumerate() {
let child_path = path.traverse(i);
let found = child.find_child(target_child, child_path);
if found.is_some(){
if found.is_some() {
return found;
}
}
Expand Down Expand Up @@ -599,14 +597,13 @@ impl DomNode {
}
Ok(())
}
DomInner::StatefulComponent{comp: _, dom_node} => {
DomInner::StatefulComponent { comp: _, dom_node } => {
dom_node.render(buffer)?;
Ok(())
}
_ => todo!("for other else"),
}
}

}

#[cfg(feature = "with-interning")]
Expand Down Expand Up @@ -706,10 +703,12 @@ where
Leaf::NodeList(nodes) => self.create_fragment_node(parent_node, nodes),
Leaf::StatefulComponent(comp) => {
//TODO: also put the children and attributes here
DomNode{
inner: DomInner::StatefulComponent{
DomNode {
inner: DomInner::StatefulComponent {
comp: Rc::clone(&comp.comp),
dom_node: Rc::new(self.create_stateful_component(Rc::clone(&parent_node), comp)),
dom_node: Rc::new(
self.create_stateful_component(Rc::clone(&parent_node), comp),
),
},
parent: parent_node,
}
Expand Down Expand Up @@ -877,7 +876,6 @@ where
}
}


/// render the underlying real dom node into string
pub fn render_real_dom_to_string(node: &web_sys::Node) -> String {
let mut f = String::new();
Expand Down Expand Up @@ -918,7 +916,7 @@ pub fn render_real_dom(node: &web_sys::Node, buffer: &mut dyn fmt::Write) -> fmt

let child_nodes = element.child_nodes();
let child_count = child_nodes.length();
for i in 0..child_count{
for i in 0..child_count {
let child = child_nodes.get(i).unwrap();
render_real_dom(&child, buffer)?;
}
Expand Down
39 changes: 23 additions & 16 deletions crates/core/src/dom/dom_patch.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::dom;
use crate::dom::dom_node;
use crate::dom::dom_node::DomInner;
use crate::dom::DomAttr;
use crate::dom::DomAttrValue;
use crate::dom::DomNode;
use crate::dom::{Application, Program};
use crate::vdom::ComponentEventCallback;
use crate::vdom::EventCallback;
use crate::vdom::TreePath;
use crate::vdom::{Attribute, AttributeValue, Patch, PatchType};
use indexmap::IndexMap;
use std::rc::Rc;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsValue;
use crate::vdom::ComponentEventCallback;
use crate::dom::dom_node;

/// a Patch where the virtual nodes are all created in the document.
/// This is necessary since the created Node doesn't contain references
Expand Down Expand Up @@ -77,29 +77,34 @@ pub enum PatchVariant {
},
}

impl DomNode{

impl DomNode {
pub(crate) fn find_node(&self, path: &mut TreePath) -> Option<DomNode> {
match &self.inner{
DomInner::StatefulComponent{comp,..} => {
log::info!("This is a stateful component, should return the element
inside relative to the child container at this path: {:?}", path);
let child_container = comp.borrow().child_container().expect("stateful component should provide the child container");
match &self.inner {
DomInner::StatefulComponent { comp, .. } => {
log::info!(
"This is a stateful component, should return the element
inside relative to the child container at this path: {:?}",
path
);
let child_container = comp
.borrow()
.child_container()
.expect("stateful component should provide the child container");
child_container.find_node(path)
}
_ => {
if path.is_empty() {
Some(self.clone())
} else {
let idx = path.remove_first();
if let Some(children) = self.children(){
if let Some(children) = self.children() {
if let Some(child) = children.get(idx) {
child.find_node(path)
} else {
log::warn!("There is no child at index: {idx}");
None
}
}else{
} else {
log::warn!("Traversing to a childless node..");
None
}
Expand All @@ -125,15 +130,17 @@ impl DomNode{
tag,
&self
);
log::info!("real entire dom: {:#?}", dom_node::render_real_dom_to_string(&self.as_node()));
log::info!(
"real entire dom: {:#?}",
dom_node::render_real_dom_to_string(&self.as_node())
);
log::warn!("entire dom: {}", self.render_to_string());
}
}
nodes_to_patch
}
}


impl<APP> Program<APP>
where
APP: Application + 'static,
Expand All @@ -157,9 +164,9 @@ where
AttributeValue::EventListener(v) => {
Some(DomAttrValue::EventListener(self.convert_event_listener(v)))
}
AttributeValue::ComponentEventListener(v) => {
Some(DomAttrValue::EventListener(self.convert_component_event_listener(v)))
}
AttributeValue::ComponentEventListener(v) => Some(DomAttrValue::EventListener(
self.convert_component_event_listener(v),
)),
AttributeValue::Empty => None,
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/core/src/dom/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::dom::DomNode;
use crate::dom::{document, window, Event};
use crate::vdom;
use crate::vdom::ComponentEventCallback;
use crate::vdom::{Attribute, AttributeValue, EventCallback};
use wasm_bindgen::JsCast;
#[cfg(web_sys_unstable_apis)]
Expand All @@ -16,7 +17,6 @@ use web_sys::{
EventTarget, HtmlDetailsElement, HtmlElement, HtmlInputElement, HtmlSelectElement,
HtmlTextAreaElement,
};
use crate::vdom::ComponentEventCallback;

#[derive(Clone, Copy)]
#[repr(i16)]
Expand Down Expand Up @@ -177,7 +177,6 @@ where
)
}


macro_rules! declare_events {

( $(
Expand Down
1 change: 0 additions & 1 deletion crates/core/src/vdom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub mod patch;
/// for Components
pub type EventCallback<MSG> = Callback<Event, MSG>;


/// Mount callback is used for mounting the component into the DOM
/// This requires no MSG to be emitted
pub type ComponentEventCallback = Callback<Event, ()>;
4 changes: 2 additions & 2 deletions crates/core/src/vdom/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![allow(clippy::type_complexity)]

use derive_where::derive_where;
use indexmap::IndexMap;
use crate::vdom::ComponentEventCallback;
use crate::vdom::EventCallback;
use derive_where::derive_where;
use indexmap::IndexMap;

pub use attribute_value::AttributeValue;
pub use callback::Callback;
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/vdom/attribute/attribute_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{html::attributes::Style, vdom::EventCallback, vdom::Value};
use crate::vdom::ComponentEventCallback;
use crate::{html::attributes::Style, vdom::EventCallback, vdom::Value};
use derive_where::derive_where;

/// Values of an attribute can be in these variants
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/vdom/attribute/callback.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Callbacks contains function that can be called at a later time.
//! This is used in containing an event listener attached to an DOM element.
use std::{any::TypeId, fmt, rc::Rc, cell::RefCell};
use std::{any::TypeId, cell::RefCell, fmt, rc::Rc};

/// A generic sized representation of a function that can be
/// attached to a Node. The callback will essentially be owned by the element
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/vdom/map_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ impl<MSG> AttributeValue<MSG> {
AttributeValue::Simple(this) => AttributeValue::Simple(this),
AttributeValue::Style(this) => AttributeValue::Style(this),
AttributeValue::EventListener(this) => AttributeValue::EventListener(this.map_msg(cb)),
AttributeValue::ComponentEventListener(this) => AttributeValue::ComponentEventListener(this),
AttributeValue::ComponentEventListener(this) => {
AttributeValue::ComponentEventListener(this)
}
AttributeValue::Empty => AttributeValue::Empty,
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/experimentals/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl StatefulComponent for Button {
self.children.extend(children);
}
}
fn child_container(&self) -> Option<DomNode> {
fn child_container(&self) -> Option<DomNode> {
self.external_children_node.clone()
}
}
4 changes: 3 additions & 1 deletion examples/experimentals/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ impl StatefulComponent for DateTimeWidget<()> {
self.children.extend(children);
}
}
fn child_container(&self) -> Option<DomNode> { todo!()}
fn child_container(&self) -> Option<DomNode> {
todo!()
}
}

#[wasm_bindgen]
Expand Down
Loading

0 comments on commit fdc601e

Please sign in to comment.