@lvchengbin/event-emitter

An event emitter for both browsers and nodejs

Stats

StarsIssuesVersionUpdatedCreatedSize
@lvchengbin/event-emitter
042.0.0a year ago4 years agoMinified + gzip package size for @lvchengbin/event-emitter in KB

Readme

EventEmitter

An implementation of EventEmitter for browsers which can use an Object as the event type.

Installation

$ npm i @lvchengbin/event-emitter --save

If you want to invoke the code to browers with <script> tag, please use event-emitter.js. For old browsers not support ES5 syntax, please use event-emitter.bc.js.

Usage

import EventEmitter from '@lvchengbin/event-emitter';

const em = new EventEmitter();

const handler = () => {
    // some code...
};

em.on( 'event', handler );
em.emit( 'event', ...args );
em.removeListener( 'event', handler );
import EventEmitter from '@lvchengbin/event-emitter';

class A extends EventEmitter {
    constructor() {
        super();

        this.on( 'xxx', () => {
            // some code...
        } );
    }
}

new A().on( 'msg', () => {
} );

Using an Object as the event type.

const em = new EventEmitter();

const func = {};

em.on( func, () => {
} );

Methods

  • on( evt, handler )

    Start to listen to an event type.

  • once( evt, handler )

    Start to listen to an event type only once, then the listener will be removed.

  • removeListener( evt, handler )

    Remove lister of an event type to stop listening it.

  • emit( evt, ...args )

    To trigger an event

  • removeAllListeners( [ evt ] )

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.