Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Mar 9, 2024
1 parent 41e07ac commit 311d0bb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 33 deletions.
10 changes: 6 additions & 4 deletions crates/core/src/dom/component.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::html::attributes::{class, classes, Attribute};
use crate::vdom::AttributeName;
use crate::vdom::Leaf;
use crate::{dom::Effects, vdom::Node};
use std::any::TypeId;
use std::rc::Rc;
use crate::vdom::Leaf;

pub use stateful_component::{lookup_template, register_template, stateful_component, StatefulComponent, StatefulModel};
pub use stateful_component::{
lookup_template, register_template, stateful_component, StatefulComponent, StatefulModel,
};
#[cfg(feature = "custom_element")]
pub use web_component::{register_web_component, WebComponent, WebComponentWrapper};

Expand Down Expand Up @@ -139,11 +141,11 @@ pub(crate) fn extract_simple_struct_name<T: ?Sized>() -> String {
.expect("must have a name")
}

pub struct StatelessModel<MSG>{
pub struct StatelessModel<MSG> {
/// the view of this stateless model
pub view: Box<Node<MSG>>,
/// the vdom template of this component
#[cfg(feature = "use-template")]
#[cfg(feature = "use-template")]
pub vdom_template: Box<Node<MSG>>,
/// component type id
pub type_id: TypeId,
Expand Down
8 changes: 5 additions & 3 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ thread_local! {
static TEMPLATE_LOOKUP: RefCell<HashMap<TypeId, web_sys::Node>> = RefCell::new(HashMap::new());
}

pub fn register_template<MSG>(type_id: TypeId, view: &vdom::Node<MSG>) -> (web_sys::Node, vdom::Node<MSG>)
pub fn register_template<MSG>(
type_id: TypeId,
view: &vdom::Node<MSG>,
) -> (web_sys::Node, vdom::Node<MSG>)
where
MSG: 'static,
{

let (dom_template, vdom_template) = template::build_template(&view);
let template = TEMPLATE_LOOKUP.with_borrow_mut(|map| {
if let Some(existing) = map.get(&type_id) {
Expand All @@ -42,7 +44,7 @@ where
}

/// lookup for the template
pub fn lookup_template(type_id: TypeId) -> Option<web_sys::Node>{
pub fn lookup_template(type_id: TypeId) -> Option<web_sys::Node> {
TEMPLATE_LOOKUP.with_borrow_mut(|map| {
if let Some(existing) = map.get(&type_id) {
Some(existing.clone_node_with_deep(true).expect("deep clone"))
Expand Down
17 changes: 10 additions & 7 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::dom::component::lookup_template;
use crate::dom::component::register_template;
use crate::dom::component::StatelessModel;
use crate::dom::DomAttr;
use crate::dom::GroupedDomAttrValues;
use crate::dom::StatefulModel;
use crate::html::lookup;
use crate::vdom::diff;
use crate::vdom::AttributeName;
use crate::dom::StatefulModel;
use crate::vdom::TreePath;
use crate::{
dom::document,
Expand All @@ -17,10 +21,6 @@ use std::fmt;
use std::{cell::Cell, collections::BTreeMap};
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
use web_sys::{self, Element, Node, Text};
use crate::vdom::diff;
use crate::dom::component::register_template;
use crate::dom::component::StatelessModel;
use crate::dom::component::lookup_template;

/// data attribute name used in assigning the node id of an element with events
pub(crate) const DATA_VDOM_ID: &str = "data-vdom-id";
Expand Down Expand Up @@ -225,8 +225,11 @@ where
let template = lookup_template(comp.type_id).expect("must have a template");
let patches = diff(&comp.vdom_template, &comp.view);
log::info!("patching template: {:#?}", patches);
let dom_patches = self.convert_patches(&template, &patches).expect("convert patches");
self.apply_dom_patches(&template, dom_patches).expect("patch template");
let dom_patches = self
.convert_patches(&template, &patches)
.expect("convert patches");
self.apply_dom_patches(&template, dom_patches)
.expect("patch template");
template
}
#[cfg(not(feature = "use-template"))]
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/dom/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ pub(crate) fn extract_static_only<MSG>(node: &Node<MSG>) -> vdom::Node<MSG> {
Leaf::SafeHtml(_) => Node::Leaf(Leaf::SafeHtml("".into())),
Leaf::Comment(_) => Node::Leaf(Leaf::Comment("".into())),
Leaf::DocType(_) => Node::Leaf(Leaf::DocType("".into())),
Leaf::StatefulComponent { .. } => {
Node::Leaf(Leaf::Comment(" ---stateful template placeholder--- ".into()))
}
Leaf::StatelessComponent {..} => {
Node::Leaf(Leaf::Comment(" ---stateless template placeholder--- ".into()))
}
Leaf::StatefulComponent { .. } => Node::Leaf(Leaf::Comment(
" ---stateful template placeholder--- ".into(),
)),
Leaf::StatelessComponent { .. } => Node::Leaf(Leaf::Comment(
" ---stateless template placeholder--- ".into(),
)),
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/core/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,13 @@ impl<MSG> Leaf<MSG> {
Leaf::DocType(doctype) => {
write!(buffer, "<!doctype {doctype}>")
}
Leaf::StatefulComponent ( _comp ) => {
Leaf::StatefulComponent(_comp) => {
//TODO: The component will be rendered based
// on its attributes and children
// maybe just call it's view directly
todo!()
}
Leaf::StatelessComponent(comp) => {
comp.view.render(buffer)
}
Leaf::StatelessComponent(comp) => comp.view.render(buffer),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions crates/core/src/vdom/leaf.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Leaf node for html dom tree
use crate::dom::StatefulComponent;
use crate::dom::StatefulModel;
use crate::dom::StatelessModel;
use crate::vdom::{Attribute, Node};
use std::any::TypeId;
use std::borrow::Cow;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use crate::dom::StatefulModel;

/// A leaf node value of html dom tree
pub enum Leaf<MSG> {
Expand Down Expand Up @@ -39,8 +39,6 @@ impl<MSG> Clone for Leaf<MSG> {
}
}



impl<MSG> fmt::Debug for Leaf<MSG> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down
10 changes: 4 additions & 6 deletions examples/js-performance-benchmark-sauron/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rand::prelude::*;
use sauron::dom::component;
use sauron::*;
use std::cmp::min;
use sauron::dom::component;

static ADJECTIVES: &[&str] = &[
"pretty",
Expand Down Expand Up @@ -59,19 +59,17 @@ impl RowData {
}
}

enum RowMsg{
enum RowMsg {
Remove(usize),
Select(usize),
}

struct Row{
struct Row {
data: RowData,
selected: bool,
}



impl Component<RowMsg, Msg> for Row{
impl Component<RowMsg, Msg> for Row {
fn update(&mut self, msg: RowMsg) -> Effects<RowMsg, Msg> {
Effects::none()
}
Expand Down

0 comments on commit 311d0bb

Please sign in to comment.