Struct emitter_rs::EventEmitter
source · pub struct EventEmitter {
pub listeners: HashMap<String, Vec<Listener>>,
}Fields§
§listeners: HashMap<String, Vec<Listener>>Implementations§
source§impl EventEmitter
impl EventEmitter
pub fn new() -> Self
sourcepub fn on<F, T>(&mut self, event: &str, callback: F) -> String
pub fn on<F, T>(&mut self, event: &str, callback: F) -> String
Adds an event listener with a callback that will get called whenever the given event is emitted. Returns the id of the newly added listener.
§Example
use emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();
// This will print <"Hello world!"> whenever the <"Some event"> event is emitted
// The type of the `value` parameter for the closure MUST be specified and, if you plan to use the `value`, the `value` type
// MUST also match the type that is being emitted (here we just use a throwaway `()` type since we don't care about using the `value`)
event_emitter.on("Some event", |value: ()| println!("Hello world!"));sourcepub fn emit<T>(&mut self, event: &str, value: T) -> Vec<JoinHandle<()>>where
T: Serialize,
pub fn emit<T>(&mut self, event: &str, value: T) -> Vec<JoinHandle<()>>where
T: Serialize,
Emits an event of the given parameters and executes each callback that is listening to that event asynchronously by spawning a new thread for each callback.
§Example
use emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();
// Emits the <"Some event"> event and a value <"Hello programmer">
// The value can be of any type as long as it implements the serde Serialize trait
event_emitter.emit("Some event", "Hello programmer!");sourcepub fn remove_listener(&mut self, id_to_delete: &str) -> Option<String>
pub fn remove_listener(&mut self, id_to_delete: &str) -> Option<String>
Removes an event listener with the given id
§Example
use emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();
let listener_id = event_emitter.on("Some event", |value: ()| println!("Hello world!"));
// Removes the listener that we just added
event_emitter.remove_listener(&listener_id);sourcepub fn on_limited<F, T>(
&mut self,
event: &str,
limit: Option<u64>,
callback: F
) -> String
pub fn on_limited<F, T>( &mut self, event: &str, limit: Option<u64>, callback: F ) -> String
Adds an event listener that will only execute the listener x amount of times - Then the listener will be deleted. Returns the id of the newly added listener.
§Example
use emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();
// Listener will be executed 3 times. After the third time, the listener will be deleted.
event_emitter.on_limited("Some event", Some(3), |value: ()| println!("Hello world!"));
event_emitter.emit("Some event", ()); // 1 >> "Hello world!"
event_emitter.emit("Some event", ()); // 2 >> "Hello world!"
event_emitter.emit("Some event", ()); // 3 >> "Hello world!"
event_emitter.emit("Some event", ()); // 4 >> <Nothing happens here because listener was deleted after the 3rd call>sourcepub fn once<F, T>(&mut self, event: &str, callback: F) -> String
pub fn once<F, T>(&mut self, event: &str, callback: F) -> String
Adds an event listener that will only execute the callback once - Then the listener will be deleted. Returns the id of the newly added listener.
§Example
use emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();
event_emitter.once("Some event", |value: ()| println!("Hello world!"));
event_emitter.emit("Some event", ()); // First event is emitted and the listener's callback is called once
// >> "Hello world!"
event_emitter.emit("Some event", ());
// >> <Nothing happens here since listener was deleted>sourcepub fn sync_emit<T>(&self, _event: &str, _value: T)where
T: Serialize,
pub fn sync_emit<T>(&self, _event: &str, _value: T)where
T: Serialize,
NOT IMPLEMENTED! Emits an event of the given parameters in a synchronous fashion. Instead of executing each callback in a newly spawned thread, it will execute each callback in the order that they were inserted.
§Example
use emitter_rs::EventEmitter;
let mut event_emitter = EventEmitter::new();
event_emitter.on("Some event", |value: ()| println!("1")); // Guaranteed to be executed first
event_emitter.on("Some event", |value: ()| println!("2")); // Will not execute this until the first callback has finished executing
event_emitter.on("Some event", |value: ()| println!("3")); // Will not execute this until the second callback has finished executing
// Emits the <"Some event"> event and a value <"Hello programmer">
// The value can be of any type
event_emitter.sync_emit("Some event", "Hello programmer!");