example cleanup

This commit is contained in:
Maccesch 2023-08-31 18:40:03 +01:00
parent fb9dfc7b76
commit 162432cbfd

View file

@ -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! {
<h2>Datagrams</h2>
<textarea on:change=move |e| set_text(event_target_value(&e))/>