Non-Printable Material

    tk-event-emitter
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.2 • Public • Published

    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

    Install

    npm i tk-event-emitter

    DownloadsWeekly Downloads

    6

    Version

    0.2.2

    License

    MIT

    Unpacked Size

    19.6 kB

    Total Files

    10

    Last publish

    Collaborators

    • chenjk110