a basic event emitter.
- Builded on ESM, CJS and UMD.
- Type-Safe.
- Optimized.
- Basic Usage.
- ES6/ES2015 or newer required.
- once function bug fix
- typing fix.
- added onceAsync function
import ChocolateMilkEmitter from "@chocolatemilkdev/emitter";//ESM and TS
/**
* const ChocolateMilkEmitter = require("@chocolatemilkdev/emitter").default
*/
const emitter = new ChocolateMilkEmitter()
emitter.on("a", (number) => {
console.log(number)
})
emitter.once("b", (str) => {
console.log(str)
})
emitter.onceAsync("b").then(([bool]) => {
console.log(bool)
})
emitter.emit("a", 1)
emitter.emit("b", "b")
emitter.emit("c", true)
emitter.hasListener("a", (number) => {
console.log(number)
})//true
emitter.isEmitted("a")//true
interface MyEvents{
eat(food: string): any;
drink(drink: string): string;
}
const emitter = new ChocolateMilkEmitter<MyEvents>()
emitter.on("eat", (food) => {
console.log(food)
})
emitter.on("drink", (drink) => {
return "a"
})
emitter.emit("eat", "kebap")//undefined
emitter.emit("drink", "water")//"a"