use std::{
boxed::Box,
future::Future,
pin::Pin
};
#[cfg(not(feature = "threadsafe"))]
type EventHandler<'a,A> = Box<dyn FnMut( &A ) -> Pin<Box<dyn Future<Output=()> + 'a>> + 'a>;
#[cfg(feature = "threadsafe")]
type EventHandler<'a,A> = Box<dyn FnMut( &A ) -> Pin<Box<dyn Future<Output=()> + 'a>> + Send + 'a>;
pub struct Event<'a,A> {
handlers: Vec<EventHandler<'a,A>>
}
impl<'a,A> Event<'a,A> {
pub(in crate) fn invoke( &mut self, args: &A ) {
for h in self.handlers.iter_mut() {
h( args );
}
}
#[cfg(not(feature = "threadsafe"))]
pub fn register<H>( &mut self, mut handler: H ) where
H: FnMut( &A ) + 'a
{
self.handlers.push(Box::new(move |args| {
handler( args );
Box::pin(async {})
}));
}
#[cfg(feature = "threadsafe")]
pub fn register<H>( &mut self, mut handler: H ) where
H: FnMut( &A ) + Send + 'a
{
self.handlers.push(Box::new(move |args| {
handler( args );
Box::pin(async {})
}));
}
#[cfg(not(feature = "threadsafe"))]
pub fn register_async<H,F>( &mut self, mut handler: H ) where
H: FnMut( &A ) -> F + 'a,
F: Future<Output=()> + 'a
{
self.handlers.push(Box::new(move |args| Box::pin( handler( args ) ) ) );
}
#[cfg(feature = "threadsafe")]
pub fn register_async<H,F>( &mut self, mut handler: H ) where
H: FnMut( &A ) -> F + Send + 'a,
F: Future<Output=()> + 'a
{
self.handlers.push(Box::new(move |args| Box::pin( handler( args ) ) ) );
}
}
impl<'a,A> Default for Event<'a,A> {
fn default() -> Self {
Self {
handlers: Vec::new()
}
}
}