text-document-frontend 1.4.1

Frontend integration layer and command wrappers for text-document
Documentation
// Generated by Qleany v1.7.3 from frontend_event_hub_client.tera
use common::event::{Event, EventHub, Origin};
use flume::Receiver;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;

/// Event callback type
pub type EventCallback = Box<dyn Fn(Event) + Send>;

/// Internal storage: each subscriber entry pairs its id (for O(n) removal on
/// token drop) with its callback. Vec preserves registration order so
/// subscribers fire deterministically.
type SubscriberList = Vec<(u64, EventCallback)>;

/// EventHubClient - handles event passing from backend to UI
/// Subscribe callbacks to specific event origins and start the event loop.
#[derive(Clone)]
pub struct EventHubClient {
    subscribers: Arc<Mutex<HashMap<Origin, SubscriberList>>>,
    receiver: Receiver<Event>,
    next_subscriber_id: Arc<AtomicU64>,
}

impl EventHubClient {
    /// Create a new event hub client
    pub fn new(event_hub: &EventHub) -> Self {
        EventHubClient {
            subscribers: Arc::new(Mutex::new(HashMap::new())),
            receiver: event_hub.subscribe_receiver(),
            next_subscriber_id: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Subscribe a callback to an origin. The returned `SubscriptionToken`
    /// removes the callback from the subscribers map when dropped — hold it
    /// for as long as you want the callback to fire, then drop it to
    /// unsubscribe.
    pub fn subscribe<F>(&self, origin: Origin, callback: F) -> SubscriptionToken
    where
        F: Fn(Event) + Send + 'static,
    {
        let id = self.next_subscriber_id.fetch_add(1, Ordering::Relaxed);
        {
            let mut subs = self.subscribers.lock().unwrap();
            subs.entry(origin.clone())
                .or_default()
                .push((id, Box::new(callback)));
        }
        SubscriptionToken {
            subscribers: Arc::clone(&self.subscribers),
            origin,
            id,
        }
    }

    /// Start the event loop in a background thread
    /// Blocks on the flume receiver — no polling, zero CPU when idle
    pub fn start(&self, quit_signal: Arc<std::sync::atomic::AtomicBool>) {
        let receiver = self.receiver.clone();
        let subscribers = Arc::clone(&self.subscribers);
        let quit_signal = Arc::clone(&quit_signal);

        log::info!("EventHubClient starting event loop");

        thread::spawn(move || {
            log::info!("EventHubClient event loop started");
            loop {
                match receiver.recv_timeout(std::time::Duration::from_millis(200)) {
                    Ok(event) => {
                        log::debug!("EventHubClient received event: {:?}", event);
                        let subs = subscribers.lock().unwrap();
                        if let Some(callbacks) = subs.get(&event.origin) {
                            for (_id, callback) in callbacks {
                                callback(event.clone());
                            }
                        }
                    }
                    Err(flume::RecvTimeoutError::Timeout) => {
                        // Just check quit signal
                    }
                    Err(flume::RecvTimeoutError::Disconnected) => {
                        log::info!("EventHubClient channel disconnected");
                        break;
                    }
                }

                if quit_signal.load(std::sync::atomic::Ordering::Relaxed) {
                    log::info!("EventHubClient quitting event loop");
                    break;
                }
            }
        });
    }
}

/// Opaque handle returned by `EventHubClient::subscribe`. Dropping it removes
/// the associated callback from the subscribers map; the origin's entry is
/// removed entirely when its last subscriber goes away.
pub struct SubscriptionToken {
    subscribers: Arc<Mutex<HashMap<Origin, SubscriberList>>>,
    origin: Origin,
    id: u64,
}

impl Drop for SubscriptionToken {
    fn drop(&mut self) {
        // Best-effort removal. If another thread poisoned the mutex, skip
        // cleanup rather than double-panic during unwind.
        if let Ok(mut subs) = self.subscribers.lock()
            && let Some(list) = subs.get_mut(&self.origin)
        {
            list.retain(|(id, _)| *id != self.id);
            if list.is_empty() {
                subs.remove(&self.origin);
            }
        }
    }
}