Skip to content

Commit

Permalink
refactor: **breaking** reclaim back Cmd which is now the name for Tas…
Browse files Browse the repository at this point in the history
…k/Sub/Action
  • Loading branch information
ivanceras committed Apr 1, 2024
1 parent a8d711b commit 0721ea0
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 88 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
pub use component::Component;
pub use effects::Effects;
pub use modifier::Modifier;
pub use task::Task;
pub use cmd::Cmd;

mod component;
mod effects;
mod modifier;
mod task;
mod cmd;

use cfg_if::cfg_if;

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

///
pub mod skip_diff;
Expand All @@ -13,15 +13,15 @@ pub trait Application: Sized + 'static {
type MSG;
/// The application can implement this method where it can modify its initial state.
/// This method is called right after the program is mounted into the DOM.
fn init(&mut self) -> Task<Self::MSG> {
Task::none()
fn init(&mut self) -> Cmd<Self::MSG> {
Cmd::none()
}

/// Update the component with a message.
/// The update function returns a Dispatch, which can be executed by the runtime.
///
/// Called each time an action is triggered from the view
fn update(&mut self, _msg: Self::MSG) -> Task<Self::MSG>;
fn update(&mut self, _msg: Self::MSG) -> Cmd<Self::MSG>;

/// Returns a node on how the component is presented.
fn view(&self) -> Node<Self::MSG>;
Expand All @@ -40,9 +40,9 @@ 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(&self, measurements: Measurements) -> Task<Self::MSG> {
fn measurements(&self, measurements: Measurements) -> Cmd<Self::MSG> {
log::debug!("Measurements: {:#?}", measurements);
Task::none().no_render()
Cmd::none().no_render()
}
}

Expand Down
37 changes: 29 additions & 8 deletions crates/core/src/dom/task.rs → crates/core/src/dom/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pub enum Command<MSG> {
}

///
pub struct Task<MSG>{
pub struct Cmd<MSG>{
/// commands
pub(crate) commands: Vec<Command<MSG>>,
pub(crate) modifier: Modifier,
}

impl<MSG> Task<MSG>
impl<MSG> Cmd<MSG>
where
MSG: 'static,
{
Expand All @@ -45,19 +45,19 @@ where
}
}

/// map the msg of this Task such that Task<MSG> becomes Task<MSG2>.
pub fn map_msg<F, MSG2>(self, f: F) -> Task<MSG2>
/// map the msg of this Cmd such that Cmd<MSG> becomes Cmd<MSG2>.
pub fn map_msg<F, MSG2>(self, f: F) -> Cmd<MSG2>
where
F: Fn(MSG) -> MSG2 + 'static + Clone,
MSG2: 'static,
{
Task{
Cmd{
commands: self.commands.into_iter().map(|t|t.map_msg(f.clone())).collect(),
modifier: Default::default(),
}
}

/// batch together multiple Task into one task
/// 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(){
Expand All @@ -83,7 +83,8 @@ where

}

impl<MSG> From<Effects<MSG, MSG>> for Task<MSG>
/*
impl<MSG> From<Effects<MSG, MSG>> for Cmd<MSG>
where MSG: 'static
{
/// Convert Effects that has only follow ups
Expand All @@ -96,7 +97,27 @@ impl<MSG> From<Effects<MSG, MSG>> for Task<MSG>
modifier:_,
} = effects;
Task::batch(local.into_iter().chain(external.into_iter()).map(Task::from))
Cmd::batch(local.into_iter().chain(external.into_iter()).map(Cmd::from))
}
}
*/

impl<MSG> From<Effects<MSG, ()>> for Cmd<MSG>
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:_,
modifier,
} = effects;

let mut cmd = Cmd::batch(local.into_iter().map(Cmd::from));
cmd.modifier = modifier;
cmd
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::dom::events::on_mount;
use crate::dom::program::MountProcedure;
use crate::dom::Application;
use crate::dom::Task;
use crate::dom::Cmd;
use crate::dom::Component;
use crate::dom::DomAttrValue;
use crate::dom::DomNode;
Expand Down
6 changes: 3 additions & 3 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, Modifier, Task};
use crate::dom::{Application, Effects, Modifier, Cmd};
use wasm_bindgen_futures::spawn_local;

