Skip to content

Commit

Permalink
feat: use Task in Application trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 1, 2024
1 parent 8b1ac1d commit 2b66ce9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 25 deletions.
3 changes: 2 additions & 1 deletion crates/core/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ cfg_if! {if #[cfg(feature = "with-dom")] {
pub use application::{Application, Measurements, SkipDiff, skip_if, skip_diff, SkipPath};
#[cfg(feature = "custom_element")]
pub use component::{register_web_component, WebComponent, WebComponentWrapper};
pub use component::{component, stateful_component, StatefulComponent, StatefulModel, StatelessModel};
pub use component::{stateful_component, StatefulComponent, StatefulModel, StatelessModel};
pub use component::component;
pub use dom_patch::{DomPatch, PatchVariant};
pub use dom_attr::{DomAttr, DomAttrValue, GroupedDomAttrValues};
pub use http::Http;
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::dom::Dispatch;
use crate::vdom::Node;
pub use skip_diff::{skip_if, SkipDiff, SkipPath};
use crate::dom::Task;

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

Expand Down
16 changes: 11 additions & 5 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![allow(unused)]
use crate::dom::events::on_mount;
use crate::dom::program::MountProcedure;
use crate::dom::Application;
use crate::dom::Dispatch;
use crate::dom::Task;
use crate::dom::Component;
use crate::dom::DomAttrValue;
use crate::dom::DomNode;
Expand Down Expand Up @@ -111,19 +112,20 @@ impl<MSG> PartialEq for StatefulModel<MSG> {
}
}

/*
impl<COMP> Application for COMP
where
COMP: Component<XMSG = ()> + StatefulComponent + 'static,
{
type MSG = COMP::MSG;
fn init(&mut self) -> Dispatch<Self> {
Dispatch::from(<Self as Component>::init(self))
fn init(&mut self) -> Cmd<Self> {
Cmd::from(<Self as Component>::init(self))
}
fn update(&mut self, msg: COMP::MSG) -> Dispatch<Self> {
fn update(&mut self, msg: COMP::MSG) -> Cmd<Self> {
let effects = <Self as Component>::update(self, msg);
Dispatch::from(effects)
Cmd::from(effects)
}
fn view(&self) -> Node<COMP::MSG> {
Expand All @@ -138,6 +140,7 @@ where
<Self as Component>::style(self)
}
}
*/

/// create a stateful component node
pub fn stateful_component<COMP, MSG, MSG2>(
Expand All @@ -150,6 +153,7 @@ where
MSG: Default + 'static,
MSG2: 'static,
{
/*
let type_id = TypeId::of::<COMP>();
let attrs = attrs.into_iter().collect::<Vec<_>>();
Expand All @@ -169,6 +173,8 @@ where
attrs: attrs.into_iter().chain([mount_event]).collect(),
children: children.into_iter().collect(),
}))
*/
todo!()
}

impl Into<DomAttrValue> for JsValue {
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/dom/program/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ where
APP: Application,
{
pub fn init_app(&self) -> Dispatch<APP> {
self.app.borrow_mut().init()
Dispatch::from(self.app.borrow_mut().init())
}

pub fn view(&self) -> vdom::Node<APP::MSG> {
Expand Down Expand Up @@ -145,7 +145,7 @@ where
}

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

/// return true if there are still pending msgs
Expand Down
25 changes: 23 additions & 2 deletions crates/core/src/dom/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use futures::StreamExt;
use std::future::Future;
use std::pin::Pin;
use crate::dom::Effects;
use crate::dom::Modifier;

/// encapsulate anything a component can do
pub enum Command<MSG> {
Expand All @@ -19,6 +20,7 @@ pub enum Command<MSG> {
pub struct Task<MSG>{
/// commands
pub(crate) commands: Vec<Command<MSG>>,
pub(crate) modifier: Modifier,
}

impl<MSG> Task<MSG>
Expand All @@ -31,13 +33,15 @@ where
F: Future<Output = MSG> + 'static,
{
Self{
commands: vec![Command::single(f)]
commands: vec![Command::single(f)],
modifier: Default::default(),
}
}
///
pub fn sub(rx: UnboundedReceiver<MSG>, event_closure: EventClosure) -> Self {
Self{
commands: vec![Command::sub(rx, event_closure)],
modifier: Default::default(),
}
}

Expand All @@ -49,6 +53,7 @@ where
{
Task{
commands: self.commands.into_iter().map(|t|t.map_msg(f.clone())).collect(),
modifier: Default::default(),
}
}

Expand All @@ -58,7 +63,22 @@ where
for task in tasks.into_iter(){
commands.extend(task.commands);
}
Self {commands}
Self {commands,
modifier: Default::default(),
}
}

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

///
pub fn no_render(mut self) -> Self {
self.modifier.should_update_view = false;
self
}

}
Expand Down Expand Up @@ -119,6 +139,7 @@ where
Self::Sub(task) => task.next().await,
}
}

}

/// Action is used to do asynchronous operations
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) -> Dispatch<Self> {
Dispatch::from(Window::every_interval(1_000, || Msg::Clock))
fn init(&mut self) -> Task<Msg> {
Window::every_interval(1_000, || Msg::Clock)
}

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

view! {
Expand Down
10 changes: 5 additions & 5 deletions examples/resize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub struct App {
impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Self> {
Cmd::from(Window::on_resize(|w, h| {
fn init(&mut self) -> Task<Msg> {
Window::on_resize(|w, h| {
log::info!("This will trigger only once.. {w}x{h}");
Msg::WindowResized(w, h)
}))
})
}

fn view(&self) -> Node<Msg> {
Expand All @@ -46,13 +46,13 @@ impl Application for App {
)
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Task<Msg> {
match msg {
Msg::WindowResized(w, h) => {
log::info!("Setting the App's width: {w} and height: {h}");
self.width = Some(w);
self.height = Some(h);
Cmd::none()
Task::none()
}
}
}
Expand Down

0 comments on commit 2b66ce9

Please sign in to comment.