4个版本
0.0.4 | 2023年5月28日 |
---|---|
0.0.3 | 2023年5月28日 |
0.0.2 | 2023年5月28日 |
0.0.1 | 2023年5月26日 |
219 在 无标准库 中排名
每月33次下载
9KB
74 行
EventEmitter
受NodeJS启发的EventEmitter对象。
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
}
输出
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.