From 4f5a9c7dc3bc5638af20a0a95949071bc11f3a28 Mon Sep 17 00:00:00 2001 From: cool-mist Date: Sun, 9 Aug 2026 19:11:36 +0530 Subject: [PATCH] WIP --- crates/hd-bus/src/lib.rs | 52 ++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/crates/hd-bus/src/lib.rs b/crates/hd-bus/src/lib.rs index 6ee221a..65520f5 100644 --- a/crates/hd-bus/src/lib.rs +++ b/crates/hd-bus/src/lib.rs @@ -1,7 +1,10 @@ use std::{ - collections::VecDeque, sync::{RwLock, mpsc::{Receiver, Sender}}, + collections::{HashMap, VecDeque}, sync::RwLock, thread::{self, JoinHandle}, time::Duration, }; +pub type EventId = u64; +pub type SubscriptionId = u64; + pub enum HError { BusLockPoisoned(String), } @@ -15,26 +18,15 @@ pub struct Event { pub struct EventBus { queue: RwLock>, subscriptions: RwLock>, - processor: Sender, } -pub trait SubscriptionEndpoint { - fn deliver(&self, evt: &Event); -} - -struct ConsoleEndpoint; -impl SubscriptionEndpoint for ConsoleEndpoint { - fn deliver(&self, evt: &Event) { - let data = String::from_utf8_lossy(&evt.data_utf8); - println!("({}, {}, {})", evt.id, evt.topic_name, data); - } +pub struct SubscriptionRequest { + topic_pattern: String, } pub struct Subscription { - topic_pattern: String, - /// from_id is inclusive - from_id: u64, - endpoint: Box, + id: u64, + request: SubscriptionRequest, } impl EventBus { @@ -54,23 +46,37 @@ impl EventBus { Ok(()) } - pub fn subscribe(&self, request: Subscription) -> Result<(), HError> { + pub fn subscribe(&self, request: SubscriptionRequest) -> Result<(), HError> { let mut subscriptions = self .subscriptions .write() .map_err(|e| HError::BusLockPoisoned(e.to_string()))?; - subscriptions.push(request); + + let last_id = subscriptions.last().map(|s| s.id).unwrap_or(0); + let subscription = Subscription { + id: last_id + 1, + request, + }; + subscriptions.push(subscription); Ok(()) } } -struct EventProcessRequest { - id: u64, +struct EventBusProcessor { + last_id: u64, + subscriptions_queue: RwLock>, } -struct EventBusProcessor { - last_processed_id: u64, - work: Receiver, +impl EventBusProcessor { + fn start() -> JoinHandle { + thread::spawn(|| { + loop { + thread::sleep(Duration::from_secs(1)); + } + + return 0; + }) + } } #[cfg(test)]