From a2239cfe7e7176c6525a4cdaeb3d7e63293c5e41 Mon Sep 17 00:00:00 2001 From: Sam Sartor Date: Tue, 21 May 2024 21:38:40 -0400 Subject: [PATCH] basic dioxus state --- Cargo.lock | 1 + Cargo.toml | 1 + src/app.rs | 13 +++++++++++++ src/lib.rs | 7 +++++++ src/main.rs | 3 +-- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 63b189c..4e65407 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1525,6 +1525,7 @@ dependencies = [ "dioxus", "dioxus-web", "manganis", + "once_cell", "serde-wasm-bindgen 0.6.5", "serde_json", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 318807a..5b4008a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" dioxus = { version = "0.5.1", features = ["web"] } dioxus-web = "0.5.1" manganis = "0.2.2" +once_cell = "1.19.0" serde-wasm-bindgen = "0.6.5" serde_json = "1.0.117" wasm-bindgen = "0.2.92" diff --git a/src/app.rs b/src/app.rs index 196cb3b..2450577 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,10 +2,23 @@ use dioxus::prelude::*; +pub struct State { + pub status: GlobalSignal, +} + +pub static STATE: State = State { + status: Signal::global(|| "Starting...".to_owned()), +}; + pub fn app() -> Element { + use_coroutine(|_: UnboundedReceiver<()>| super::network_entrypoint()); + + let status = &STATE.status; rsx!( div { "Hello, World!" + br {} + "{status}" } ) } diff --git a/src/lib.rs b/src/lib.rs index 6abddd7..75af3fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ pub mod app; +use app::STATE; use wasm_bindgen::prelude::*; use web_sys::console; use web_sys::WebTransport; @@ -42,6 +43,8 @@ pub async fn network_entrypoint() { console::log_1(&"Created WebTransportOptions!".into()); + *STATE.status.write() = "Connecting to WebTransport...".into(); + let transport = match WebTransport::new_with_options( "https://localhost:4433/?hostname=ohea.xyz&port=64738&username=test", &options, @@ -64,6 +67,8 @@ pub async fn network_entrypoint() { console::log_1(&"Transport is ready.".into()); + *STATE.status.write() = "Creating stream...".into(); + let stream: web_sys::WebTransportBidirectionalStream = match wasm_bindgen_futures::JsFuture::from(transport.create_bidirectional_stream()).await { Ok(x) => x.into(), @@ -78,4 +83,6 @@ pub async fn network_entrypoint() { console::log_1(&"Created bidirectional stream!".into()); console::log_1(&stream.into()); + + //*STATE.status.write() = "Ready!".into(); } diff --git a/src/main.rs b/src/main.rs index d8ac286..9c1fbae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ -use mumble_webtransport::{app, network_entrypoint}; +use mumble_webtransport::app; pub fn main() { - wasm_bindgen_futures::spawn_local(network_entrypoint()); dioxus::launch(app::app); }