pub struct ParallelEventEmitter<K: EventKey = String> { /* private fields */ }
Expand description
Parallel Event Emitter
Listeners added to the emitter will be invoked in a thread pool concurrently.
Implementations§
Source§impl<K: EventKey> ParallelEventEmitter<K>
impl<K: EventKey> ParallelEventEmitter<K>
Sourcepub fn new() -> ParallelEventEmitter<K>
pub fn new() -> ParallelEventEmitter<K>
Creates a new ParallelEventEmitter
with the default CpuPool
Sourcepub fn with_pool(pool: CpuPool) -> ParallelEventEmitter<K>
pub fn with_pool(pool: CpuPool) -> ParallelEventEmitter<K>
Creates a new ParallelEventEmitter
with an already existing CpuPool
instance.
This allows for custom thread preferences and lifecycle hooks.
Sourcepub fn event_names(&self) -> EventResult<Vec<K>>
pub fn event_names(&self) -> EventResult<Vec<K>>
Collect the names of all events being listened for.
Unfortunately, this method locks a mutex on an internal structure, so an iterator cannot be returned.
Sourcepub fn event_names_visitor<F>(&self, visitor: F) -> EventResult<()>
pub fn event_names_visitor<F>(&self, visitor: F) -> EventResult<()>
As an alternative to cloning all the event names and collecting them into a Vec
,
like in event_names
,
a visitor callback can be used to iterate all the event names more efficiently.
Sourcepub fn add_listener<F, E: Into<K>>(
&mut self,
event: E,
cb: F,
) -> EventResult<ListenerId>
pub fn add_listener<F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
Add a simple listener callback that does not accept any arguments
The return value of this is a unique ID for that listener, which can later be used to remove it if desired.
Sourcepub fn once<F, E: Into<K>>(
&mut self,
event: E,
cb: F,
) -> EventResult<ListenerId>
pub fn once<F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
Like add_listener
, but the listener will be removed from the event emitter after a single invocation.
Sourcepub fn add_listener_value<T, F, E: Into<K>>(
&mut self,
event: E,
cb: F,
) -> EventResult<ListenerId>
pub fn add_listener_value<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
Add a listener that can accept a value passed via emit_value
, or emit_value_sync
if T
is Clone
If no value or an incompatible value was passed to emit*
, None
is passed.
The return value of this is a unique ID for that listener, which can later be used to remove it if desired.
Sourcepub fn once_value<T, F, E: Into<K>>(
&mut self,
event: E,
cb: F,
) -> EventResult<ListenerId>
pub fn once_value<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
Like add_listener_value
, but the listener will be removed from the event emitter after a single invocation.
Sourcepub fn add_listener_sync<T, F, E: Into<K>>(
&mut self,
event: E,
cb: F,
) -> EventResult<ListenerId>
pub fn add_listener_sync<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
Variation of add_listener_value
that accepts Sync
types,
where intermediate copies on emit*
are unnecessary.
This will attempt to use a reference to the original
value passed to emit_value_sync
. If a value of T
was passed via emit_value
,
the callback will be invoked with the Clone
d copy.
There is nothing statically forcing the use of this instead of add_listener_value
,
but it is here just in case your type T
is Sync
but might not implement Clone
,
or if you want to avoid cloning values all over the place.
The return value of this is a unique ID for that listener, which can later be used to remove it if desired.
Sourcepub fn once_sync<T, F, E: Into<K>>(
&mut self,
event: E,
cb: F,
) -> EventResult<ListenerId>
pub fn once_sync<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
Like add_listener_sync
, but the listener will be removed from the event emitter after a single invocation.
Sourcepub fn remove_listener<E: Into<K>>(
&mut self,
event: E,
id: ListenerId,
) -> EventResult<bool>
pub fn remove_listener<E: Into<K>>( &mut self, event: E, id: ListenerId, ) -> EventResult<bool>
Removes a listener with the given ID and associated with the given event.
If the listener was not found (either doesn’t exist or the wrong event given) Ok(false)
is returned.
If the listener was removed, Ok(true)
is returned.
Sourcepub fn remove_any_listener(&mut self, id: ListenerId) -> EventResult<bool>
pub fn remove_any_listener(&mut self, id: ListenerId) -> EventResult<bool>
Exhaustively searches through ALL events for a listener with the given ID.
Ok(false)
is returned if it was not found.
Sourcepub fn emit<E: Into<K>>(
&mut self,
event: E,
) -> BoxFuture<usize, Trace<EventError>>
pub fn emit<E: Into<K>>( &mut self, event: E, ) -> BoxFuture<usize, Trace<EventError>>
Emit an event, invoking all the listeners for that event in the thread pool concurrently.
The Future
returned by emit
resolves to the number of listeners invoked,
and any errors should be forwarded up.
Sourcepub fn emit_value<T, E: Into<K>>(
&mut self,
event: E,
value: T,
) -> BoxFuture<usize, Trace<EventError>>
pub fn emit_value<T, E: Into<K>>( &mut self, event: E, value: T, ) -> BoxFuture<usize, Trace<EventError>>
Emit an event, invoking all the listeners for that event in the thread pool concurrently.
A copy of the value will be passed to every listener.
The Future
returned by emit_value
resolves to the number of listeners invoked,
and any errors should be forwarded up.
Sourcepub fn emit_value_sync<T, E: Into<K>>(
&mut self,
event: E,
value: T,
) -> BoxFuture<usize, Trace<EventError>>
pub fn emit_value_sync<T, E: Into<K>>( &mut self, event: E, value: T, ) -> BoxFuture<usize, Trace<EventError>>
Variation of emit_value
for Sync
types, where intermediate copies are unnecessary.
All listeners receive a reference to the same value.
The Future
returned by emit_value_sync
resolves to the number of listeners invoked,
and any errors should be forwarded up.