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 1, 2024
1 parent f2a8d7b commit a8d711b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/dom/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ impl Window {
}

/// do this task at every `ms` interval
pub fn every_interval<F, MSG>(interval_ms: i32, mut cb: F) -> Task<MSG>
pub fn every_interval<F, MSG>(interval_ms: i32, cb: F) -> Task<MSG>
where
F: FnMut() -> MSG + 'static,
F: Fn() -> MSG + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
Expand Down
27 changes: 13 additions & 14 deletions examples/fetch-data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![deny(warnings)]
use sauron::dom::spawn_local;
use sauron::dom::Http;
use sauron::html::attributes::*;
use sauron::html::events::*;
use sauron::html::*;
use sauron::js_sys::TypeError;
use sauron::{jss, text, wasm_bindgen, Application, Cmd, Node, Program};
use sauron::{jss, text, wasm_bindgen, Application, Node, Program, Task};
use serde::Deserialize;

#[macro_use]
Expand Down Expand Up @@ -56,27 +55,27 @@ impl App {
}
}

fn fetch_page(&self) -> Cmd<Self> {
fn fetch_page(&self) -> Task<Msg> {
let url = format!("{}?page={}&per_page={}", DATA_URL, self.page, PER_PAGE);
Cmd::new(|mut program| {
spawn_local(async move {
Task::single(
async move {
let msg = match Http::fetch_text(&url).await {
Ok(v) => match serde_json::from_str(&v) {
Ok(data1) => Msg::ReceivedData(data1),
Err(err) => Msg::JsonError(err),
},
Err(e) => Msg::RequestError(e),
};
program.dispatch(msg);
})
})
msg
}
)
}
}

impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Self> {
fn init(&mut self) -> Task<Msg> {
console_log::init_with_level(log::Level::Trace).unwrap();
self.fetch_page()
}
Expand Down Expand Up @@ -140,15 +139,15 @@ impl Application for App {
)
}

fn update(&mut self, msg: Msg) -> Cmd<Self> {
fn update(&mut self, msg: Msg) -> Task<Msg> {
trace!("App is updating from msg: {:?}", msg);
match msg {
Msg::NextPage => {
if self.page < self.data.total_pages {
self.page += 1;
self.fetch_page()
} else {
Cmd::none()
Task::none()
}
}
Msg::PrevPage => {
Expand All @@ -159,20 +158,20 @@ impl Application for App {
}
Msg::ReceivedData(data1) => {
self.data = data1;
Cmd::none()
Task::none()
}
Msg::JsonError(err) => {
trace!("Error fetching users! {:#?}", err);
self.error = Some(format!("There was an error fetching the page: {:?}", err));
Cmd::none()
Task::none()
}
Msg::RequestError(type_error) => {
trace!("Error requesting the page: {:?}", type_error);
self.error = Some(format!(
"There was an error fetching the page: {:?}",
type_error
));
Cmd::none()
Task::none()
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions examples/resize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sauron::{html::*, *};

pub enum Msg {
WindowResized(i32, i32),
TickTock,
}

#[derive(Default)]
Expand All @@ -16,10 +17,13 @@ impl Application for App {
type MSG = Msg;

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)
})
Task::batch([
Window::on_resize(|w, h| {
log::info!("This will trigger only once.. {w}x{h}");
Msg::WindowResized(w, h)
}),
Window::every_interval(1_000, || Msg::TickTock),
])
}

fn view(&self) -> Node<Msg> {
Expand Down Expand Up @@ -54,6 +58,10 @@ impl Application for App {
self.height = Some(h);
Task::none()
}
Msg::TickTock => {
log::info!("tick tock!");
Task::none()
}
}
}

Expand Down

0 comments on commit a8d711b

Please sign in to comment.