1.0.9 • Published 3 years ago

@nebrob/event-emitter v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Workflow Passing Minified Size

Event Emitter

A tiny ES6 event emitter

Install

  • Using yarn yarn add @nebrob/event-emitter
  • Using npm npm i @nebrob/event-emitter

Usage

Standalone

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

const eventEmitter = new EventEmitter();

eventEmitter.on('eventName', (arg1, arg2) => {
    console.log(arg1, arg2) // Output: "arguement_1", "arguement_2"
})

eventEmitter.emit('eventName', 'arguement_1', 'argument_2', ...);

Sub-class

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

class MyClass extends EventEmitter {
    constructor() {
        super();
    }

    trigger() {
        this.emit('eventName', 'argument_1', 'argument_2', ...);
    }
}

const clazz = new MyClass();

clazz.on('eventName', (arg1, arg2) => {
    console.log(arg1, arg2) // Output: "arguement_1", "arguement_2"
})

clazz.trigger();

Methods

All methods are available and documented in dist/index.d.ts

export default class EventEmitter {
    listeners: Map<string, Set<Function | undefined>>;

    constructor();

    /**
     * Emit an event with a given payload
     */
    emit(eventName: string, ...eventPayload: any[]): this;

    /**
     * Attach an event listener
     */
    on(eventName: string, eventListener: Function): this;

    /**
     * Attach an event listener that will be triggered only once
     */
    once(eventName: string, eventListener: Function): this;

    /**
     * Detach an event listener if provided, all if not
     */
    off(eventName: string, eventListener?: Function): this;
}

Development

This project is using microbundle for building, and Jest for tests.

  • Run yarn install to install development dependencies.
  • Run yarn dev to run microbundle as development mode (enable watching file changes)

Build

  • Run yarn test to run all the tests.
  • Run yarn build to make a production build.
1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago