ass_editor/events/handler.rs
1//! Handler trait and channel configuration/statistics types.
2//!
3//! Declares the [`EventHandler`] trait implemented by observers along with the
4//! [`EventStats`] and [`EventChannelConfig`] value types used by the channel.
5
6use super::{DocumentEvent, EventFilter};
7use crate::core::Result;
8
9/// Event handler trait for responding to document events
10pub trait EventHandler: Send + Sync {
11 /// Handle a document event
12 fn handle_event(&mut self, event: &DocumentEvent) -> Result<()>;
13
14 /// Get the event filter for this handler
15 fn event_filter(&self) -> EventFilter {
16 EventFilter::new()
17 }
18
19 /// Get handler priority (higher numbers = higher priority)
20 fn priority(&self) -> i32 {
21 0
22 }
23}
24
25/// Statistics about event handling
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct EventStats {
28 /// Total number of events dispatched
29 pub events_dispatched: usize,
30 /// Number of handlers currently registered
31 pub handlers_count: usize,
32 /// Number of events dropped due to filters
33 pub events_filtered: usize,
34 /// Number of async events queued
35 pub async_events_queued: usize,
36 /// Average event processing time in microseconds
37 pub avg_processing_time_us: u64,
38}
39
40/// Event channel configuration
41#[derive(Debug, Clone)]
42pub struct EventChannelConfig {
43 /// Maximum number of handlers
44 pub max_handlers: usize,
45 /// Maximum size of async event queue
46 pub max_async_queue_size: usize,
47 /// Whether to enable event batching
48 pub enable_batching: bool,
49 /// Maximum batch size for event processing
50 pub max_batch_size: usize,
51 /// Whether to log events for debugging
52 pub enable_logging: bool,
53}
54
55impl Default for EventChannelConfig {
56 fn default() -> Self {
57 Self {
58 max_handlers: 100,
59 max_async_queue_size: 1000,
60 enable_batching: false,
61 max_batch_size: 10,
62 enable_logging: false,
63 }
64 }
65}