/// Dispatch is a command to be executed by the system.
Expand Down Expand Up @@ -150,11 +150,11 @@ where
}
}

impl<APP> From<Task<APP::MSG>> for Dispatch<APP>
impl<APP> From<Cmd<APP::MSG>> for Dispatch<APP>
where
APP: Application,
{
fn from(task: Task<APP::MSG>) -> Self {
fn from(task: Cmd<APP::MSG>) -> Self {
Dispatch::new(move |program| {
for mut command in task.commands.into_iter(){
let program = program.downgrade();
Expand Down
1 change: 0 additions & 1 deletion crates/core/src/dom/dom_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ where
first_node.insert_after(replacement);
}
if patch_path.path.is_empty() {
log::info!("setting root node to the first node at non-fragment");
*self.root_node.borrow_mut() = Some(first_node);
}
}
Expand Down
32 changes: 16 additions & 16 deletions crates/core/src/dom/effects.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::dom::Modifier;
use crate::dom::Task;
use crate::dom::Cmd;
use std::future::ready;
use std::future::Future;

Expand All @@ -12,10 +12,10 @@ use std::future::Future;
/// that are sent to the parent Component in response to an event that has been triggerred.
pub struct Effects<MSG, XMSG> {
/// Messages that will be executed locally in the Component
pub local: Vec<Task<MSG>>,
pub local: Vec<Cmd<MSG>>,
/// effects that will be executed on the parent Component which instantiate
/// this component
pub external: Vec<Task<XMSG>>,
pub external: Vec<Cmd<XMSG>>,
pub(crate) modifier: Modifier,
}

Expand All @@ -32,10 +32,10 @@ where
XMSG: 'static,
{
Self {
local: local.into_iter().map(|l| Task::single(ready(l))).collect(),
local: local.into_iter().map(|l| Cmd::single(ready(l))).collect(),
external: external
.into_iter()
.map(|x| Task::single(ready(x)))
.map(|x| Cmd::single(ready(x)))
.collect(),
modifier: Modifier::default(),
}
Expand All @@ -53,16 +53,16 @@ where
XMSG: 'static,
{
Self {
local: local.into_iter().map(Task::single).collect(),
external: external.into_iter().map(Task::single).collect(),
local: local.into_iter().map(Cmd::single).collect(),
external: external.into_iter().map(Cmd::single).collect(),
modifier: Modifier::default(),
}
}

/// Create an Effects with local messages that will be executed on the next update loop on this Component
pub fn with_local(local: impl IntoIterator<Item = MSG>) -> Self {
Self {
local: local.into_iter().map(|l| Task::single(ready(l))).collect(),
local: local.into_iter().map(|l| Cmd::single(ready(l))).collect(),
external: vec![],
modifier: Modifier::default(),
}
Expand All @@ -75,7 +75,7 @@ where
F: Future<Output = MSG> + 'static,
{
Self {
local: local.into_iter().map(Task::single).collect(),
local: local.into_iter().map(Cmd::single).collect(),
external: vec![],
modifier: Modifier::default(),
}
Expand All @@ -90,7 +90,7 @@ where
local: vec![],
external: external
.into_iter()
.map(|x| Task::single(ready(x)))
.map(|x| Cmd::single(ready(x)))
.collect(),
modifier: Modifier::default(),
}
Expand All @@ -105,7 +105,7 @@ where
{
Self {
local: vec![],
external: external.into_iter().map(Task::single).collect(),
external: external.into_iter().map(Cmd::single).collect(),
modifier: Modifier::default(),
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ where
/// Append this msgs to the local effects
pub fn append_local(mut self, local: impl IntoIterator<Item = MSG>) -> Self {
self.local
.extend(local.into_iter().map(|l| Task::single(ready(l))));
.extend(local.into_iter().map(|l| Cmd::single(ready(l))));
self
}

Expand Down Expand Up @@ -238,15 +238,15 @@ where
XMSG: 'static,
{
self.local
.extend(local.into_iter().map(|l| Task::single(ready(l))));
.extend(local.into_iter().map(|l| Cmd::single(ready(l))));
self.external
.extend(external.into_iter().map(|x| Task::single(ready(x))));
.extend(external.into_iter().map(|x| Cmd::single(ready(x))));
self
}
}

impl<MSG, XMSG> From<Task<MSG>> for Effects<MSG, XMSG> {
fn from(task: Task<MSG>) -> Effects<MSG, XMSG> {
impl<MSG, XMSG> From<Cmd<MSG>> for Effects<MSG, XMSG> {
fn from(task: Cmd<MSG>) -> Effects<MSG, XMSG> {
Effects {
local: vec![task],
external: vec![],
Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/dom/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ where

/// execute DOM changes in order to reflect the APP's view into the browser representation
pub fn update_dom(&mut self, modifier: &Modifier) -> Result<(), JsValue> {
log::info!("updating the dom...");
let t1 = now();
// a new view is created due to the app update
let view = self.app_context.view();
Expand Down Expand Up @@ -684,6 +685,8 @@ where

if cmd.modifier.should_update_view {
self.update_dom(&cmd.modifier).expect("must update dom");
}else{
log::warn!("not updating dom...");
}

// Ensure all pending patches are applied before emiting the Cmd from update
Expand Down
28 changes: 14 additions & 14 deletions crates/core/src/dom/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dom::{dom_node::intern, util, window, Task};
use crate::dom::{dom_node::intern, util, window, Cmd};
use futures::channel::mpsc;
use wasm_bindgen::{prelude::*, JsCast};
use web_sys::MouseEvent;
Expand All @@ -8,9 +8,9 @@ use web_sys::MouseEvent;
pub struct Window;

impl Window {
/// Create a recurring Task which will be triggered
/// Create a recurring Cmd which will be triggered
/// everytime the window is resized
pub fn on_resize<F, MSG>(mut cb: F) -> Task<MSG>
pub fn on_resize<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(i32, i32) -> MSG + Clone + 'static,
MSG: 'static,
Expand All @@ -30,11 +30,11 @@ impl Window {
)
.expect("add event callback");

Task::sub(rx, resize_callback)
Cmd::sub(rx, resize_callback)
}

///
pub fn on_mousemove<F, MSG>(mut cb: F) -> Task<MSG>
pub fn on_mousemove<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
MSG: 'static,
Expand All @@ -52,11 +52,11 @@ impl Window {
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Task::sub(rx, mousemove_cb)
Cmd::sub(rx, mousemove_cb)
}

///
pub fn on_mouseup<F, MSG>(mut cb: F) -> Task<MSG>
pub fn on_mouseup<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
MSG: 'static,
Expand All @@ -74,11 +74,11 @@ impl Window {
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Task::sub(rx, mousemove_cb)
Cmd::sub(rx, mousemove_cb)
}

/// do this task at every `ms` interval
pub fn every_interval<F, MSG>(interval_ms: i32, cb: F) -> Task<MSG>
pub fn every_interval<F, MSG>(interval_ms: i32, cb: F) -> Cmd<MSG>
where
F: Fn() -> MSG + 'static,
MSG: 'static,
Expand All @@ -97,23 +97,23 @@ impl Window {
interval_ms,
)
.expect("Unable to start interval");
Task::sub(rx, closure_cb)
Cmd::sub(rx, closure_cb)
}

/// scroll the window to the top of the document
pub fn scroll_to_top<MSG>(msg: MSG) -> Task<MSG>
pub fn scroll_to_top<MSG>(msg: MSG) -> Cmd<MSG>
where
MSG: 'static,
{
use std::future::ready;
Task::single(ready({
Cmd::single(ready({
util::scroll_window_to_top();
msg
}))
}

///
pub fn on_popstate<F, MSG>(mut cb: F) -> Task<MSG>
pub fn on_popstate<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(web_sys::PopStateEvent) -> MSG + 'static,
MSG: 'static,
Expand All @@ -132,6 +132,6 @@ impl Window {
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Task::sub(rx, closure_cb)
Cmd::sub(rx, closure_cb)
}
}
Loading

0 comments on commit 0721ea0

Please sign in to comment.