diff --git a/examples/counter_isomorphic/Makefile.toml b/examples/counter_isomorphic/Makefile.toml index ab9175602..356c805c2 100644 --- a/examples/counter_isomorphic/Makefile.toml +++ b/examples/counter_isomorphic/Makefile.toml @@ -1,3 +1,5 @@ +extend = [{ path = "../cargo-make/common.toml" }] + [tasks.build] command = "cargo" args = ["+nightly", "build-all-features"] diff --git a/examples/counter_isomorphic/src/counters.rs b/examples/counter_isomorphic/src/counters.rs index 69ed1a6fc..7e13a2743 100644 --- a/examples/counter_isomorphic/src/counters.rs +++ b/examples/counter_isomorphic/src/counters.rs @@ -1,27 +1,27 @@ +use cfg_if::cfg_if; use leptos::*; -use leptos_router::*; use leptos_meta::*; +use leptos_router::*; -#[cfg(feature = "ssr")] -use std::sync::atomic::{AtomicI32, Ordering}; +cfg_if! { + if #[cfg(feature = "ssr")] { + use std::sync::atomic::{AtomicI32, Ordering}; + use broadcaster::BroadcastChannel; + static COUNT: AtomicI32 = AtomicI32::new(0); -#[cfg(feature = "ssr")] -use broadcaster::BroadcastChannel; + lazy_static::lazy_static! { + pub static ref COUNT_CHANNEL: BroadcastChannel = BroadcastChannel::new(); + } -#[cfg(feature = "ssr")] -pub fn register_server_functions() { - _ = GetServerCount::register(); - _ = AdjustServerCount::register(); - _ = ClearServerCount::register(); + pub fn register_server_functions() { + _ = GetServerCount::register(); + _ = AdjustServerCount::register(); + _ = ClearServerCount::register(); + } + + } } -#[cfg(feature = "ssr")] -static COUNT: AtomicI32 = AtomicI32::new(0); - -#[cfg(feature = "ssr")] -lazy_static::lazy_static! { - pub static ref COUNT_CHANNEL: BroadcastChannel = BroadcastChannel::new(); -} // "/api" is an optional prefix that allows you to locate server functions wherever you'd like on the server #[server(GetServerCount, "/api")] pub async fn get_server_count() -> Result { @@ -29,7 +29,10 @@ pub async fn get_server_count() -> Result { } #[server(AdjustServerCount, "/api")] -pub async fn adjust_server_count(delta: i32, msg: String) -> Result { +pub async fn adjust_server_count( + delta: i32, + msg: String, +) -> Result { let new = COUNT.load(Ordering::Relaxed) + delta; COUNT.store(new, Ordering::Relaxed); _ = COUNT_CHANNEL.send(&new).await; @@ -46,36 +49,49 @@ pub async fn clear_server_count() -> Result { #[component] pub fn Counters(cx: Scope) -> impl IntoView { provide_meta_context(cx); - view! { - cx, + view! { cx,

"Server-Side Counters"

"Each of these counters stores its data in the same variable on the server."

-

"The value is shared across connections. Try opening this is another browser tab to see what I mean."

+

+ "The value is shared across connections. Try opening this is another browser tab to see what I mean." +

