4 releases

0.0.4 May 28, 2023
0.0.3 May 28, 2023
0.0.2 May 28, 2023
0.0.1 May 26, 2023

#335 in No standard library

33 downloads per month

MIT license

9KB
74 lines

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.

No runtime deps

Features