Crates.io | EventEmitter |
lib.rs | EventEmitter |
version | 0.0.4 |
source | src |
created_at | 2023-05-26 06:40:50.461455 |
updated_at | 2023-05-28 08:20:39.757155 |
description | An EventEmitter object inspired by NodeJS. |
homepage | http://thispage.tech:9680/jclee1995/rust-eventemitter |
repository | |
max_upload_size | |
id | 874836 |
size | 9,979 |
An EventEmitter object inspired by NodeJS.
use std::sync::{Arc};
use EventEmitter::EventEmitter;
fn main() {
let emitter = EventEmitter::new();
let callback1 = Arc::new(|| println!("[event1 emitted]: The first callback of event1 has been called."));
let callback2 = Arc::new(|| println!("[event1 emitted]: The second callback of event1 has been called."));
let callback3 = Arc::new(|| println!("[event2 emitted]: The only one callbask of event2 has been called."));
// Add event listener
emitter.on("event1", callback1);
emitter.on("event1", callback2);
emitter.on("event2", callback3);
let ct1 = emitter.listener_count("event1");
let ct2 = emitter.listener_count("event2");
println!("Number of Listeners for event1 is: {ct1}, \nNumber of Listeners for event2 is: {ct2}");
emitter.emit("event1"); // Emit event1
emitter.emit("event2"); // Emit event1
}
outputs:
Number of Listeners for event1 is: 2,
Number of Listeners for event2 is: 1
[event1 emitted]: The first callback of event1 has been called.
[event2 emitted]: The only one callbask of event2 has been called.
[event1 emitted]: The second callback of event1 has been called.