From fdd576535a255562e6138a1aebd6dcaf9515cee6 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Fri, 19 Jan 2024 15:14:39 -0500 Subject: [PATCH] clean up examples --- .../action-form-error-handling/src/app.rs | 11 +- .../action-form-error-handling/src/main.rs | 4 +- examples/counter_isomorphic/src/counters.rs | 13 +-- examples/counter_isomorphic/src/main.rs | 100 +++++++++--------- 4 files changed, 66 insertions(+), 62 deletions(-) diff --git a/examples/action-form-error-handling/src/app.rs b/examples/action-form-error-handling/src/app.rs index 6989756fc..cba24375f 100644 --- a/examples/action-form-error-handling/src/app.rs +++ b/examples/action-form-error-handling/src/app.rs @@ -28,7 +28,9 @@ pub fn App() -> impl IntoView { } #[server] -async fn do_something(should_error: Option) -> Result { +async fn do_something( + should_error: Option, +) -> Result { if should_error.is_none() { Ok(String::from("Successful submit")) } else { @@ -42,7 +44,12 @@ async fn do_something(should_error: Option) -> Result impl IntoView { let do_something_action = Action::::server(); - let value = Signal::derive(move || do_something_action.value().get().unwrap_or_else(|| Ok(String::new()))); + let value = Signal::derive(move || { + do_something_action + .value() + .get() + .unwrap_or_else(|| Ok(String::new())) + }); Effect::new_isomorphic(move |_| { logging::log!("Got value = {:?}", value.get()); diff --git a/examples/action-form-error-handling/src/main.rs b/examples/action-form-error-handling/src/main.rs index d9cbb052e..30f4c743d 100644 --- a/examples/action-form-error-handling/src/main.rs +++ b/examples/action-form-error-handling/src/main.rs @@ -1,11 +1,11 @@ #[cfg(feature = "ssr")] #[actix_web::main] async fn main() -> std::io::Result<()> { + use action_form_error_handling::app::*; use actix_files::Files; use actix_web::*; use leptos::*; use leptos_actix::{generate_route_list, LeptosRoutes}; - use action_form_error_handling::app::*; let conf = get_configuration(None).await.unwrap(); let addr = conf.leptos_options.site_addr; @@ -43,8 +43,8 @@ pub fn main() { // a client-side main function is required for using `trunk serve` // prefer using `cargo leptos serve` instead // to run: `trunk serve --open --features csr` - use leptos::*; use action_form_error_handling::app::*; + use leptos::*; use wasm_bindgen::prelude::wasm_bindgen; console_error_panic_hook::set_once(); diff --git a/examples/counter_isomorphic/src/counters.rs b/examples/counter_isomorphic/src/counters.rs index 755c83fb5..b1cbabd68 100644 --- a/examples/counter_isomorphic/src/counters.rs +++ b/examples/counter_isomorphic/src/counters.rs @@ -118,9 +118,9 @@ pub fn Counters() -> impl IntoView { // This is the typical pattern for a CRUD app #[component] pub fn Counter() -> impl IntoView { - let dec = create_action(|_| adjust_server_count(-1, "decing".into())); - let inc = create_action(|_| adjust_server_count(1, "incing".into())); - let clear = create_action(|_| clear_server_count()); + let dec = create_action(|_: &()| adjust_server_count(-1, "decing".into())); + let inc = create_action(|_: &()| adjust_server_count(1, "incing".into())); + let clear = create_action(|_: &()| clear_server_count()); let counter = create_resource( move || { ( @@ -222,9 +222,10 @@ pub fn FormCounter() -> impl IntoView { #[component] pub fn MultiuserCounter() -> impl IntoView { let dec = - create_action(|_| adjust_server_count(-1, "dec dec goose".into())); - let inc = create_action(|_| adjust_server_count(1, "inc inc moose".into())); - let clear = create_action(|_| clear_server_count()); + create_action(|_: &()| adjust_server_count(-1, "dec dec goose".into())); + let inc = + create_action(|_: &()| adjust_server_count(1, "inc inc moose".into())); + let clear = create_action(|_: &()| clear_server_count()); #[cfg(not(feature = "ssr"))] let multiplayer_value = { diff --git a/examples/counter_isomorphic/src/main.rs b/examples/counter_isomorphic/src/main.rs index 1dd7f6828..368efbe82 100644 --- a/examples/counter_isomorphic/src/main.rs +++ b/examples/counter_isomorphic/src/main.rs @@ -1,57 +1,53 @@ mod counters; - use leptos::*; - use actix_files::{Files}; - use actix_web::*; - use crate::counters::*; - use leptos_actix::{generate_route_list, LeptosRoutes}; +use crate::counters::*; +use actix_files::Files; +use actix_web::*; +use leptos::*; +use leptos_actix::{generate_route_list, LeptosRoutes}; - #[get("/api/events")] - async fn counter_events() -> impl Responder { - use futures::StreamExt; +#[get("/api/events")] +async fn counter_events() -> impl Responder { + use futures::StreamExt; - let stream = - futures::stream::once(async { crate::counters::get_server_count().await.unwrap_or(0) }) - .chain(COUNT_CHANNEL.clone()) - .map(|value| { - Ok(web::Bytes::from(format!( - "event: message\ndata: {value}\n\n" - ))) as Result - }); - HttpResponse::Ok() - .insert_header(("Content-Type", "text/event-stream")) - .streaming(stream) - } - - #[actix_web::main] - async fn main() -> std::io::Result<()> { - - // Explicit server function registration is no longer required - // on the main branch. On 0.3.0 and earlier, uncomment the lines - // below to register the server functions. - // _ = GetServerCount::register(); - // _ = AdjustServerCount::register(); - // _ = ClearServerCount::register(); - - // Setting this to None means we'll be using cargo-leptos and its env vars. - // when not using cargo-leptos None must be replaced with Some("Cargo.toml") - let conf = get_configuration(None).await.unwrap(); - - let addr = conf.leptos_options.site_addr; - let routes = generate_route_list(Counters); - - HttpServer::new(move || { - let leptos_options = &conf.leptos_options; - let site_root = &leptos_options.site_root; - - App::new() - .service(counter_events) - .leptos_routes(leptos_options.to_owned(), routes.to_owned(), Counters) - .service(Files::new("/", site_root)) - //.wrap(middleware::Compress::default()) - }) - .bind(&addr)? - .run() - .await - } + let stream = futures::stream::once(async { + crate::counters::get_server_count().await.unwrap_or(0) + }) + .chain(COUNT_CHANNEL.clone()) + .map(|value| { + Ok(web::Bytes::from(format!( + "event: message\ndata: {value}\n\n" + ))) as Result + }); + HttpResponse::Ok() + .insert_header(("Content-Type", "text/event-stream")) + .streaming(stream) +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + // Setting this to None means we'll be using cargo-leptos and its env vars. + // when not using cargo-leptos None must be replaced with Some("Cargo.toml") + let conf = get_configuration(None).await.unwrap(); + + let addr = conf.leptos_options.site_addr; + let routes = generate_route_list(Counters); + + HttpServer::new(move || { + let leptos_options = &conf.leptos_options; + let site_root = &leptos_options.site_root; + + App::new() + .service(counter_events) + .leptos_routes( + leptos_options.to_owned(), + routes.to_owned(), + Counters, + ) + .service(Files::new("/", site_root)) + //.wrap(middleware::Compress::default()) + }) + .bind(&addr)? + .run() + .await }