diff --git a/application/Cargo.toml b/application/Cargo.toml index 5ae2782..3637192 100644 --- a/application/Cargo.toml +++ b/application/Cargo.toml @@ -13,6 +13,7 @@ leptos_router = { version = "0.6", features = ["csr"] } console_log = "1" log = "0.4" console_error_panic_hook = "0.1" +leptos-use = "0.10.2" # utils # strum = { version = "0.25", features = ["derive", "strum_macros"] } diff --git a/application/src/lib.rs b/application/src/lib.rs index c036b63..266150a 100644 --- a/application/src/lib.rs +++ b/application/src/lib.rs @@ -5,6 +5,7 @@ use leptos_router::*; // Modules mod components; mod pages; +mod util; // Top-Level pages use crate::pages::home::Home; @@ -17,6 +18,11 @@ use crate::pages::times; pub fn App() -> impl IntoView { // Provides context that manages stylesheets, titles, meta tags, etc. provide_meta_context(); + util::surrealdb::init_surrealdb(); + + let websocket = expect_context::(); + let participants = use_context::() + .expect("Could not find participants context"); view! { @@ -52,6 +58,9 @@ pub fn App() -> impl IntoView { > +

{websocket.message}

+

{ format!("{:?}", participants.0.get() )}

+
diff --git a/application/src/util/mod.rs b/application/src/util/mod.rs new file mode 100644 index 0000000..9538350 --- /dev/null +++ b/application/src/util/mod.rs @@ -0,0 +1 @@ +pub mod surrealdb; diff --git a/application/src/util/surrealdb.rs b/application/src/util/surrealdb.rs new file mode 100644 index 0000000..b2acd93 --- /dev/null +++ b/application/src/util/surrealdb.rs @@ -0,0 +1,68 @@ +use leptos::*; +use leptos_use::{core::ConnectionReadyState, use_websocket, UseWebsocketReturn}; +use std::rc::Rc; + +#[derive(Clone, Debug)] +pub enum HourKind { + A, + B, + C, + D, + Z, +} + +#[derive(Clone, Debug)] +pub struct GroupKind { + hour: HourKind, + lane: u8, +} + +#[derive(Clone, Debug)] +pub struct Participant { + name: String, + group: GroupKind, +} + +#[derive(Clone, Debug)] +pub struct ParticipantsContext(pub ReadSignal>); + +#[derive(Clone)] +pub struct SurrealContext { + pub message: Signal>, + send: Rc, +} + +impl SurrealContext { + pub fn new(message: Signal>, send: Rc) -> Self { + Self { message, send } + } + + #[inline(always)] + pub fn send(&self, message: &str) { + (self.send)(message) + } +} + +pub fn init_surrealdb() { + let UseWebsocketReturn { + ready_state, + message, + send, + .. + } = use_websocket("wss://echo.websocket.events/"); + + let (participants, _set_participants) = create_signal::>(vec![Participant { + name: "User1".to_string(), + group: GroupKind { + hour: HourKind::A, + lane: 1, + }, + }]); + + provide_context(SurrealContext::new(message, Rc::new(send.clone()))); + provide_context(ParticipantsContext(participants)); + + let _status = move || ready_state.get().to_string(); + + let _connected = move || ready_state.get() == ConnectionReadyState::Open; +}