Fix Tokio runtime error - use std::thread instead of tokio::spawn for EventBus

This commit is contained in:
Aether 2026-02-23 21:21:28 +00:00
parent 4f9085f728
commit 653eb07444
No known key found for this signature in database
GPG Key ID: 95AFEE837E39AFD2
1 changed files with 6 additions and 6 deletions

View File

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use serde_json::Value; use serde_json::Value;
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
use tokio::sync::mpsc; use std::sync::mpsc as std_mpsc;
use tracing::{debug, info}; use tracing::{debug, info};
use uuid::Uuid; use uuid::Uuid;
@ -10,7 +10,7 @@ pub type EventHandler = Box<dyn Fn(Value) + Send + Sync>;
pub struct EventBus { pub struct EventBus {
subscribers: Arc<Mutex<HashMap<String, Vec<(SubscriptionId, EventHandler)>>>>, subscribers: Arc<Mutex<HashMap<String, Vec<(SubscriptionId, EventHandler)>>>>,
sender: mpsc::UnboundedSender<Event>, sender: std_mpsc::Sender<Event>,
} }
pub type SubscriptionId = String; pub type SubscriptionId = String;
@ -25,13 +25,13 @@ pub struct Event {
impl EventBus { impl EventBus {
pub fn new() -> Self { pub fn new() -> Self {
let (sender, mut receiver) = mpsc::unbounded_channel::<Event>(); let (sender, receiver) = std_mpsc::channel::<Event>();
let subscribers: Arc<Mutex<HashMap<String, Vec<(SubscriptionId, EventHandler)>>>> = Arc::new(Mutex::new(HashMap::new())); let subscribers: Arc<Mutex<HashMap<String, Vec<(SubscriptionId, EventHandler)>>>> = Arc::new(Mutex::new(HashMap::new()));
let subs_clone = subscribers.clone(); let subs_clone = subscribers.clone();
// Event dispatch loop // Event dispatch loop (using std::thread instead of tokio::spawn)
tokio::spawn(async move { std::thread::spawn(move || {
while let Some(event) = receiver.recv().await { while let Ok(event) = receiver.recv() {
let subs = subs_clone.lock().unwrap(); let subs = subs_clone.lock().unwrap();
if let Some(handlers) = subs.get(&event.event_type) { if let Some(handlers) = subs.get(&event.event_type) {
for (_, handler) in handlers.iter() { for (_, handler) in handlers.iter() {