Struct EventManager

Source
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 Arc and Mutex to 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 (Send and Sync).

Implementations§

Source§

impl<T: Clone + Send + Sync + 'static> EventManager<T>

Source

pub fn new() -> Self

Creates a new instance of EventManager.

§Returns

A new, empty instance of EventManager ready to register listeners.

Source

pub 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.
  • Err(anyhow::Error) if access to the Mutex failed.
§Example
let manager = emitix::EventManager::new();
let list = manager.list_event_kinds().unwrap();
Source

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 the Mutex failed.
§Example
let manager = emitix::EventManager::new();
manager
    .add_listener("Events You Like", |event: String| {
        println!("Event received: {}", event);
    })
    .unwrap();
Source

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 the Mutex failed.
§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();
Source

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 the Mutex failed.
§Example
let manager = emitix::EventManager::new();
let has_listeners = manager.has_listeners("Events You Like").unwrap();
Source

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 the Mutex failed.
§Example
let manager = emitix::EventManager::new();
let count = manager.listeners_count("Events You Like").unwrap();
Source

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 the Mutex failed.
§Example
let manager = emitix::EventManager::new();
manager.clear_listeners("Events You Like").unwrap()
Source

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 the Mutex failed.
§Example
let manager = emitix::EventManager::new();
manager
    .emit("Events You Like", String::from("Test Event"))
    .unwrap();

Trait Implementations§

Source§

impl<T: Clone + Clone + Send + Sync + 'static> Clone for EventManager<T>

Source§

fn clone(&self) -> EventManager<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.