- - }/> - - }/> - - }/> + } + } + /> + } + } + /> + } + } + />
@@ -93,33 +109,47 @@ pub fn Counter(cx: Scope) -> impl IntoView { let clear = create_action(cx, |_| clear_server_count()); let counter = create_resource( cx, - move || (dec.version().get(), inc.version().get(), clear.version().get()), + move || { + ( + dec.version().get(), + inc.version().get(), + clear.version().get(), + ) + }, |_| get_server_count(), ); - let value = move || counter.read(cx).map(|count| count.unwrap_or(0)).unwrap_or(0); - let error_msg = move || { + let value = move || { counter .read(cx) - .map(|res| match res { - Ok(_) => None, - Err(e) => Some(e), - }) - .flatten() + .map(|count| count.unwrap_or(0)) + .unwrap_or(0) + }; + let error_msg = move || { + counter.read(cx).and_then(|res| match res { + Ok(_) => None, + Err(e) => Some(e), + }) }; - view! { - cx, + view! { cx,

"Simple Counter"

-

"This counter sets the value on the server and automatically reloads the new value."

+

+ "This counter sets the value on the server and automatically reloads the new value." +

"Value: " {value} "!"
- {move || error_msg().map(|msg| view! { cx,

"Error: " {msg.to_string()}

})} + {move || { + error_msg() + .map(|msg| { + view! { cx,

"Error: " {msg.to_string()}

} + }) + }}
} } @@ -142,19 +172,15 @@ pub fn FormCounter(cx: Scope) -> impl IntoView { ); let value = move || { log::debug!("FormCounter looking for value"); - counter - .read(cx) - .map(|n| n.ok()) - .flatten() - .map(|n| n) - .unwrap_or(0) + counter.read(cx).and_then(|n| n.ok()).unwrap_or(0) }; - view! { - cx, + view! { cx,

"Form Counter"

-

"This counter uses forms to set the value on the server. When progressively enhanced, it should behave identically to the “Simple Counter.”"

+

+ "This counter uses forms to set the value on the server. When progressively enhanced, it should behave identically to the “Simple Counter.”" +

// calling a server function is the same as POSTing to its API URL // so we can just do that with a form and button @@ -185,26 +211,32 @@ pub fn FormCounter(cx: Scope) -> impl IntoView { // This is the primitive pattern for live chat, collaborative editing, etc. #[component] pub fn MultiuserCounter(cx: Scope) -> impl IntoView { - let dec = create_action(cx, |_| adjust_server_count(-1, "dec dec goose".into())); - let inc = create_action(cx, |_| adjust_server_count(1, "inc inc moose".into())); + let dec = + create_action(cx, |_| adjust_server_count(-1, "dec dec goose".into())); + let inc = + create_action(cx, |_| adjust_server_count(1, "inc inc moose".into())); let clear = create_action(cx, |_| clear_server_count()); #[cfg(not(feature = "ssr"))] let multiplayer_value = { use futures::StreamExt; - let mut source = gloo_net::eventsource::futures::EventSource::new("/api/events") - .expect("couldn't connect to SSE stream"); + let mut source = + gloo_net::eventsource::futures::EventSource::new("/api/events") + .expect("couldn't connect to SSE stream"); let s = create_signal_from_stream( cx, - source.subscribe("message").unwrap().map(|value| { - match value { - Ok(value) => { - value.1.data().as_string().expect("expected string value") - }, + source + .subscribe("message") + .unwrap() + .map(|value| match value { + Ok(value) => value + .1 + .data() + .as_string() + .expect("expected string value"), Err(_) => "0".to_string(), - } - }) + }), ); on_cleanup(cx, move || source.close()); @@ -212,18 +244,20 @@ pub fn MultiuserCounter(cx: Scope) -> impl IntoView { }; #[cfg(feature = "ssr")] - let (multiplayer_value, _) = - create_signal(cx, None::); + let (multiplayer_value, _) = create_signal(cx, None::); - view! { - cx, + view! { cx,

"Multi-User Counter"

-

"This one uses server-sent events (SSE) to live-update when other users make changes."

+

+ "This one uses server-sent events (SSE) to live-update when other users make changes." +

- "Multiplayer Value: " {move || multiplayer_value.get().unwrap_or_default().to_string()} + + "Multiplayer Value: " {move || multiplayer_value.get().unwrap_or_default()} +
diff --git a/examples/counter_isomorphic/src/lib.rs b/examples/counter_isomorphic/src/lib.rs index 675657c7a..535fdb874 100644 --- a/examples/counter_isomorphic/src/lib.rs +++ b/examples/counter_isomorphic/src/lib.rs @@ -1,10 +1,10 @@ use cfg_if::cfg_if; -use leptos::*; pub mod counters; // Needs to be in lib.rs AFAIK because wasm-bindgen needs us to be compiling a lib. I may be wrong. cfg_if! { if #[cfg(feature = "hydrate")] { + use leptos::*; use wasm_bindgen::prelude::wasm_bindgen; use crate::counters::*; diff --git a/examples/counter_isomorphic/src/main.rs b/examples/counter_isomorphic/src/main.rs index 0e66040ee..02a60fcf1 100644 --- a/examples/counter_isomorphic/src/main.rs +++ b/examples/counter_isomorphic/src/main.rs @@ -1,11 +1,11 @@ use cfg_if::cfg_if; -use leptos::*; mod counters; // boilerplate to run in different modes cfg_if! { // server-only stuff if #[cfg(feature = "ssr")] { + use leptos::*; use actix_files::{Files}; use actix_web::*; use crate::counters::*;