@chocolatemilkdev/emitter
v1.0.7
Published
a basic event emitter.
Downloads
12
Readme
@chocolatemilkdev/emitter
a basic event emitter.
Features
- Builded on ESM, CJS and UMD.
- Type-Safe.
- Optimized.
- Basic Usage.
Note:
- ES6/ES2015 or newer required.
1.0.1
- bug fix
Usage
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
Note: Typing Events
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"
Contact/Support
Discord