mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 00:59:22 -05:00
example cleanup
This commit is contained in:
parent
fb9dfc7b76
commit
162432cbfd
1 changed files with 8 additions and 24 deletions
|
@ -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))/>
|
||||
|
|
Loading…
Add table
Reference in a new issue