From 162432cbfd32318c4fbcef178c534b7a1f190189 Mon Sep 17 00:00:00 2001 From: Maccesch Date: Thu, 31 Aug 2023 18:40:03 +0100 Subject: [PATCH] example cleanup --- examples/use_webtransport/src/main.rs | 32 +++++++-------------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/examples/use_webtransport/src/main.rs b/examples/use_webtransport/src/main.rs index da7b23d..80bcafe 100644 --- a/examples/use_webtransport/src/main.rs +++ b/examples/use_webtransport/src/main.rs @@ -5,12 +5,14 @@ use leptos_use::{use_webtransport_with_options, UseWebTransportOptions}; #[component] fn Demo() -> impl IntoView { - let (log, set_log) = create_signal(vec![]); + let (datagrams_log, set_datagrams_log) = create_signal(vec![]); let transport = use_webtransport_with_options( "https://echo.webtransport.day", UseWebTransportOptions::default() - .on_error(|e| set_log.update(|log| log.push(format!("Error: {:?}", e)))), + .on_open(|_| set_datagrams_log.update(|log| log.push("Connection opened".to_string()))) + .on_close(|_| set_datagrams_log.update(|log| log.push("Connection closed".to_string()))) + .on_error(|e| set_datagrams_log.update(|log| log.push(format!("Error: {:?}", e)))), ); let (text, set_text) = create_signal("".to_string()); @@ -19,37 +21,17 @@ fn Demo() -> impl IntoView { let transport = transport.clone(); move |e| { - set_log.update(|log| log.push(format!("Sent datagram: '{}'", text()))); + set_datagrams_log.update(|log| log.push(format!("Sent datagram: '{}'", text()))); transport.send_datagrams(text().as_bytes()); } }; - let ready_state = transport.ready_state; - - let _ = watch( - ready_state, - move |ready, prev_ready, _| { - if ready == &ConnectionReadyState::Open - && prev_ready.unwrap_or(&ConnectionReadyState::Closed) - != &ConnectionReadyState::Open - { - set_log.update(|log| log.push("Connection opened".to_string())); - } else if ready == &ConnectionReadyState::Closed - && prev_ready.unwrap_or(&ConnectionReadyState::Open) - != &ConnectionReadyState::Closed - { - set_log.update(|log| log.push("Connection closed".to_string())); - } - }, - false, - ); - let _ = watch( transport.datagrams, move |grams, _, _| { if let Some(grams) = grams { - set_log.update(|log| { + set_datagrams_log.update(|log| { log.push(format!( "Received datagrams: '{}'", String::from_utf8(grams.clone()).expect("valid utf8") @@ -60,6 +42,8 @@ fn Demo() -> impl IntoView { false, ); + let ready_state = transport.ready_state; + view! {

Datagrams