1.0.0 • Published 5 months ago

@anderpang/event-emitter v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

EventEmitter

Install

npm install @anderpang/event-emitter

Usage

import

  import EventEmitter from "@anderpang/event-emitter";

or

<script src="@anderpang/event-emitter/dist/EventEmitter.js"></script>

Example

  import EventEmitter from "@anderpang/event-emitter";

   var emitter=new EventEmitter();

        emitter.on("abc",function(a,b,c){
            console.log(a,b,c);
        });

        emitter.once("abc",function(a,b,c){
            console.log("once:",a,b,c);
        });

        setTimeout(function(){
            emitter.emit("abc","a1",{b:2},{hello:"World"});
            emitter.emit("abc","a2",{b:22},{hello:"World2"});
        },500);

        /** print:
        a1 {b: 2} {hello: 'World'}
        ------------------------------
        once: a1 {b: 2} {hello: 'World'}
        ----------------------------------
        a2 {b: 22} {hello: 'World2'}
        ----------------------------------
         * */

Constructor

type EventEmitterFunction = (a?: any, b?: any, c?: any) => void;

export default class EventEmitter {
    on(type: string, f: EventEmitterFunction): this;
    once(type: string, f: EventEmitterFunction): this;
    off(type: string, f?: EventEmitterFunction): this;
    emit(type: string, a?: any, b?: any, c?: any): this;
}
1.0.0

5 months ago