Struct AsyncEventEmitter

Source
pub struct AsyncEventEmitter {
    pub listeners: DashMap<String, Vec<AsyncListener>>,
}

Fields§

§listeners: DashMap<String, Vec<AsyncListener>>

Implementations§

Source§

impl AsyncEventEmitter

Source

pub fn new() -> Self

Source

pub async fn emit<'a, T>(&self, event: &str, value: T) -> Result<()>
where T: Serialize + Deserialize<'a> + Send + Sync + 'a,

Emits an event of the given parameters and executes each callback that is listening to that event asynchronously by spawning a task for each callback.

§Example
use async_event_emitter::AsyncEventEmitter;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let event_emitter = AsyncEventEmitter::new();

    // Emits the <"Some event"> event and a value <"Hello programmer">
    // The value can be of any type as long as it implements the serde Serialize trait
    event_emitter.emit("Some event", "Hello programmer!").await;

    Ok(())
}
Source

pub fn remove_listener(&self, id_to_delete: &str) -> Option<String>

Removes an event listener with the given id

§Example
use async_event_emitter::AsyncEventEmitter;
let event_emitter = AsyncEventEmitter::new();
let listener_id =
    event_emitter.on("Some event", |value: ()| async { println!("Hello world!") });
println!("{:?}", event_emitter.listeners);

// Removes the listener that we just added
event_emitter.remove_listener(&listener_id);
Source

pub fn on_limited<F, T, C>( &self, event: &str, limit: Option<u64>, callback: C, ) -> String
where for<'de> T: Deserialize<'de>, C: Fn(T) -> F + Send + Sync + 'static, F: Future<Output = ()> + Send + Sync + 'static,

Adds an event listener that will only execute the listener x amount of times - Then the listener will be deleted. Returns the id of the newly added listener.

§Example
use async_event_emitter::AsyncEventEmitter;
#[tokio::main]
async fn main() {
let event_emitter = AsyncEventEmitter::new();
// Listener will be executed 3 times. After the third time, the listener will be deleted.
event_emitter.on_limited("Some event", Some(3), |value: ()| async{ println!("Hello world!")});
event_emitter.emit("Some event", ()).await; // 1 >> "Hello world!"
event_emitter.emit("Some event", ()).await; // 2 >> "Hello world!"
event_emitter.emit("Some event", ()).await; // 3 >> "Hello world!"
event_emitter.emit("Some event", ()).await; // 4 >> <Nothing happens here because listener was deleted after the 3rd call>
}
Source

pub fn once<F, T, C>(&self, event: &str, callback: C) -> String
where for<'de> T: Deserialize<'de>, C: Fn(T) -> F + Send + Sync + 'static, F: Future<Output = ()> + Send + Sync + 'static,

Adds an event listener that will only execute the callback once - Then the listener will be deleted. Returns the id of the newly added listener.

§Example
use async_event_emitter::AsyncEventEmitter;
let  event_emitter = AsyncEventEmitter::new();

event_emitter.once("Some event", |value: ()| async {println!("Hello world!")});
event_emitter.emit("Some event", ()); // First event is emitted and the listener's callback is called once
// >> "Hello world!"

event_emitter.emit("Some event", ());
// >> <Nothing happens here since listener was deleted>
Source

pub fn on<F, T, C>(&self, event: &str, callback: C) -> String
where for<'de> T: Deserialize<'de>, C: Fn(T) -> F + Send + Sync + 'static, F: Future<Output = ()> + Send + Sync + 'static,

Adds an event listener with a callback that will get called whenever the given event is emitted. Returns the id of the newly added listener.

§Example
use async_event_emitter::AsyncEventEmitter;
let  event_emitter = AsyncEventEmitter::new();

// This will print <"Hello world!"> whenever the <"Some event"> event is emitted
// The type of the `value` parameter for the closure MUST be specified and, if you plan to use the `value`, the `value` type
// MUST also match the type that is being emitted (here we just use a throwaway `()` type since we don't care about using the `value`)
event_emitter.on("Some event", |value: ()| async { println!("Hello world!")});

Trait Implementations§

Source§

impl Clone for AsyncEventEmitter

Source§

fn clone(&self) -> AsyncEventEmitter

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for AsyncEventEmitter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AsyncEventEmitter

Source§

fn default() -> AsyncEventEmitter

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.