Skip to main content

ass_editor/events/
registry.rs

1//! Handler registration for [`EventChannel`].
2//!
3//! Implements registering and unregistering [`EventHandler`] instances,
4//! enforcing the configured handler limit and keeping handlers sorted by
5//! descending priority.
6
7use super::channel::{EventChannel, HandlerInfo};
8use super::EventHandler;
9use crate::core::Result;
10
11#[cfg(not(feature = "std"))]
12use alloc::{boxed::Box, format};
13
14impl EventChannel {
15    /// Register an event handler
16    pub fn register_handler(&mut self, handler: Box<dyn EventHandler>) -> Result<usize> {
17        let handler_id = self.next_handler_id;
18        self.next_handler_id += 1;
19
20        let filter = handler.event_filter();
21        let priority = handler.priority();
22
23        let handler_info = HandlerInfo {
24            id: handler_id,
25            handler,
26            filter,
27            priority,
28            events_processed: 0,
29        };
30
31        #[cfg(feature = "multi-thread")]
32        {
33            let mut handlers =
34                self.handlers
35                    .write()
36                    .map_err(|_| crate::core::EditorError::ThreadSafetyError {
37                        message: "Failed to acquire write lock for handlers".to_string(),
38                    })?;
39
40            if handlers.len() >= self.config.max_handlers {
41                return Err(crate::core::EditorError::CommandFailed {
42                    message: format!("Handler limit reached: {}", self.config.max_handlers),
43                });
44            }
45
46            handlers.push(handler_info);
47            // Sort by priority (highest first)
48            handlers.sort_by_key(|h| core::cmp::Reverse(h.priority));
49        }
50
51        #[cfg(not(feature = "multi-thread"))]
52        {
53            let mut handlers = self.handlers.borrow_mut();
54
55            if handlers.len() >= self.config.max_handlers {
56                return Err(crate::core::EditorError::CommandFailed {
57                    message: format!("Handler limit reached: {}", self.config.max_handlers),
58                });
59            }
60
61            handlers.push(handler_info);
62            // Sort by priority (highest first)
63            handlers.sort_by_key(|h| core::cmp::Reverse(h.priority));
64        }
65
66        self.stats.handlers_count += 1;
67        Ok(handler_id)
68    }
69
70    /// Unregister an event handler by ID
71    pub fn unregister_handler(&mut self, handler_id: usize) -> Result<bool> {
72        #[cfg(feature = "multi-thread")]
73        {
74            let mut handlers =
75                self.handlers
76                    .write()
77                    .map_err(|_| crate::core::EditorError::ThreadSafetyError {
78                        message: "Failed to acquire write lock for handlers".to_string(),
79                    })?;
80
81            if let Some(pos) = handlers.iter().position(|h| h.id == handler_id) {
82                handlers.remove(pos);
83                self.stats.handlers_count -= 1;
84                Ok(true)
85            } else {
86                Ok(false)
87            }
88        }
89
90        #[cfg(not(feature = "multi-thread"))]
91        {
92            let mut handlers = self.handlers.borrow_mut();
93
94            if let Some(pos) = handlers.iter().position(|h| h.id == handler_id) {
95                handlers.remove(pos);
96                self.stats.handlers_count -= 1;
97                Ok(true)
98            } else {
99                Ok(false)
100            }
101        }
102    }
103}