Skip to content

ChocolateMilkDev/emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@chocolatemilkdev/emitter

a basic event emitter.

Image Image

Features

  • Builded on ESM, CJS and UMD.
  • Type-Safe.
  • Optimized.
  • Basic Usage.

Note:

  • ES6/ES2015 or newer required.

1.0.1

  • once function bug fix
  • typing fix.
  • added onceAsync function

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

Releases

No releases published

Packages

No packages published