Struct ParallelEventEmitter

Source
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>

Source

pub fn new() -> ParallelEventEmitter<K>

Creates a new ParallelEventEmitter with the default CpuPool

Source

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.

Source

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.

Source

pub fn event_names_visitor<F>(&self, visitor: F) -> EventResult<()>
where F: Fn(&K),

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.

Source

pub fn add_listener<F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
where F: Fn() -> EventResult<()> + 'static,

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.

Source

pub fn once<F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
where F: Fn() -> EventResult<()> + 'static,

Like add_listener, but the listener will be removed from the event emitter after a single invocation.

Source

pub fn add_listener_value<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
where T: Any + Clone + Send, F: Fn(Option<T>) -> EventResult<()> + 'static,

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.

Source

pub fn once_value<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
where T: Any + Clone + Send, F: Fn(Option<T>) -> EventResult<()> + 'static,

Like add_listener_value, but the listener will be removed from the event emitter after a single invocation.

Source

pub fn add_listener_sync<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
where T: Any + Send + Sync, F: Fn(Option<&T>) -> EventResult<()> + 'static,

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 Cloned 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.

Source

pub fn once_sync<T, F, E: Into<K>>( &mut self, event: E, cb: F, ) -> EventResult<ListenerId>
where T: Any + Send + Sync, F: Fn(Option<&T>) -> EventResult<()> + 'static,

Like add_listener_sync, but the listener will be removed from the event emitter after a single invocation.

Source

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.

Source

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.

Source

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.

Source

pub fn emit_value<T, E: Into<K>>( &mut self, event: E, value: T, ) -> BoxFuture<usize, Trace<EventError>>
where T: Any + Clone + Send,

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.

Source

pub fn emit_value_sync<T, E: Into<K>>( &mut self, event: E, value: T, ) -> BoxFuture<usize, Trace<EventError>>
where T: Any + Send + Sync,

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.

Trait Implementations§

Source§

impl<K: EventKey> Default for ParallelEventEmitter<K>

Source§

fn default() -> ParallelEventEmitter<K>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.