Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 4, 2024
1 parent 9ea3493 commit 96e6a8b
Show file tree
Hide file tree
Showing 27 changed files with 107 additions and 161 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
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;

/// Cnd 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: 1 addition & 3 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,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 Down
5 changes: 4 additions & 1 deletion crates/core/src/dom/component/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ fn create_fragment_node_no_listeners<MSG>(
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
4 changes: 1 addition & 3 deletions crates/core/src/dom/component/web_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ where
.app
.borrow_mut()
.connected_callback();
self.program
.update_dom()
.expect("must update dom");
self.program.update_dom().expect("must update dom");
}

/// called when the web component is removed
Expand Down
15 changes: 4 additions & 11 deletions crates/core/src/dom/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! when the application starts or after the application updates.
//!
use crate::dom::Program;
use crate::dom::{Application, Effects, Cmd};
use crate::dom::{Application, Cmd, Effects};
use wasm_bindgen_futures::spawn_local;

/// Dispatch is a command to be executed by the system.
Expand Down Expand Up @@ -58,17 +58,14 @@ where

/// Tell the runtime that there are no commands.
pub fn none() -> Self {
Dispatch {
commands: vec![],
}
Dispatch { commands: vec![] }
}

/// returns true if commands is empty
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}


/// Executes the Dispatch
pub(crate) fn emit(self, program: Program<APP>) {
for cb in self.commands {
Expand Down Expand Up @@ -96,16 +93,12 @@ where
fn from(effects: Effects<APP::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;

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


impl<APP, IN> From<IN> for Dispatch<APP>
where
APP: Application,
Expand All @@ -122,7 +115,7 @@ where
{
fn from(task: Cmd<APP::MSG>) -> Self {
Dispatch::new(move |program| {
for mut command in task.commands.into_iter(){
for mut command in task.commands.into_iter() {
let program = program.downgrade();
spawn_local(async move {
let mut program = program.upgrade().expect("upgrade");
Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/dom/document.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::dom::{dom_node::intern,Cmd};
use crate::dom::document;
use crate::dom::{dom_node::intern, Cmd};
use futures::channel::mpsc;
use wasm_bindgen::{prelude::*, JsCast};
use crate::dom::document;

/// Provides function for document related functions
#[derive(Clone, Copy)]
pub struct Document;

impl Document {

///
pub fn on_selectionchange<F, MSG>(mut cb: F) -> Cmd<MSG>
where
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ impl DomNode {
parent.replace_child(self, replacement);
} else {
//NOTE: This must be replacing a mount node
self
.as_element()
self.as_element()
.replace_with_with_node_1(&replacement.as_node())
.expect("must replace child");
}
Expand Down Expand Up @@ -517,9 +516,11 @@ impl DomNode {
Ok(())
}

fn dispatch_mount_event(&self){
let event_target:web_sys::EventTarget = self.as_element().unchecked_into();
event_target.dispatch_event(&MountEvent::create_web_event()).expect("must be ok");
fn dispatch_mount_event(&self) {
let event_target: web_sys::EventTarget = self.as_element().unchecked_into();
event_target
.dispatch_event(&MountEvent::create_web_event())
.expect("must be ok");
}

/// render this DomNode into an html string represenation
Expand Down Expand Up @@ -768,7 +769,6 @@ where
self.create_dom_node(parent_node, &real_comp_view)
}


/// set element with the dom attrs
pub(crate) fn set_element_dom_attrs(
&self,
Expand Down
Loading

0 comments on commit 96e6a8b

Please sign in to comment.