pub struct AsyncEventEmitter { /* private fields */ }
Implementations§
Source§impl AsyncEventEmitter
impl AsyncEventEmitter
pub fn new() -> Self
Sourcepub fn event_count(&self) -> usize
pub fn event_count(&self) -> usize
Returns the numbers of events
§Example
use async_event_emitter::AsyncEventEmitter;
let event_emitter = AsyncEventEmitter::new();
event_emitter.event_count(); // returns 0
Sourcepub fn listeners_by_event(&self, event: &str) -> Vec<AsyncListener>
pub fn listeners_by_event(&self, event: &str) -> Vec<AsyncListener>
Returns all listeners on the specified event
§Example
use async_event_emitter::AsyncEventEmitter;
#[tokio::main]
async fn main() {
let emitter = AsyncEventEmitter::new();
emitter.on("test", |value: ()| async { println!("Hello world!") });
emitter.emit("test", ()).await;
let listeners = emitter.listeners_by_event("test");
println!("{listeners:?}");
}
Sourcepub fn listener_count_by_event(&self, event: &str) -> usize
pub fn listener_count_by_event(&self, event: &str) -> usize
Returns the numbers of listners per event
§Example
use async_event_emitter::AsyncEventEmitter;
#[tokio::main]
async fn main() {
let emitter = AsyncEventEmitter::new();
emitter.on("test", |value: ()| async { println!("Hello world!") });
emitter.emit("test", ()).await;
emitter.listener_count_by_event("test"); // returns 1
}
Sourcepub async fn emit<T>(&self, event: &str, value: T) -> Result<()>where
T: Serialize,
pub async fn emit<T>(&self, event: &str, value: T) -> Result<()>where
T: Serialize,
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(())
}
Sourcepub fn remove_listener(&self, id_to_delete: Uuid) -> Option<Uuid>
pub fn remove_listener(&self, id_to_delete: Uuid) -> Option<Uuid>
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!") });
// Removes the listener that we just added
event_emitter.remove_listener(listener_id);
Sourcepub fn on_limited<F, T, C>(
&self,
event: &str,
limit: Option<u64>,
callback: C,
) -> Uuid
pub fn on_limited<F, T, C>( &self, event: &str, limit: Option<u64>, callback: C, ) -> Uuid
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>
}
Sourcepub fn once<F, T, C>(&self, event: &str, callback: C) -> Uuid
pub fn once<F, T, C>(&self, event: &str, callback: C) -> Uuid
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>
Sourcepub fn on<F, T, C>(&self, event: &str, callback: C) -> Uuid
pub fn on<F, T, C>(&self, event: &str, callback: C) -> Uuid
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!")});
Sourcepub fn on_all<F, T, C>(&self, callback: C) -> Uuid
pub fn on_all<F, T, C>(&self, callback: C) -> Uuid
Adds an event listener called for whenever every event is called Returns the id of the newly added listener.
§Example
use async_event_emitter::AsyncEventEmitter;
#[tokio::main]
async fn main() {
let mut event_emitter = AsyncEventEmitter::new();
// this will print Hello world two because of
event_emitter.on_all(|value: ()| async { println!("Hello world!") });
event_emitter.emit("Some event", ()).await;
// >> "Hello world!"
event_emitter.emit("next event", ()).await;
// >> <Nothing happens here since listener was deleted>
}
Trait Implementations§
Source§impl Clone for AsyncEventEmitter
impl Clone for AsyncEventEmitter
Source§fn clone(&self) -> AsyncEventEmitter
fn clone(&self) -> AsyncEventEmitter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more