Skip to content

Commit

Permalink
refactor: **breaking** rename single to once, make new map to one. re…
Browse files Browse the repository at this point in the history
…name sub to recurring
  • Loading branch information
ivanceras committed Apr 2, 2024
1 parent 8e96fe0 commit e0547d4
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 45 deletions.
17 changes: 13 additions & 4 deletions crates/core/src/dom/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ impl<MSG> Cmd<MSG>
where
MSG: 'static,
{
///
pub fn single<F>(f: F) -> Self
/// maps to a once future
pub fn new<F>(f: F) -> Self
where
F: Future<Output = MSG> + 'static,
{
Self::once(f)
}

/// Creates a Cmd which expects to be polled only once
pub fn once<F>(f: F) -> Self
where
F: Future<Output = MSG> + 'static,
{
Expand All @@ -38,8 +46,8 @@ where
modifier: Default::default(),
}
}
///
pub fn sub(rx: UnboundedReceiver<MSG>, event_closure: Closure<dyn FnMut(web_sys::Event)>) -> Self {
/// Creates a Cmd which will be polled multiple times
pub fn recurring(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 @@ -210,6 +218,7 @@ where
}
}

/// Sub is a recurring operation
pub struct Sub<MSG> {
pub(crate) receiver: UnboundedReceiver<MSG>,
/// store the associated closures so it is not dropped before being event executed
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/dom/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ impl Document {
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, closure_cb)
Cmd::recurring(rx, closure_cb)
}
}
14 changes: 7 additions & 7 deletions crates/core/src/dom/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ where
XMSG: 'static,
{
Self {
local: local.into_iter().map(|l| Cmd::single(ready(l))).collect(),
local: local.into_iter().map(|l| Cmd::once(ready(l))).collect(),
external: external
.into_iter()
.map(|x| Cmd::single(ready(x)))
.map(|x| Cmd::once(ready(x)))
.collect(),
modifier: Modifier::default(),
}
Expand All @@ -44,7 +44,7 @@ where
/// Create an Effects with local messages that will be executed on the next update loop on this Component
pub fn with_local(local: impl IntoIterator<Item = MSG>) -> Self {
Self {
local: local.into_iter().map(|l| Cmd::single(ready(l))).collect(),
local: local.into_iter().map(|l| Cmd::once(ready(l))).collect(),
external: vec![],
modifier: Modifier::default(),
}
Expand All @@ -60,7 +60,7 @@ where
local: vec![],
external: external
.into_iter()
.map(|x| Cmd::single(ready(x)))
.map(|x| Cmd::once(ready(x)))
.collect(),
modifier: Modifier::default(),
}
Expand Down Expand Up @@ -146,7 +146,7 @@ where
/// Append this msgs to the local effects
pub fn append_local(mut self, local: impl IntoIterator<Item = MSG>) -> Self {
self.local
.extend(local.into_iter().map(|l| Cmd::single(ready(l))));
.extend(local.into_iter().map(|l| Cmd::once(ready(l))));
self
}

Expand Down Expand Up @@ -195,9 +195,9 @@ where
XMSG: 'static,
{
self.local
.extend(local.into_iter().map(|l| Cmd::single(ready(l))));
.extend(local.into_iter().map(|l| Cmd::once(ready(l))));
self.external
.extend(external.into_iter().map(|x| Cmd::single(ready(x))));
.extend(external.into_iter().map(|x| Cmd::once(ready(x))));
self
}
}
Expand Down
20 changes: 10 additions & 10 deletions crates/core/src/dom/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Window {
)
.expect("add event callback");

Cmd::sub(rx, resize_callback)
Cmd::recurring(rx, resize_callback)
}

///
Expand All @@ -52,7 +52,7 @@ impl Window {
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, mousemove_cb)
Cmd::recurring(rx, mousemove_cb)
}

///
Expand All @@ -74,7 +74,7 @@ impl Window {
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, mousemove_cb)
Cmd::recurring(rx, mousemove_cb)
}

