Skip to content

Commit

Permalink
refactor: **breaking** rename Cmd to Dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 1, 2024
1 parent 65d29f0 commit 103c07b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ cfg_if! {if #[cfg(feature = "with-dom")] {
pub use raf::{request_animation_frame, AnimationFrameHandle};
pub use ric::{request_idle_callback, IdleCallbackHandle, IdleDeadline};
pub use timeout::{delay, request_timeout_callback, TimeoutCallbackHandle};
pub use cmd::Cmd;
pub use dispatch::Dispatch;
use crate::dom::events::MountEvent;
pub use window::Window;
pub use dom_node::DomNode;

mod application;
pub mod cmd;
pub mod dispatch;
mod dom_node;
mod dom_patch;
mod dom_attr;
Expand Down
14 changes: 7 additions & 7 deletions crates/core/src/dom/application.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dom::Cmd;
use crate::dom::Dispatch;
use crate::vdom::Node;
pub use skip_diff::{skip_if, SkipDiff, SkipPath};

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) -> Cmd<Self> {
Cmd::none()
fn init(&mut self) -> Dispatch<Self> {
Dispatch::none()
}

/// Update the component with a message.
/// The update function returns a Cmd, which can be executed by the runtime.
/// 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) -> Cmd<Self>;
fn update(&mut self, _msg: Self::MSG) -> Dispatch<Self>;

/// 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) -> Cmd<Self> {
fn measurements(&self, measurements: Measurements) -> Dispatch<Self> {
log::debug!("Measurements: {:#?}", measurements);
Cmd::none().no_render()
Dispatch::none().no_render()
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::dom::events::on_mount;
use crate::dom::program::MountProcedure;
use crate::dom::Application;
use crate::dom::Cmd;
use crate::dom::Dispatch;
use crate::dom::Component;
use crate::dom::DomAttrValue;
use crate::dom::DomNode;
Expand Down Expand Up @@ -117,13 +117,13 @@ where
{
type MSG = COMP::MSG;

fn init(&mut self) -> Cmd<Self> {
Cmd::from(<Self as Component>::init(self))
fn init(&mut self) -> Dispatch<Self> {
Dispatch::from(<Self as Component>::init(self))
}

fn update(&mut self, msg: COMP::MSG) -> Cmd<Self> {
fn update(&mut self, msg: COMP::MSG) -> Dispatch<Self> {
let effects = <Self as Component>::update(self, msg);
Cmd::from(effects)
Dispatch::from(effects)
}

fn view(&self) -> Node<COMP::MSG> {
Expand Down
34 changes: 17 additions & 17 deletions crates/core/src/dom/cmd.rs → crates/core/src/dom/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ use crate::dom::Program;
use crate::dom::{Application, Effects, Modifier, Task};
use wasm_bindgen_futures::spawn_local;

/// Cmd is a command to be executed by the system.
/// Dispatch is a command to be executed by the system.
/// This is returned at the init function of a component and is executed right
/// after instantiation of that component.
/// Cmd required a DSP object which is the Program as an argument
/// Dispatch required a DSP object which is the Program as an argument
/// The emit function is called with the program argument.
/// The callback is supplied with the program an is then executed/emitted.
pub struct Cmd<APP>
pub struct Dispatch<APP>
where
APP: Application,
{
/// the functions that would be executed when this Cmd is emited
/// the functions that would be executed when this Dispatch is emited
#[allow(clippy::type_complexity)]
pub(crate) commands: Vec<Box<dyn FnOnce(Program<APP>)>>,
pub(crate) modifier: Modifier,
}

impl<APP> Cmd<APP>
impl<APP> Dispatch<APP>
where
APP: Application,
{
/// creates a new Cmd from a function
/// creates a new Dispatch from a function
pub fn new<F>(f: F) -> Self
where
F: FnOnce(Program<APP>) + 'static,
Expand Down Expand Up @@ -63,7 +63,7 @@ where

/// Tell the runtime that there are no commands.
pub fn none() -> Self {
Cmd {
Dispatch {
commands: vec![],
modifier: Default::default(),
}
Expand All @@ -74,7 +74,7 @@ where
self.commands.is_empty()
}

/// Modify the Cmd such that whether or not it will update the view set by `should_update_view`
/// Modify the Dispatch such that whether or not it will update the view set by `should_update_view`
/// when the cmd is executed in the program
pub fn should_update_view(mut self, should_update_view: bool) -> Self {
self.modifier.should_update_view = should_update_view;
Expand All @@ -93,14 +93,14 @@ where
self
}

/// Modify the Cmd such that it will log a measuregment when it is executed
/// Modify the Dispatch such that it will log a measuregment when it is executed
/// The `measurement_name` is set to distinguish the measurements from each other.
pub fn measure_with_name(mut self, name: &str) -> Self {
self.modifier.measurement_name = name.to_string();
self.measure()
}

/// Executes the Cmd
/// Executes the Dispatch
pub(crate) fn emit(self, program: Program<APP>) {
for cb in self.commands {
let program_clone = program.clone();
Expand All @@ -113,13 +113,13 @@ where
///
pub fn batch_msg(msg_list: impl IntoIterator<Item = APP::MSG>) -> Self {
let msg_list: Vec<APP::MSG> = msg_list.into_iter().collect();
Cmd::new(move |mut program| {
Dispatch::new(move |mut program| {
program.dispatch_multiple(msg_list);
})
}
}

impl<APP> From<Effects<APP::MSG, ()>> for Cmd<APP>
impl<APP> From<Effects<APP::MSG, ()>> for Dispatch<APP>
where
APP: Application,
{
Expand All @@ -133,28 +133,28 @@ where
modifier,
} = effects;

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

impl<APP, IN> From<IN> for Cmd<APP>
impl<APP, IN> From<IN> for Dispatch<APP>
where
APP: Application,
IN: IntoIterator<Item = Effects<APP::MSG, ()>>,
{
fn from(effects: IN) -> Self {
Cmd::batch(effects.into_iter().map(Cmd::from))
Dispatch::batch(effects.into_iter().map(Dispatch::from))
}
}

impl<APP> From<Task<APP::MSG>> for Cmd<APP>
impl<APP> From<Task<APP::MSG>> for Dispatch<APP>
where
APP: Application,
{
fn from(mut task: Task<APP::MSG>) -> Self {
Cmd::new(move |mut program| {
Dispatch::new(move |mut program| {
spawn_local(async move {
while let Some(msg) = task.next().await {
program.dispatch(msg)
Expand Down
16 changes: 8 additions & 8 deletions crates/core/src/dom/program/app_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "with-measure")]
use crate::dom::Measurements;
use crate::dom::{Application, Cmd};
use crate::dom::{Application, Dispatch};
use crate::vdom;
use std::{
cell::{Ref, RefCell},
Expand Down Expand Up @@ -29,7 +29,7 @@ where
pub(crate) pending_msgs: Rc<RefCell<VecDeque<APP::MSG>>>,

/// pending cmds that hasn't been emited yet
pub(crate) pending_cmds: Rc<RefCell<VecDeque<Cmd<APP>>>>,
pub(crate) pending_cmds: Rc<RefCell<VecDeque<Dispatch<APP>>>>,
}

pub(crate) struct WeakContext<APP>
Expand All @@ -39,7 +39,7 @@ where
pub(crate) app: Weak<RefCell<APP>>,
pub(crate) current_vdom: Weak<RefCell<vdom::Node<APP::MSG>>>,
pub(crate) pending_msgs: Weak<RefCell<VecDeque<APP::MSG>>>,
pub(crate) pending_cmds: Weak<RefCell<VecDeque<Cmd<APP>>>>,
pub(crate) pending_cmds: Weak<RefCell<VecDeque<Dispatch<APP>>>>,
}

impl<APP> WeakContext<APP>
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<APP> AppContext<APP>
where
APP: Application,
{
pub fn init_app(&self) -> Cmd<APP> {
pub fn init_app(&self) -> Dispatch<APP> {
self.app.borrow_mut().init()
}

Expand All @@ -136,15 +136,15 @@ where
}

#[cfg(feature = "with-measure")]
pub fn measurements(&self, measurements: Measurements) -> Cmd<APP> {
pub fn measurements(&self, measurements: Measurements) -> Dispatch<APP> {
self.app.borrow().measurements(measurements).no_render()
}

pub fn push_msgs(&mut self, msgs: impl IntoIterator<Item = APP::MSG>) {
self.pending_msgs.borrow_mut().extend(msgs);
}

pub fn update_app(&mut self, msg: APP::MSG) -> Cmd<APP> {
pub fn update_app(&mut self, msg: APP::MSG) -> Dispatch<APP> {
self.app.borrow_mut().update(msg)
}

Expand Down Expand Up @@ -181,7 +181,7 @@ where
}
}

pub fn batch_pending_cmds(&mut self) -> Cmd<APP> {
Cmd::batch(self.pending_cmds.borrow_mut().drain(..))
pub fn batch_pending_cmds(&mut self) -> Dispatch<APP> {
Dispatch::batch(self.pending_cmds.borrow_mut().drain(..))
}
}
2 changes: 1 addition & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub mod prelude {
pub use wasm_bindgen::prelude::*;
pub use serde_wasm_bindgen;
pub use crate::html::events::*;
pub use crate::dom::{Application, SkipDiff, skip_if, events, Program, document, now, window, Window, Cmd,
pub use crate::dom::{Application, SkipDiff, skip_if, events, Program, document, now, window, Window, Dispatch,
AnimationFrameHandle, Component, StatefulComponent, Effects, Measurements, MountAction,
MountTarget, Task, TimeoutCallbackHandle, DomAttrValue,
stateful_component,
Expand Down
6 changes: 3 additions & 3 deletions examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sauron::{
html::text, html::units::px, jss, node, wasm_bindgen, Application, Cmd, Node, Program,
html::text, html::units::px, jss, node, wasm_bindgen, Application, Dispatch, Node, Program,
};

enum Msg {
Expand Down Expand Up @@ -41,13 +41,13 @@ impl Application for App {
}
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Dispatch<Self> {
match msg {
Msg::Increment => self.count += 1,
Msg::Decrement => self.count -= 1,
Msg::Reset => self.count = 0,
}
Cmd::none()
Dispatch::none()
}

fn stylesheet() -> Vec<String> {
Expand Down
8 changes: 4 additions & 4 deletions examples/interactive-macro-syntax/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ impl App {
impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Self> {
Cmd::from(Window::every_interval(1_000, || Msg::Clock))
fn init(&mut self) -> Dispatch<Self> {
Dispatch::from(Window::every_interval(1_000, || Msg::Clock))
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Dispatch<Self> {
match msg {
Msg::Click => {
self.click_count += 1;
Expand All @@ -66,7 +66,7 @@ impl Application for App {
}
}
}
Cmd::none()
Dispatch::none()
}

view! {
Expand Down

0 comments on commit 103c07b

Please sign in to comment.