Defined Participants and first step to implement surrealdb

This commit is contained in:
xeovalyte 2024-02-13 15:46:33 +01:00
parent 3cdbfe78f9
commit 3d1a2e6117
No known key found for this signature in database
4 changed files with 79 additions and 0 deletions

View File

@ -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"] }

View File

@ -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::<util::surrealdb::SurrealContext>();
let participants = use_context::<util::surrealdb::ParticipantsContext>()
.expect("Could not find participants context");
view! {
@ -52,6 +58,9 @@ pub fn App() -> impl IntoView {
>
<Router>
<components::navbar::Navbar />
<p>{websocket.message}</p>
<p>{ format!("{:?}", participants.0.get() )}</p>
<button on:click=move |_| { websocket.send("BOE!") }>"Test Message"</button>
<main>
<Routes>
<Route path="/" view=Home />

View File

@ -0,0 +1 @@
pub mod surrealdb;

View File

@ -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<Vec<Participant>>);
#[derive(Clone)]
pub struct SurrealContext {
pub message: Signal<Option<String>>,
send: Rc<dyn Fn(&str)>,
}
impl SurrealContext {
pub fn new(message: Signal<Option<String>>, send: Rc<dyn Fn(&str)>) -> 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>>(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;
}