pub trait EventManager<T: Clone + Send + Sync + 'static = ()>:
Default
+ Clone
+ Send
+ Sync
+ 'static {
// Required methods
fn list_event_kinds(&self) -> Result<Vec<String>>;
fn has_listeners(&self, event_kind: &str) -> Result<bool>;
fn listeners_count(&self, event_kind: &str) -> Result<usize>;
fn clear_listeners(&self) -> Result<()>;
fn add_listener<F: FnMut(T) + Send + Sync + 'static>(
&self,
event_kind: &str,
listener: F,
) -> Result<Uuid>;
fn remove_listener(&self, listener_id: Uuid) -> Result<bool>;
fn remove_listeners_by_kind(&self, event_kind: &str) -> Result<usize>;
fn new_emitter(&self, event_kind: &str) -> Box<dyn EventEmitter<T>>;
fn new_broadcast_emitter(
&self,
event_kinds: &[&str],
) -> Box<dyn EventEmitter<T>>;
fn new_null_emitter() -> Box<dyn EventEmitter<T>>;
}Required Methods§
Sourcefn list_event_kinds(&self) -> Result<Vec<String>>
fn list_event_kinds(&self) -> Result<Vec<String>>
Lists all event kinds that have registered listeners.
§Returns
Ok(Vec<String>)containing the names of all event kinds with listeners.Err(anyhow::Error)if access to the underlying data structure fails.
Sourcefn has_listeners(&self, event_kind: &str) -> Result<bool>
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 the underlying data structure fails.
Sourcefn listeners_count(&self, event_kind: &str) -> Result<usize>
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 the underlying data structure fails.
Sourcefn clear_listeners(&self) -> Result<()>
fn clear_listeners(&self) -> Result<()>
Clears all listeners.
§Returns
Ok(())if the listeners were successfully cleared.Err(anyhow::Error)if access to the underlying data structure fails.
Sourcefn add_listener<F: FnMut(T) + Send + Sync + 'static>(
&self,
event_kind: &str,
listener: F,
) -> Result<Uuid>
fn add_listener<F: FnMut(T) + Send + Sync + 'static>( &self, event_kind: &str, listener: F, ) -> Result<Uuid>
Adds a listener for a specific event kind.
§Arguments
event_kind: A string that identifies the type of event to listen for.listener: A function that will be called when the event occurs.
§Listener Function
The listener function must accept a single argument of type T,
which is the event data, and must be Send, Sync, and 'static.
§Returns
Ok(Uuid)which is a unique identifier for the listener.Err(anyhow::Error)if access to the underlying data structure fails.
Sourcefn remove_listener(&self, listener_id: Uuid) -> Result<bool>
fn remove_listener(&self, listener_id: Uuid) -> Result<bool>
Sourcefn remove_listeners_by_kind(&self, event_kind: &str) -> Result<usize>
fn remove_listeners_by_kind(&self, event_kind: &str) -> Result<usize>
Sourcefn new_emitter(&self, event_kind: &str) -> Box<dyn EventEmitter<T>>
fn new_emitter(&self, event_kind: &str) -> Box<dyn EventEmitter<T>>
Creates a new event emitter for the specified event kind.
§Arguments
event_kind: A string that identifies the type of event this emitter will handle.
§Returns
Ok(Box<dyn EventEmitter<T>>)which is a boxed event emitter that can emit events of typeT.Err(anyhow::Error)if the emitter could not be created.
Sourcefn new_broadcast_emitter(
&self,
event_kinds: &[&str],
) -> Box<dyn EventEmitter<T>>
fn new_broadcast_emitter( &self, event_kinds: &[&str], ) -> Box<dyn EventEmitter<T>>
Emits an event of type T to all registered listeners for the specified event kinds.
§Arguments
event_kinds: A slice of strings that identifies the types of events to emit.
§Returns
Ok(Box<dyn EventEmitter<T>>)which is a boxed event emitter that can emit events of typeT.Err(anyhow::Error)if the emitter could not be created.
Sourcefn new_null_emitter() -> Box<dyn EventEmitter<T>>
fn new_null_emitter() -> Box<dyn EventEmitter<T>>
Returns a null emitter used as default emitter.
Null emitters do not emit events and do not have any listeners.
It’s like, we call Self::default().new_emitter("").
This is useful when no listeners are registered for an event kind.
§Returns
Box<dyn EventEmitter<T>>which is a boxed event emitter that does not emit events.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.