Skip to content

Commit 7aeff7d

Browse files
author
Kevin Souza
committed
logs levelfilters tweak, wip leaving channel, needs more testing
1 parent 6008cc9 commit 7aeff7d

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src-tauri/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub fn run() {
3636
tauri_plugin_log::Builder::new()
3737
.level(LevelFilter::Debug)
3838
.level_for("rustls", LevelFilter::Off)
39+
.level_for("h2", LevelFilter::Off)
40+
.level_for("hyper_util", LevelFilter::Off)
41+
.level_for("sqlx", LevelFilter::Info)
3942
.build(),
4043
)
4144
.setup(|app| {

src-tauri/src/twitch/chat.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
use std::{collections::HashMap, sync::Arc, thread::sleep, time::Duration};
1+
use std::{
2+
collections::HashMap,
3+
sync::{
4+
atomic::{AtomicBool, Ordering},
5+
Arc,
6+
},
7+
thread::sleep,
8+
time::Duration,
9+
};
210

311
use futures_util::{SinkExt, StreamExt};
412
use lazy_static::lazy_static;
@@ -8,6 +16,7 @@ use serde::Serialize;
816
use tauri::{
917
async_runtime::{self, Mutex},
1018
ipc::Channel,
19+
AppHandle, Listener,
1120
};
1221
use tokio_tungstenite::tungstenite::Message;
1322

@@ -59,7 +68,11 @@ pub enum ChatEvent {
5968
}
6069

6170
#[tauri::command]
62-
pub async fn join_chat(username: &str, reader: Channel<ChatEvent>) -> Result<(), String> {
71+
pub async fn join_chat(
72+
app_handle: AppHandle,
73+
username: &str,
74+
reader: Channel<ChatEvent>,
75+
) -> Result<(), String> {
6376
let user_emotes = emote::query_user_emotes(username).await.unwrap_or_default();
6477

6578
let mut ws_stream = match tokio_tungstenite::connect_async(WS_CHAT_URL).await {
@@ -94,7 +107,19 @@ pub async fn join_chat(username: &str, reader: Channel<ChatEvent>) -> Result<(),
94107

95108
let ws_sink = Arc::new(Mutex::new(ws_sink));
96109

110+
let is_cancelled = Arc::new(AtomicBool::new(false));
111+
let cancel_flag = Arc::clone(&is_cancelled);
112+
113+
let listener = app_handle.listen("leave_chat", move |_event| {
114+
cancel_flag.store(true, Ordering::SeqCst);
115+
});
116+
97117
while let Some(Ok(Message::Text(text))) = ws_stream.next().await {
118+
if is_cancelled.load(Ordering::Relaxed) {
119+
break;
120+
}
121+
122+
// Handle PING/PONG messages
98123
if text.starts_with(PING) {
99124
let ws_sink = Arc::clone(&ws_sink);
100125

@@ -103,9 +128,8 @@ pub async fn join_chat(username: &str, reader: Channel<ChatEvent>) -> Result<(),
103128
continue;
104129
}
105130

106-
// Ping the server after 60 seconds
131+
// Schedule a PING after 60 seconds
107132
let ws_sink = Arc::clone(&ws_sink);
108-
109133
async_runtime::spawn(async move {
110134
sleep(Duration::from_secs(60));
111135

@@ -164,6 +188,8 @@ pub async fn join_chat(username: &str, reader: Channel<ChatEvent>) -> Result<(),
164188
}
165189
}
166190

191+
app_handle.unlisten(listener);
192+
167193
Ok(())
168194
}
169195

src/lib/components/Chat.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
import { openUrl } from '@tauri-apps/plugin-opener';
55
import { Channel, invoke } from '@tauri-apps/api/core';
6+
import { emit } from '@tauri-apps/api/event';
67
78
import SimpleBar from 'simplebar';
89
@@ -98,7 +99,11 @@
9899
}
99100
};
100101
101-
(async () => await invoke('join_chat', { username: watching.username, reader }))();
102+
invoke('join_chat', { username: watching.username, reader });
103+
104+
return () => {
105+
emit('leave_chat', watching.username);
106+
};
102107
});
103108
</script>
104109

0 commit comments

Comments
 (0)