Skip to content

Commit

Permalink
Merge pull request #93 from slowtec/fix-clippy-lints
Browse files Browse the repository at this point in the history
Fix clippy lints
  • Loading branch information
ivanceras authored Apr 12, 2024
2 parents 299815b + ae66a35 commit 4673a4d
Show file tree
Hide file tree
Showing 41 changed files with 215 additions and 309 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/dom.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! This module provides functionalities for
//! manipulating the actual Document Object Model in the browser
pub use cmd::Cmd;
pub use component::Component;
pub use effects::Effects;
pub use cmd::Cmd;

mod cmd;
mod component;
mod effects;
mod cmd;

use cfg_if::cfg_if;

Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/dom/application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::dom::Cmd;
use crate::vdom::Node;
pub use skip_diff::{skip_if, SkipDiff, SkipPath};
use crate::dom::Cmd;

///
pub mod skip_diff;
Expand Down Expand Up @@ -40,8 +40,7 @@ pub trait Application: Sized + 'static {
/// This is for diagnostic and performance measurement purposes.
///
/// Warning: DO NOT use for anything else other than the intended purpose
fn measurements(&mut self, _measurements: Measurements){
}
fn measurements(&mut self, _measurements: Measurements) {}
}

/// Contains the time it took for the last app update call for the component
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/dom/application/skip_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl SkipDiff {
pub fn in_path(&self, path: &TreePath) -> Option<&Self> {
let mut path = path.clone();
if path.is_empty() {
Some(&self)
Some(self)
} else {
let idx = path.remove_first();
if let Some(child) = self.children.get(idx) {
Expand Down
53 changes: 26 additions & 27 deletions crates/core/src/dom/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::dom::Effects;
use futures::channel::mpsc;
use futures::channel::mpsc::UnboundedReceiver;
use futures::StreamExt;
use std::future::Future;
use std::pin::Pin;
use crate::dom::Effects;
#[cfg(feature = "with-dom")]
use wasm_bindgen::closure::Closure;

/// Cmd is a way to tell the Runtime that something needs to be executed
pub struct Cmd<MSG>{
pub struct Cmd<MSG> {
/// commands
pub(crate) commands: Vec<Command<MSG>>,
}
Expand All @@ -22,7 +22,6 @@ pub enum Command<MSG> {
Sub(Sub<MSG>),
}


impl<MSG> Cmd<MSG>
where
MSG: 'static,
Expand All @@ -40,13 +39,16 @@ where
where
F: Future<Output = MSG> + 'static,
{
Self{
Self {
commands: vec![Command::single(f)],
}
}
/// Creates a Cmd which will be polled multiple times
pub fn recurring(rx: UnboundedReceiver<MSG>, event_closure: Closure<dyn FnMut(web_sys::Event)>) -> Self {
Self{
pub fn recurring(
rx: UnboundedReceiver<MSG>,
event_closure: Closure<dyn FnMut(web_sys::Event)>,
) -> Self {
Self {
commands: vec![Command::sub(rx, event_closure)],
}
}
Expand All @@ -57,48 +59,44 @@ where
F: Fn(MSG) -> MSG2 + 'static + Clone,
MSG2: 'static,
{
Cmd{
commands: self.commands.into_iter().map(|t|t.map_msg(f.clone())).collect(),
Cmd {
commands: self
.commands
.into_iter()
.map(|t| t.map_msg(f.clone()))
.collect(),
}
}

/// batch together multiple Cmd into one task
pub fn batch(tasks: impl IntoIterator<Item = Self>) -> Self {
let mut commands = vec![];
for task in tasks.into_iter(){
for task in tasks.into_iter() {
commands.extend(task.commands);
}
Self {commands,
}
Self { commands }
}

///
pub fn none() -> Self {
Self{commands: vec![],
}
Self { commands: vec![] }
}


}


impl<MSG> From<Effects<MSG, ()>> for Cmd<MSG>
where MSG: 'static
where
MSG: 'static,
{
/// Convert Effects that has only follow ups
fn from(effects: Effects<MSG, ()>) -> Self {
// we can safely ignore the effects here
// as there is no content on it.
let Effects {
local,
external:_,
} = effects;
let Effects { local, external: _ } = effects;

Cmd::batch(local.into_iter().map(Cmd::from))
}
}


impl<MSG> Command<MSG>
where
MSG: 'static,
Expand All @@ -111,10 +109,13 @@ where
Self::Action(Action::new(f))
}

///
///
#[cfg(feature = "with-dom")]
pub fn sub(rx: UnboundedReceiver<MSG>, event_closure: Closure<dyn FnMut(web_sys::Event)>) -> Self {
Self::Sub(Sub{
pub fn sub(
rx: UnboundedReceiver<MSG>,
event_closure: Closure<dyn FnMut(web_sys::Event)>,
) -> Self {
Self::Sub(Sub {
receiver: rx,
event_closure,
})
Expand All @@ -141,7 +142,6 @@ where
Self::Sub(task) => task.next().await,
}
}

}

/// Action is used to do asynchronous operations
Expand All @@ -168,7 +168,6 @@ where
}
}


/// apply a function to the msg to create a different task which has a different msg
fn map_msg<F, MSG2>(self, f: F) -> Action<MSG2>
where
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/dom/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub trait Component {
where
Self: Sized,
{
let class_names: Vec<&str> = class_name.split(" ").collect();
let class_names: Vec<&str> = class_name.split(' ').collect();
let prefixed_classes = class_names
.iter()
.map(|c| Self::prefix_class(c))
Expand Down Expand Up @@ -194,7 +194,7 @@ impl<MSG> Clone for StatelessModel<MSG> {
fn clone(&self) -> Self {
Self {
view: self.view.clone(),
type_id: self.type_id.clone(),
type_id: self.type_id,
}
}
}
Expand Down
44 changes: 16 additions & 28 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
use crate::dom::events::on_mount;
use crate::dom::program::MountProcedure;
use crate::dom::Application;
use crate::dom::Cmd;
use crate::dom::Component;
use crate::dom::DomAttrValue;
use crate::dom::DomNode;
use crate::dom::Program;
use crate::vdom::Attribute;
use crate::vdom::AttributeName;
use crate::vdom::Leaf;
use crate::vdom::Node;
use std::any::TypeId;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use std::{any::TypeId, cell::RefCell, fmt, rc::Rc};

use crate::{
dom::{
events::on_mount, program::MountProcedure, Application, Cmd, Component, DomAttrValue,
DomNode, Program,
},
vdom::{Attribute, AttributeName, Leaf, Node},
};

/// A component that can be used directly in the view without mapping
pub trait StatefulComponent {
Expand Down Expand Up @@ -94,7 +87,7 @@ impl<MSG> Clone for StatefulModel<MSG> {
fn clone(&self) -> Self {
Self {
comp: Rc::clone(&self.comp),
type_id: self.type_id.clone(),
type_id: self.type_id,
attrs: self.attrs.clone(),
children: self.children.clone(),
}
Expand Down Expand Up @@ -145,9 +138,7 @@ pub fn stateful_component<COMP, MSG, MSG2>(
children: impl IntoIterator<Item = Node<MSG>>,
) -> Node<MSG>
where
COMP: Component<MSG = MSG2, XMSG = ()>
+ StatefulComponent
+ Application<MSG=MSG2> + 'static,
COMP: Component<MSG = MSG2, XMSG = ()> + StatefulComponent + Application<MSG = MSG2> + 'static,
MSG: Default + 'static,
MSG2: 'static,
{
Expand All @@ -173,17 +164,14 @@ where
}

#[cfg(feature = "with-dom")]
impl Into<DomAttrValue> for wasm_bindgen::JsValue {
fn into(self) -> DomAttrValue {
if let Some(v) = self.as_bool() {
impl From<wasm_bindgen::JsValue> for DomAttrValue {
fn from(val: wasm_bindgen::JsValue) -> Self {
if let Some(v) = val.as_bool() {
DomAttrValue::Simple(v.into())
} else if let Some(v) = self.as_f64() {
} else if let Some(v) = val.as_f64() {
DomAttrValue::Simple(v.into())
} else if let Some(v) = self.as_string() {
} else if let Some(v) = val.as_string() {
DomAttrValue::Simple(v.into())
} else if self.is_null() {
log::info!("it is a null value");
DomAttrValue::Empty
} else {
todo!("handle other conversion, other than bool, f64, strings, nulls ")
}
Expand Down
62 changes: 24 additions & 38 deletions crates/core/src/dom/component/template.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use crate::dom::document;
use crate::dom::dom_node::intern;
use crate::dom::dom_node::DomInner;
use crate::dom::now;
use crate::dom::Application;
use crate::dom::DomAttr;
use crate::dom::DomAttrValue;
use crate::dom::DomNode;
use crate::dom::GroupedDomAttrValues;
use crate::dom::Program;
use crate::dom::StatelessModel;
use crate::vdom;
use crate::vdom::Attribute;
use crate::vdom::AttributeValue;
use crate::vdom::Leaf;
use std::any::TypeId;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::{any::TypeId, cell::RefCell, collections::hash_map, collections::HashMap, rc::Rc};

use wasm_bindgen::JsCast;

use crate::{
dom::{
document, dom_node::intern, dom_node::DomInner, now, Application, DomAttr, DomAttrValue,
DomNode, GroupedDomAttrValues, Program, StatelessModel,
},
vdom::{self, Attribute, AttributeValue, Leaf},
};

thread_local! {
static TEMPLATE_LOOKUP: RefCell<HashMap<TypeId, DomNode>> = RefCell::new(HashMap::new());
}
Expand All @@ -33,32 +24,25 @@ pub fn register_template<MSG>(
if let Some(template) = lookup_template(type_id) {
template
} else {
let template = create_dom_node_no_listeners(parent_node, &vdom_template);
let template = create_dom_node_no_listeners(parent_node, vdom_template);
add_template(type_id, &template);
template
}
}

pub fn add_template(type_id: TypeId, template: &DomNode) {
TEMPLATE_LOOKUP.with_borrow_mut(|map| {
if map.contains_key(&type_id) {
// already added
if let hash_map::Entry::Vacant(e) = map.entry(type_id) {
e.insert(template.deep_clone());
} else {
map.insert(type_id, template.deep_clone());
// already added
}
})
}

/// lookup for the template
pub fn lookup_template(type_id: TypeId) -> Option<DomNode> {
TEMPLATE_LOOKUP.with_borrow(|map| {
if let Some(existing) = map.get(&type_id) {
// TODO: have to traverse the real children and convert each into DomNode
Some(existing.deep_clone())
} else {
None
}
})
TEMPLATE_LOOKUP.with_borrow(|map| map.get(&type_id).map(|existing| existing.deep_clone()))
}

#[cfg(feature = "with-debug")]
Expand Down Expand Up @@ -101,7 +85,7 @@ impl Section {
}

#[cfg(feature = "with-debug")]
thread_local!(pub static TIME_SPENT: RefCell<Vec<Section>> = RefCell::new(vec![]));
thread_local!(pub static TIME_SPENT: RefCell<Vec<Section>> = const { RefCell::new(vec![]) });

#[cfg(feature = "with-debug")]
pub fn add_time_trace(section: Section) {
Expand Down Expand Up @@ -164,14 +148,17 @@ fn create_fragment_node_no_listeners<MSG>(
};
let dom_node_rc = Rc::new(Some(dom_node.clone()));
let children = nodes
.into_iter()
.map(|node| create_dom_node_no_listeners(Rc::clone(&dom_node_rc), &node))
.iter()
.map(|node| create_dom_node_no_listeners(Rc::clone(&dom_node_rc), node))
.collect();
dom_node.append_children(children);
dom_node
}

fn create_leaf_node_no_listeners<MSG>(parent_node: Rc<Option<DomNode>>, leaf: &Leaf<MSG>) -> DomNode {
fn create_leaf_node_no_listeners<MSG>(
parent_node: Rc<Option<DomNode>>,
leaf: &Leaf<MSG>,
) -> DomNode {
match leaf {
Leaf::Text(txt) => DomNode {
inner: DomInner::Text(document().create_text_node(txt)),
Expand Down Expand Up @@ -312,7 +299,7 @@ where
#[cfg(feature = "with-debug")]
let t2 = now();
let patches =
self.create_patches_with_skip_diff(&vdom_template, &real_comp_view, &skip_diff);
self.create_patches_with_skip_diff(&vdom_template, real_comp_view, &skip_diff);
#[cfg(feature = "with-debug")]
let t3 = now();
let dom_patches = self
Expand Down Expand Up @@ -353,8 +340,7 @@ where
let dom_patches = self
.convert_patches(&dom_template, &patches)
.expect("convert patches");
let _new_template_node = self
.apply_dom_patches(dom_patches)
self.apply_dom_patches(dom_patches)
.expect("template patching");
dom_template
}
Expand Down
Loading

0 comments on commit 4673a4d

Please sign in to comment.