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;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct EventArgs<'a> {
id: u32,
message: &'a str,
}
let mut event = AsyncEvent::<EventArgs>::new();
event.add(|args| async move {
println!("Event invoked with args: {:?}", args);
assert_eq!(args, EventArgs {id: 0, message: ""});
});
let arg = EventArgs {id: 0, message: ""};
event.invoke_async(arg).await;
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 mut 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 invoked.
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 invoked");
});
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 invoked");
});
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: TEventArgs)where
TEventArgs: Clone,
pub async fn invoke_async(&self, arg: TEventArgs)where
TEventArgs: Clone,
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: TEventArgs,
) -> JoinAll<impl Future<Output = ()> + 'a>where
TEventArgs: Clone,
pub fn invoke_parallel_async(
&self,
arg: TEventArgs,
) -> JoinAll<impl Future<Output = ()> + 'a>where
TEventArgs: Clone,
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