diff --git a/crates/core/src/dom/cmd.rs b/crates/core/src/dom/cmd.rs index e7faf989..dc8fd253 100644 --- a/crates/core/src/dom/cmd.rs +++ b/crates/core/src/dom/cmd.rs @@ -28,8 +28,16 @@ impl Cmd where MSG: 'static, { - /// - pub fn single(f: F) -> Self + /// maps to a once future + pub fn new(f: F) -> Self + where + F: Future + 'static, + { + Self::once(f) + } + + /// Creates a Cmd which expects to be polled only once + pub fn once(f: F) -> Self where F: Future + 'static, { @@ -38,8 +46,8 @@ where modifier: Default::default(), } } - /// - pub fn sub(rx: UnboundedReceiver, event_closure: Closure) -> Self { + /// Creates a Cmd which will be polled multiple times + pub fn recurring(rx: UnboundedReceiver, event_closure: Closure) -> Self { Self{ commands: vec![Command::sub(rx, event_closure)], modifier: Default::default(), @@ -210,6 +218,7 @@ where } } +/// Sub is a recurring operation pub struct Sub { pub(crate) receiver: UnboundedReceiver, /// store the associated closures so it is not dropped before being event executed diff --git a/crates/core/src/dom/document.rs b/crates/core/src/dom/document.rs index 977e2c89..e7f0c754 100644 --- a/crates/core/src/dom/document.rs +++ b/crates/core/src/dom/document.rs @@ -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) } } diff --git a/crates/core/src/dom/effects.rs b/crates/core/src/dom/effects.rs index 5a12dee2..6d10a9d4 100644 --- a/crates/core/src/dom/effects.rs +++ b/crates/core/src/dom/effects.rs @@ -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(), } @@ -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) -> 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(), } @@ -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(), } @@ -146,7 +146,7 @@ where /// Append this msgs to the local effects pub fn append_local(mut self, local: impl IntoIterator) -> Self { self.local - .extend(local.into_iter().map(|l| Cmd::single(ready(l)))); + .extend(local.into_iter().map(|l| Cmd::once(ready(l)))); self } @@ -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 } } diff --git a/crates/core/src/dom/window.rs b/crates/core/src/dom/window.rs index 2ca7eaa2..85540702 100644 --- a/crates/core/src/dom/window.rs +++ b/crates/core/src/dom/window.rs @@ -30,7 +30,7 @@ impl Window { ) .expect("add event callback"); - Cmd::sub(rx, resize_callback) + Cmd::recurring(rx, resize_callback) } /// @@ -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) } /// @@ -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) } /// @@ -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) } /// @@ -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) } /// @@ -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) } /// @@ -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 @@ -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 @@ -194,7 +194,7 @@ impl Window { MSG: 'static, { use std::future::ready; - Cmd::single(ready({ + Cmd::once(ready({ util::scroll_window_to_top(); msg })) @@ -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) } } diff --git a/examples/delay/src/lib.rs b/examples/delay/src/lib.rs index 881c76ba..998c239a 100644 --- a/examples/delay/src/lib.rs +++ b/examples/delay/src/lib.rs @@ -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!"); })) } diff --git a/examples/fetch-data-component/src/fetcher.rs b/examples/fetch-data-component/src/fetcher.rs index 1fad1e8a..46efea72 100644 --- a/examples/fetch-data-component/src/fetcher.rs +++ b/examples/fetch-data-component/src/fetcher.rs @@ -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), diff --git a/examples/fetch-data-macro-syntax/src/lib.rs b/examples/fetch-data-macro-syntax/src/lib.rs index 64b54cab..1cdf6d75 100644 --- a/examples/fetch-data-macro-syntax/src/lib.rs +++ b/examples/fetch-data-macro-syntax/src/lib.rs @@ -51,9 +51,7 @@ impl App { fn fetch_page(&self) -> Cmd { 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) { @@ -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), - } - } - ) } } diff --git a/examples/fetch-data/src/lib.rs b/examples/fetch-data/src/lib.rs index 1bfb3979..0ad14634 100644 --- a/examples/fetch-data/src/lib.rs +++ b/examples/fetch-data/src/lib.rs @@ -57,7 +57,7 @@ impl App { fn fetch_page(&self) -> Cmd { 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) { diff --git a/examples/interactive/src/app.rs b/examples/interactive/src/app.rs index d8fbf51c..4f8fad0f 100644 --- a/examples/interactive/src/app.rs +++ b/examples/interactive/src/app.rs @@ -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 { diff --git a/examples/now-you-see-me/src/lib.rs b/examples/now-you-see-me/src/lib.rs index c4882108..f6de7817 100644 --- a/examples/now-you-see-me/src/lib.rs +++ b/examples/now-you-see-me/src/lib.rs @@ -21,7 +21,7 @@ impl Application for App { type MSG = Msg; fn init(&mut self) -> Cmd { - Cmd::single( async move{ + Cmd::new( async move{ Msg::ToggleShow }) } @@ -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