From 3d1a2e61172c6fc640bbb245ba1e94b8116baf88 Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Tue, 13 Feb 2024 15:46:33 +0100 Subject: [PATCH] Defined Participants and first step to implement surrealdb --- application/Cargo.toml | 1 + application/src/lib.rs | 9 ++++ application/src/util/mod.rs | 1 + application/src/util/surrealdb.rs | 68 +++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 application/src/util/mod.rs create mode 100644 application/src/util/surrealdb.rs 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; +}