///
Expand All @@ -96,7 +96,7 @@ impl Window {
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, mousemove_cb)
Cmd::recurring(rx, mousemove_cb)
}

///
Expand All @@ -118,7 +118,7 @@ impl Window {
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, mousemove_cb)
Cmd::recurring(rx, mousemove_cb)
}

///
Expand All @@ -140,7 +140,7 @@ impl Window {
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, closure_cb)
Cmd::recurring(rx, closure_cb)
}

///
Expand All @@ -162,7 +162,7 @@ impl Window {
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, closure_cb)
Cmd::recurring(rx, closure_cb)
}

/// do this task at every `ms` interval
Expand All @@ -185,7 +185,7 @@ impl Window {
interval_ms,
)
.expect("Unable to start interval");
Cmd::sub(rx, closure_cb)
Cmd::recurring(rx, closure_cb)
}

/// scroll the window to the top of the document
Expand All @@ -194,7 +194,7 @@ impl Window {
MSG: 'static,
{
use std::future::ready;
Cmd::single(ready({
Cmd::once(ready({
util::scroll_window_to_top();
msg
}))
Expand All @@ -220,6 +220,6 @@ impl Window {
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, closure_cb)
Cmd::recurring(rx, closure_cb)
}
}
2 changes: 1 addition & 1 deletion examples/delay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Application for App {
.expect("must have a handle");

*current_handle.borrow_mut() = Some(handle);
Cmd::sub(rx, sauron::Closure::new(|_:sauron::web_sys::Event|{
Cmd::recurring(rx, sauron::Closure::new(|_:sauron::web_sys::Event|{
panic!("This is not called!");
}))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/fetch-data-component/src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Fetcher {
let url = format!("{}?page={}&per_page={}", DATA_URL, self.page, PER_PAGE);
log::info!("url: {}", url);

Effects::from(Cmd::single(async move {
Effects::from(Cmd::new(async move {
match Http::fetch_text(&url).await {
Ok(v) => match serde_json::from_str(&v) {
Ok(data1) => Msg::ReceivedData(data1),
Expand Down
18 changes: 1 addition & 17 deletions examples/fetch-data-macro-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ impl App {

fn fetch_page(&self) -> Cmd<Msg> {
let url = format!("{}?page={}&per_page={}", DATA_URL, self.page, PER_PAGE);

/*
Cmd::single(
Cmd::new(
async move {
let msg = match Http::fetch_text(&url).await {
Ok(v) => match serde_json::from_str(&v) {
Expand All @@ -65,20 +63,6 @@ impl App {
msg
}
)
*/
Cmd::single(
async move{
let response = reqwest::Client::new()
.get(url)
.send()
.await.expect("dont error pls");

match serde_json::from_str(&response.text().await.unwrap()) {
Ok(data1) => Msg::ReceivedData(data1),
Err(err) => Msg::JsonError(err),
}
}
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/fetch-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl App {

fn fetch_page(&self) -> Cmd<Msg> {
let url = format!("{}?page={}&per_page={}", DATA_URL, self.page, PER_PAGE);
Cmd::single(
Cmd::new(
async move {
let msg = match Http::fetch_text(&url).await {
Ok(v) => match serde_json::from_str(&v) {
Expand Down
2 changes: 1 addition & 1 deletion examples/interactive/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Application for App {
1000,
)
.expect("Unable to start interval");
Cmd::sub(rx, clock_cb)
Cmd::recurring(rx, clock_cb)
}

fn update(&mut self, msg: Msg) -> Cmd<Msg> {
Expand Down
4 changes: 2 additions & 2 deletions examples/now-you-see-me/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Application for App {
type MSG = Msg;

fn init(&mut self) -> Cmd<Msg> {
Cmd::single( async move{
Cmd::new( async move{
Msg::ToggleShow
})
}
Expand All @@ -34,7 +34,7 @@ impl Application for App {
} else {
document().set_title("Now, you don't!");
}
Cmd::single(
Cmd::new(
async move {
delay(2000).await;
Msg::ToggleShow
Expand Down

0 comments on commit e0547d4

Please sign in to comment.