Struct async_event_emitter::AsyncEventEmitter
source · pub struct AsyncEventEmitter {
pub listeners: HashMap<String, Vec<AsyncListener>>,
}
Fields§
§listeners: HashMap<String, Vec<AsyncListener>>
Implementations§
source§impl AsyncEventEmitter
impl AsyncEventEmitter
pub fn new() -> Self
sourcepub async fn emit<'a, T>(&mut self, event: &str, value: T) -> Result<()>where
T: Serialize + Deserialize<'a> + Send + Sync + 'a + Debug,
pub async fn emit<'a, T>(&mut self, event: &str, value: T) -> Result<()>where T: Serialize + Deserialize<'a> + Send + Sync + 'a + Debug,
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;
let mut 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
tokio_test::block_on(async {
event_emitter.emit("Some event", "Hello programmer!").await;
})
sourcepub fn remove_listener(&mut self, id_to_delete: &str) -> Option<String>
pub fn remove_listener(&mut self, id_to_delete: &str) -> Option<String>
Removes an event listener with the given id
Example
use async_event_emitter::AsyncEventEmitter;
let mut 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);
sourcepub fn on_limited<F, T, C>(
&mut self,
event: &str,
limit: Option<u64>,
callback: C
) -> Stringwhere
for<'de> T: Deserialize<'de> + Debug,
C: Fn(T) -> F + Send + Sync + 'static,
F: Future<Output = ()> + Send + Sync + 'static,
pub fn on_limited<F, T, C>( &mut self, event: &str, limit: Option<u64>, callback: C ) -> Stringwhere for<'de> T: Deserialize<'de> + Debug, 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;
let mut 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!")});
tokio_test::block_on( async{
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>(&mut self, event: &str, callback: C) -> Stringwhere
for<'de> T: Deserialize<'de> + Debug,
C: Fn(T) -> F + Send + Sync + 'static,
F: Future<Output = ()> + Send + Sync + 'static,
pub fn once<F, T, C>(&mut self, event: &str, callback: C) -> Stringwhere for<'de> T: Deserialize<'de> + Debug, 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 mut 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>(&mut self, event: &str, callback: C) -> Stringwhere
for<'de> T: Deserialize<'de> + Debug,
C: Fn(T) -> F + Send + Sync + 'static,
F: Future<Output = ()> + Send + Sync + 'static,
pub fn on<F, T, C>(&mut self, event: &str, callback: C) -> Stringwhere for<'de> T: Deserialize<'de> + Debug, 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 mut 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
impl Clone for AsyncEventEmitter
source§fn clone(&self) -> AsyncEventEmitter
fn clone(&self) -> AsyncEventEmitter
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for AsyncEventEmitter
impl Debug for AsyncEventEmitter
source§impl Default for AsyncEventEmitter
impl Default for AsyncEventEmitter
source§fn default() -> AsyncEventEmitter
fn default() -> AsyncEventEmitter
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !RefUnwindSafe for AsyncEventEmitter
impl Send for AsyncEventEmitter
impl Sync for AsyncEventEmitter
impl Unpin for AsyncEventEmitter
impl !UnwindSafe for AsyncEventEmitter
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