Skip to content

Commit

Permalink
refactor: move Time functions into a time module
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 3, 2024
1 parent 08e8020 commit 4919401
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 29 deletions.
2 changes: 2 additions & 0 deletions crates/core/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cfg_if! {if #[cfg(feature = "with-dom")] {
pub use window::Window;
pub use dom_node::DomNode;
pub use document::Document;
pub use time::Time;

mod application;
pub mod dispatch;
Expand All @@ -49,6 +50,7 @@ cfg_if! {if #[cfg(feature = "with-dom")] {
mod ric;
mod window;
mod document;
mod time;
mod timeout;


Expand Down
32 changes: 32 additions & 0 deletions crates/core/src/dom/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::dom::{window, Cmd};
use futures::channel::mpsc;
use wasm_bindgen::{prelude::*, JsCast};

/// Provides function related to Time
#[derive(Clone, Copy)]
pub struct Time;

impl Time {

/// do this task at every `ms` interval
pub fn every<F, MSG>(interval_ms: i32, cb: F) -> Cmd<MSG>
where
F: Fn() -> MSG + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
//The web_sys::Event here is undefined, it is just used here to make storing the closure
//uniform
let closure_cb: Closure<dyn FnMut(web_sys::Event)> = Closure::new(move |_event| {
let msg = cb();
tx.start_send(msg).unwrap();
});
window()
.set_interval_with_callback_and_timeout_and_arguments_0(
closure_cb.as_ref().unchecked_ref(),
interval_ms,
)
.expect("Unable to start interval");
Cmd::recurring(rx, closure_cb)
}
}
22 changes: 0 additions & 22 deletions crates/core/src/dom/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,6 @@ impl Window {
Cmd::recurring(rx, closure_cb)
}

/// do this task at every `ms` interval
pub fn every_interval<F, MSG>(interval_ms: i32, cb: F) -> Cmd<MSG>
where
F: Fn() -> MSG + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
//The web_sys::Event here is undefined, it is just used here to make storing the closure
//uniform
let closure_cb: Closure<dyn FnMut(web_sys::Event)> = Closure::new(move |_event| {
log::info!("event: {:?}", _event);
let msg = cb();
tx.start_send(msg).unwrap();
});
window()
.set_interval_with_callback_and_timeout_and_arguments_0(
closure_cb.as_ref().unchecked_ref(),
interval_ms,
)
.expect("Unable to start interval");
Cmd::recurring(rx, closure_cb)
}

/// scroll the window to the top of the document
pub fn scroll_to_top<MSG>(msg: MSG) -> Cmd<MSG>
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub mod prelude {
pub use crate::dom::{Application, SkipDiff, skip_if, events, Program, document, Document, now, window, Window, Dispatch,
AnimationFrameHandle, Component, StatefulComponent, Effects, Measurements, MountAction,
MountTarget, Cmd, TimeoutCallbackHandle, DomAttrValue,
stateful_component,
stateful_component, Time,
};
#[cfg(feature = "custom_element")]
pub use crate::dom::WebComponent;
Expand Down
2 changes: 1 addition & 1 deletion examples/experimentals/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Application for App {
type MSG = Msg;

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

fn update(&mut self, msg: Msg) -> Cmd<Msg> {
Expand Down
2 changes: 1 addition & 1 deletion examples/fragments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Msg> {
Window::every_interval(1000, ||Msg::AddItem)
Time::every(1000, ||Msg::AddItem)
}

fn update(&mut self, msg: Msg) -> Cmd<Msg>
Expand Down
2 changes: 1 addition & 1 deletion examples/interactive-macro-syntax/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Application for App {
type MSG = Msg;

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

fn update(&mut self, msg: Msg) -> Cmd<Msg> {
Expand Down
2 changes: 1 addition & 1 deletion examples/resize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Application for App {
log::info!("This will trigger only once.. {w}x{h}");
Msg::WindowResized(w, h)
}),
Window::every_interval(1_000, || Msg::TickTock),
Time::every(1_000, || Msg::TickTock),
])
}

Expand Down
2 changes: 1 addition & 1 deletion examples/svg-clock-macro-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Application for Clock {
// we wire the window set_interval api to trigger an Msg::Tick
// by dispatching it from the program, through the Cmd interface
fn init(&mut self) -> Cmd<Msg> {
Window::every_interval(1_000/60, ||Msg::Tick)
Time::every(1_000/60, ||Msg::Tick)
}

fn update(&mut self, msg: Msg) -> Cmd<Msg> {
Expand Down
2 changes: 1 addition & 1 deletion examples/svg-clock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Application for Clock {
// we wire the window set_interval api to trigger an Msg::Tick
// by dispatching it from the program, through the Cmd interface
fn init(&mut self) -> Cmd<Msg> {
Window::every_interval(17, ||Msg::Tick)
Time::every(17, ||Msg::Tick)
}

fn update(&mut self, msg: Msg) -> Cmd<Msg> {
Expand Down

0 comments on commit 4919401

Please sign in to comment.