Skip to content

Commit

Permalink
Clean up WebSocket code
Browse files Browse the repository at this point in the history
  • Loading branch information
kahnclusions committed Aug 8, 2024
1 parent cc04658 commit 313f37e
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 697 deletions.
35 changes: 0 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion app/src/components/status_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use qbittorrent_rs_proto::transfer::ServerStateFull;

#[component]
pub fn StatusBar(server_state: ServerState) -> impl IntoView {
tracing::info!("updating with new server_state");
let dl_speed = move || human_bytes(server_state.dl_info_speed.get());
let up_speed = move || human_bytes(server_state.up_info_speed.get());
let dl_data = move || human_bytes(server_state.dl_info_data.get());
Expand Down
80 changes: 24 additions & 56 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ mod signals;

use crate::error_template::{AppError, ErrorTemplate};
use auth::{has_auth, Login};
use codee::binary::MsgpackSerdeCodec;
use components::{status_bar::StatusBar, torrents::TorrentList};
use leptos::{either::Either, prelude::*};
use leptos_meta::*;
use leptos_router::{components::*, StaticSegment};

use fnord_ui::components::{Navbar, NavbarBrand, Text, View};
use qbittorrent_rs_proto::sync::MainData;
use serde::{Deserialize, Serialize};
use signals::syncstate::{ServerState, SyncState, Torrent};
use signals::use_sync_maindata::{use_sync_maindata, UseSyncMaindataReturn};
use use_websocket::core::ConnectionReadyState;
use use_websocket::{use_websocket, UseWebSocketReturn};

pub mod error_template;

Expand Down Expand Up @@ -51,56 +47,6 @@ pub fn App() -> impl IntoView {
let is_auth = Resource::new(move || login.version(), move |_| has_auth());
let auth = Signal::derive(move || is_auth.get().map(|v| v.unwrap_or(false)).unwrap_or(false));

// let UseSyncMaindataReturn {
// connected, data, ..
// } = use_sync_maindata("wss://local.ck.dev/ws");

let (data, set_data) = signal(SyncState::default());

let UseWebSocketReturn {
ready_state,
message,
..
} = use_websocket::<MainData, MsgpackSerdeCodec>("wss://local.ck.dev/ws");

let connected = Signal::derive(move || ready_state.get() == ConnectionReadyState::Open);

Effect::new(move |_| {
message.with(|message| {
if let Some(m) = message {
match m {
MainData::Full(full_data) => set_data.set(SyncState::from(full_data)),
MainData::Partial(partial_data) => {
data.with(|data| {
let torrents = partial_data.clone().torrents;
if let Some(torrents) = torrents {
for (hash, partial) in torrents {
data.torrents
.get(&hash)
.map(|torrent| torrent.apply_partial(partial));
}
}
if let Some(server_state) = partial_data.clone().server_state {
data.server_state.apply_partial(server_state);
}
});
}
}
}
});
});

let status = move || {
if connected.get() {
"Connected"
} else {
"Disconnected"
}
};

Effect::new(move |_| {
tracing::info!("Status = {}", status());
});
// let torrents = Signal::derive(move || {
// let v: Vec<_> = data().torrents.into_iter().map(|(_h, v)| v).collect();
// v
Expand All @@ -115,7 +61,7 @@ pub fn App() -> impl IntoView {

// content for this welcome page
<Router>
<Navbar>
<Navbar class="z-50">
<NavbarBrand class="font-display text-green1">"bit-tower"</NavbarBrand>
<ul class="p-2 font-cubic">
<A href="/login">login</A>
Expand All @@ -128,7 +74,29 @@ pub fn App() -> impl IntoView {
outside_errors.insert_with_default_key(AppError::NotFound);
view! { <ErrorTemplate outside_errors/> }.into_view()
}>
<Route path=StaticSegment("") view=move || view! { <HomePage is_auth=auth action=login data=data /> } />
<Route path=StaticSegment("") view=move || {
let UseSyncMaindataReturn {
connected, data, ..
} = use_sync_maindata("wss://local.ck.dev/ws");

let status = move || {
if connected.get() {
"Connected"
} else {
"Disconnected"
}
};

Effect::new(move |_| {
tracing::info!("Status = {}", status());
});

view! {


<HomePage is_auth=auth action=login data=data />

}} />
</FlatRoutes>
</main>
</Router>
Expand Down
1 change: 0 additions & 1 deletion app/src/signals/syncstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ impl ServerState {
self.connection_status.set(cs);
}
if let Some(v) = partial.dl_info_speed {
tracing::info!("speed = {}", v);
self.dl_info_speed.set(v);
}
if let Some(v) = partial.up_info_speed {
Expand Down
9 changes: 7 additions & 2 deletions app/src/signals/use_sync_maindata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use leptos::prelude::*;
use qbittorrent_rs_proto::sync::MainData;
use use_websocket::{core::ConnectionReadyState, use_websocket, UseWebSocketReturn};
use use_websocket::{
core::ConnectionReadyState, use_websocket, use_websocket_with_options, UseWebSocketError,
UseWebSocketOptions, UseWebSocketReturn,
};

use codee::binary::MsgpackSerdeCodec;

Expand All @@ -26,13 +29,15 @@ pub fn use_sync_maindata(
) -> UseSyncMaindataReturn<impl Fn() + Clone + 'static, impl Fn() + Clone + 'static> {
let (data, set_data) = signal(SyncState::default());

let opts = UseWebSocketOptions::default();

let UseWebSocketReturn {
ready_state,
message,
open,
close,
..
} = use_websocket::<MainData, MsgpackSerdeCodec>(url);
} = use_websocket_with_options::<MainData, MsgpackSerdeCodec>(url, opts);

let connected = Signal::derive(move || ready_state.get() == ConnectionReadyState::Open);

Expand Down
45 changes: 0 additions & 45 deletions qbittorrent_rs_sse/Cargo.toml

This file was deleted.

36 changes: 0 additions & 36 deletions qbittorrent_rs_sse/src/axum.rs

This file was deleted.

Loading

0 comments on commit 313f37e

Please sign in to comment.