pub struct AsyncEvent<'a, TEventArgs> { /* private fields */ }
Expand description
An asynchronous event that can have multiple handlers attached to it.
This is similar to C#’s event
keyword but designed for async/await patterns.
Handlers are stored in a slab storage for efficient access by index.
§Examples
use async_event_rs::AsyncEvent;
let mut event = AsyncEvent::<()>::new();
event.add(|args| async move {
println!("Event triggered with args: {:?}", args);
});
Implementations§
Source§impl<'a, TEventArgs> AsyncEvent<'a, TEventArgs>
impl<'a, TEventArgs> AsyncEvent<'a, TEventArgs>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty AsyncEvent
§Examples
use async_event_rs::AsyncEvent;
let event: AsyncEvent<()> = AsyncEvent::new();
Sourcepub fn add<F, Fut>(&mut self, handler: F) -> usize
pub fn add<F, Fut>(&mut self, handler: F) -> usize
Adds an event handler to the event.
The handler should be a closure that accepts a reference to the event arguments and returns a future. The future will be executed when the event is triggered.
Returns a handle that can be used to remove the handler later.
§Examples
use async_event_rs::AsyncEvent;
let mut event = AsyncEvent::<()>::new();
let handle = event.add(|args| async move {
println!("Event triggered");
});
Sourcepub fn remove(&mut self, handle: usize) -> bool
pub fn remove(&mut self, handle: usize) -> bool
Removes an event handler using its handle.
Returns true
if the handler was found and removed, false
otherwise.
§Examples
use async_event_rs::AsyncEvent;
let mut event = AsyncEvent::<()>::new();
let handle = event.add(|args| async move {
println!("Event triggered");
});
assert!(event.remove(handle));
assert!(!event.remove(handle)); // Already removed
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all event handlers.
§Examples
use async_event_rs::AsyncEvent;
let mut event = AsyncEvent::<()>::new();
event.add(|args| async move { println!("Handler 1"); });
event.add(|args| async move { println!("Handler 2"); });
event.clear(); // Remove all handlers
Sourcepub async fn invoke_async(&self, arg: &'a TEventArgs)
pub async fn invoke_async(&self, arg: &'a TEventArgs)
Invokes all event handlers sequentially (one after another).
Each handler is awaited before the next one is executed.
§Examples
use async_event_rs::AsyncEvent;
let mut event = AsyncEvent::new();
event.add(|args| async move { println!("Handler 1"); });
event.add(|args| async move { println!("Handler 2"); });
event.invoke_async(&()).await; // Execute all handlers in order
Sourcepub fn invoke_parallel_async(
&self,
arg: &'a TEventArgs,
) -> JoinAll<impl Future<Output = ()> + 'a>
pub fn invoke_parallel_async( &self, arg: &'a TEventArgs, ) -> JoinAll<impl Future<Output = ()> + 'a>
Invokes all event handlers in parallel.
All handlers are spawned concurrently and executed simultaneously.
§Examples
use async_event_rs::AsyncEvent;
let mut event = AsyncEvent::new();
event.add(|args| async move { println!("Handler 1"); });
event.add(|args| async move { println!("Handler 2"); });
event.invoke_parallel_async(&()).await; // Execute all handlers in parallel