Skip to content

chenjk110/tk-event-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TKEventEmitter

  • version: 0.2.2
  • update: 2022.2.25

API

class TKEventEmitter
.eventNames: (string | symbol)[]
.addEventListener (eventName: string | symbol, listener: Function): this
.removeAllListeners (eventName?: string | symbol): void
.removeListener (eventName: string | symbol, listener: Function): boolean
.listenerCount (eventName: string | symbol): number
.listeners (eventName: string | symbol): Function[]
.emit (eventName: string | symbol, ...args: any[]): this
.off (eventName: string | symbol, listener: Function): boolean
.on (eventName: string | symbol, listener: Function): this
.once (eventName: string | symbol, listener: Function): this

Examples

1. on and emit

const emitter = new TKEventEmitter();

emitter.on("update", (id) => {
  console.log("task: " + id + "update completed!");
});

emitter.emit("update", 12);

2. once and emit

const handler = () => {
  console.log("Hello World!");
};

emitter.once("only-one", handler);

emitter.emit("only-one"); // 'Hello World'

emitter.emit("only-one"); // nothing happened

emitter.emit("only-one"); // nothing happened

3. on and off

const handler = () => {
  console.log("some message...");
};

emitter.on("run", handler);

emitter.emit("run"); // 'some message...'

emitter.off("run", handler);

emitter.emit("run"); // nothing happened

About

custom events emitter inspired by Events module of Node.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published