pub struct EventManager<T: Clone + Send + Sync + 'static> { /* private fields */ }Expand description
EventManager is a thread-safe structure for managing events.
§Features
- Allows adding event listeners.
- Emits events to all registered listeners.
- Uses
ArcandMutexto ensure thread safety.
§Example Usage
use emitix::EventManager;
let manager = EventManager::new();
manager
.add_listener("Events You Like", |event: String| {
println!("Event received: {}", event);
})
.unwrap();
manager
.emit("Events You Like", String::from("Test Event"))
.unwrap();§Thread-Safety Guarantees
- Listeners are stored in a
Mutex, ensuring exclusive access. - The structure can be cloned and shared across multiple threads using
Arc. - Listeners must be thread-safe functions (
SendandSync).
Implementations§
Source§impl<T: Clone + Send + Sync + 'static> EventManager<T>
impl<T: Clone + Send + Sync + 'static> EventManager<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new instance of EventManager.
§Returns
A new, empty instance of EventManager ready to register listeners.
Sourcepub fn list_event_kinds(&self) -> Result<Vec<String>>
pub fn list_event_kinds(&self) -> Result<Vec<String>>
Sourcepub fn add_listener<F: FnMut(T) + Send + Sync + 'static>(
&self,
event_kind: &str,
listener: F,
) -> Result<()>
pub fn add_listener<F: FnMut(T) + Send + Sync + 'static>( &self, event_kind: &str, listener: F, ) -> Result<()>
Adds an event listener.
§Arguments
event_kind: A string that identifies the type of event this listener is for.listener: A function or closure that will be called when the event is emitted.
§Returns
Ok(())if the listener was successfully added.Err(anyhow::Error)if access to theMutexfailed.
§Example
let manager = emitix::EventManager::new();
manager
.add_listener("Events You Like", |event: String| {
println!("Event received: {}", event);
})
.unwrap();Sourcepub fn remove_listener<F: FnMut(T) + Send + Sync + 'static>(
&self,
event_kind: &str,
listener: F,
) -> Result<()>
pub fn remove_listener<F: FnMut(T) + Send + Sync + 'static>( &self, event_kind: &str, listener: F, ) -> Result<()>
Removes a listener for a specific event kind.
§Arguments
event_kind: A string that identifies the type of event this listener is for.listener: The listener function or closure to be removed.
§Returns
Ok(())if the listener was successfully removed.Err(anyhow::Error)if access to theMutexfailed.
§Example
let manager = emitix::EventManager::new();
let listener = |event: String| {
println!("Event received: {}", event);
};
manager.add_listener("Events You Like", listener).unwrap();
manager
.remove_listener("Events You Like", listener)
.unwrap();Sourcepub fn has_listeners(&self, event_kind: &str) -> Result<bool>
pub fn has_listeners(&self, event_kind: &str) -> Result<bool>
Checks if there are any listeners for a specific event kind.
§Arguments
event_kind: A string that identifies the type of event to check for listeners.
§Returns
Ok(bool)indicating whether there are listeners for the specified event kind.Err(anyhow::Error)if access to theMutexfailed.
§Example
let manager = emitix::EventManager::new();
let has_listeners = manager.has_listeners("Events You Like").unwrap();Sourcepub fn listeners_count(&self, event_kind: &str) -> Result<usize>
pub fn listeners_count(&self, event_kind: &str) -> Result<usize>
Returns the number of listeners for a specific event kind.
§Arguments
event_kind: A string that identifies the type of event whose listeners count is requested.
§Returns
Ok(usize)representing the number of listeners for the specified event kind.Err(anyhow::Error)if access to theMutexfailed.
§Example
let manager = emitix::EventManager::new();
let count = manager.listeners_count("Events You Like").unwrap();Sourcepub fn clear_listeners(&self, event_kind: &str) -> Result<()>
pub fn clear_listeners(&self, event_kind: &str) -> Result<()>
Clears all listeners for a specific event kind.
§Arguments
event_kind: A string that identifies the type of event whose listeners should be cleared.
§Returns
Ok(())if the listeners were successfully cleared.Err(anyhow::Error)if access to theMutexfailed.
§Example
let manager = emitix::EventManager::new();
manager.clear_listeners("Events You Like").unwrap()Sourcepub fn emit(&self, event_kind: &str, event_arg: T) -> Result<()>
pub fn emit(&self, event_kind: &str, event_arg: T) -> Result<()>
Emits an event to all registered listeners.
§Arguments
event_kind: A string that identifies the type of event being emitted.event_arg: The event argument that will be passed to each listener.
§Returns
Ok(())if the event was successfully emitted.Err(anyhow::Error)if access to theMutexfailed.
§Example
let manager = emitix::EventManager::new();
manager
.emit("Events You Like", String::from("Test Event"))
.unwrap();Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for EventManager<T>
impl<T> RefUnwindSafe for EventManager<T>
impl<T> Send for EventManager<T>
impl<T> Sync for EventManager<T>
impl<T> Unpin for EventManager<T>
impl<T> UnwindSafe for EventManager<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more