EventEmitter

Crates.ioEventEmitter
lib.rsEventEmitter
version0.0.4
sourcesrc
created_at2023-05-26 06:40:50.461455
updated_at2023-05-28 08:20:39.757155
descriptionAn EventEmitter object inspired by NodeJS.
homepagehttp://thispage.tech:9680/jclee1995/rust-eventemitter
repository
max_upload_size
id874836
size9,979
李俊才 (jacklee1995)

documentation

README

EventEmitter

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.
Commit count: 0

cargo fmt