Skip to content

Commit

Permalink
fix: more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 2, 2024
1 parent 2674513 commit 344d47a
Show file tree
Hide file tree
Showing 25 changed files with 86 additions and 94 deletions.
11 changes: 7 additions & 4 deletions crates/core/src/dom/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::dom::dom_node::EventClosure;
use crate::dom::spawn_local;
use futures::channel::mpsc;
use futures::channel::mpsc::UnboundedReceiver;
Expand All @@ -7,6 +6,7 @@ use std::future::Future;
use std::pin::Pin;
use crate::dom::Effects;
use crate::dom::Modifier;
use wasm_bindgen::closure::Closure;

/// encapsulate anything a component can do
pub enum Command<MSG> {
Expand Down Expand Up @@ -38,7 +38,7 @@ where
}
}
///
pub fn sub(rx: UnboundedReceiver<MSG>, event_closure: EventClosure) -> Self {
pub fn sub(rx: UnboundedReceiver<MSG>, event_closure: Closure<dyn FnMut(web_sys::Event)>) -> Self {
Self{
commands: vec![Command::sub(rx, event_closure)],
modifier: Default::default(),
Expand Down Expand Up @@ -134,7 +134,7 @@ where
Self::Action(Action::new(f))
}
///
pub fn sub(rx: UnboundedReceiver<MSG>, event_closure: EventClosure) -> Self {
pub fn sub(rx: UnboundedReceiver<MSG>, event_closure: Closure<dyn FnMut(web_sys::Event)>) -> Self {
Self::Sub(Sub{
receiver: rx,
event_closure,
Expand All @@ -155,6 +155,7 @@ where

/// return the next value
pub async fn next(&mut self) -> Option<MSG> {
log::info!("Calling on next..");
match self {
Self::Action(task) => task.next().await,
Self::Sub(task) => task.next().await,
Expand Down Expand Up @@ -203,6 +204,7 @@ where

/// get the next value
async fn next(&mut self) -> Option<MSG> {
log::info!("it is in Action");
// return None is already done since awaiting it again is an error
if self.done {
None
Expand All @@ -228,14 +230,15 @@ where
pub struct Sub<MSG> {
pub(crate) receiver: UnboundedReceiver<MSG>,
/// store the associated closures so it is not dropped before being event executed
pub(crate) event_closure: EventClosure,
pub(crate) event_closure: Closure<dyn FnMut(web_sys::Event)>,
}

impl<MSG> Sub<MSG>
where
MSG: 'static,
{
async fn next(&mut self) -> Option<MSG> {
log::info!("It is in Sub..");
self.receiver.next().await
}

Expand Down
6 changes: 6 additions & 0 deletions crates/core/src/dom/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,18 @@ where
APP: Application,
{
fn from(task: Cmd<APP::MSG>) -> Self {
log::info!("converting cmd into dispatch...");
Dispatch::new(move |program| {
log::info!("There are {}", task.commands.len());
for mut command in task.commands.into_iter(){
log::info!("spawning 1 command..");
let program = program.downgrade();
spawn_local(async move {
log::info!("spawned here..");
let mut program = program.upgrade().expect("upgrade");
log::info!("program upgraded...");
while let Some(msg) = command.next().await {
log::info!("waiting...");
program.dispatch(msg)
}
});
Expand Down
11 changes: 11 additions & 0 deletions crates/core/src/dom/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ impl DomNode {
children.borrow_mut().push(child);
}
}
DomInner::Fragment {
fragment, children, ..
} => {
for mut child in for_append.into_iter() {
fragment
.append_child(&child.as_node())
.expect("append child");
child.parent = Rc::new(Some(self.clone()));
children.borrow_mut().push(child);
}
}
_ => unreachable!(
"appending should only be called to Element and Fragment, found: {:#?}",
self
Expand Down
2 changes: 1 addition & 1 deletion examples/arc-reactor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct App;
impl Application for App {
type MSG = Msg;

fn update(&mut self, _msg: Msg) -> Cmd<Self> {
fn update(&mut self, _msg: Msg) -> Cmd<Msg> {
Cmd::none()
}

Expand Down
2 changes: 1 addition & 1 deletion examples/counter-skip_diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl App {
impl Application for App {
type MSG = Msg;

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Increment => self.count += 1,
Msg::Decrement => self.count -= 1,
Expand Down
6 changes: 3 additions & 3 deletions examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sauron::{
html::text, html::units::px, jss, node, wasm_bindgen, Application, Node, Program,
Task,
Cmd,
};

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

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

fn stylesheet() -> Vec<String> {
Expand Down
2 changes: 1 addition & 1 deletion examples/csr-tailwind-trunk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Application for App {
}
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Increment => self.count += 1,
Msg::Decrement => self.count -= 1,
Expand Down
12 changes: 6 additions & 6 deletions examples/data-viewer/src/views/resize_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ impl Application for ResizeWrapper {
/// Unliked when listen by the this view container, which the mouse
/// can be outside of this view, which causes the mousmove event
/// not being triggered
fn init(&mut self) -> Cmd<Self> {
fn init(&mut self) -> Cmd<Msg> {
Cmd::batch([
Cmd::from(Window::on_mouseup(|event| {
Window::on_mouseup(|event| {
Msg::EndResize(event.client_x(), event.client_y())
})),
Cmd::from(Window::on_mousemove(|event| {
}),
Window::on_mousemove(|event| {
Msg::MouseMove(event.client_x(), event.client_y())
})),
}),
Cmd::from(self.data_view.init().map_msg(Msg::DataViewMsg)),
])
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::DataViewMsg(data_view_msg) => {
let effects = self.data_view.update(data_view_msg);
Expand Down
2 changes: 1 addition & 1 deletion examples/delay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Application for App {
)
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Click => {
spawn_local(some_async_function());
Expand Down
4 changes: 2 additions & 2 deletions examples/experimentals/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl App {
impl Application for App {
type MSG = Msg;

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

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Click => {
self.click_count += 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/fancy-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Application for App {
}
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Clicked => {
log::info!("Button has been clicked...");
Expand Down
1 change: 1 addition & 0 deletions examples/fragments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ sauron = { path = "../../"}
console_error_panic_hook = "0.1"
log = "0.4"
console_log = { version = "0.2", features = ["color"] }
futures = "=0.3.30"
18 changes: 7 additions & 11 deletions examples/fragments/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use sauron::dom::{delay, spawn_local};
use sauron::{html::fragment, *};
use futures::channel::mpsc;
use crate::html::node_list;

#[wasm_bindgen(start)]
pub fn start() {
Expand All @@ -20,17 +22,11 @@ struct App {
impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Self> {
Cmd::new(|mut program| {
spawn_local(async move {
loop {
delay(1000).await;
program.dispatch(Msg::AddItem);
}
})
})
fn init(&mut self) -> Cmd<Msg> {
Window::every_interval(1000, ||Msg::AddItem)
}
fn update(&mut self, msg: Msg) -> Cmd<Self>

fn update(&mut self, msg: Msg) -> Cmd<Msg>
where
Self: Sized + 'static,
{
Expand All @@ -46,7 +42,7 @@ impl Application for App {
fn view(&self) -> Node<Msg> {
node! {
<div>
{fragment(self.items.iter().cloned().chain([node! {<span />}]))}
{node_list(self.items.iter().cloned().chain([node! {<span />}]))}
</div>
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Application for App {
}
}

fn update(&mut self, _msg: ()) -> Cmd<Self> {
fn update(&mut self, _msg: ()) -> Cmd<()> {
Cmd::none()
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/interactive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ js-sys = "0.3"
sauron = { path = "../../", features= ["log-patches"] }
log = "0.4"
console_log = "0.2"
futures = "=0.3.30"
28 changes: 14 additions & 14 deletions examples/interactive/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(warnings)]
use js_sys::Date;
use sauron::{html::attributes::*, html::events::*, html::*, jss, web_sys::MouseEvent, *};
use futures::channel::mpsc;

pub enum Msg {
Click,
Expand Down Expand Up @@ -36,22 +37,21 @@ impl App {
impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Self> {
Cmd::new(|mut program| {
let clock: Closure<dyn FnMut()> = Closure::new(move || {
program.dispatch(Msg::Clock);
});
window()
.set_interval_with_callback_and_timeout_and_arguments_0(
clock.as_ref().unchecked_ref(),
1000,
)
.expect("Unable to start interval");
clock.forget();
})
fn init(&mut self) -> Cmd<Msg> {
let (mut tx, rx) = mpsc::unbounded();
let clock_cb: Closure<dyn FnMut(web_sys::Event)> = Closure::new(move |_| {
tx.start_send(Msg::Clock).expect("send");
});
window()
.set_interval_with_callback_and_timeout_and_arguments_0(
clock_cb.as_ref().unchecked_ref(),
1000,
)
.expect("Unable to start interval");
Cmd::sub(rx, clock_cb)
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Click => {
self.click_count += 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-macro-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Application for App {
}
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
log::trace!("App is updating with msg: {:?}", msg);
match msg {
Msg::Click => self.click_count += 1,
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Application for App {
)
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Click => self.click_count += 1,
Msg::NoOp => (),
Expand Down
18 changes: 9 additions & 9 deletions examples/now-you-see-me/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ struct App {
impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Self> {
Cmd::new(|mut program| {
program.dispatch(Msg::ToggleShow);
fn init(&mut self) -> Cmd<Msg> {
Cmd::single( async move{
Msg::ToggleShow
})
}
fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::ToggleShow => {
self.show = !self.show;
Expand All @@ -34,12 +34,12 @@ impl Application for App {
} else {
document().set_title("Now, you don't!");
}
Cmd::new(|mut program| {
spawn_local(async move {
Cmd::single(
async move {
delay(2000).await;
program.dispatch(Msg::ToggleShow);
})
})
Msg::ToggleShow
}
)
}
}
}
Expand Down
Loading

0 comments on commit 344d47a

Please sign in to comment.