npm.io
1.0.0 • Published 4 years ago

@pdw.io/event-dispatcher

Licence
MIT
Version
1.0.0
Deps
0
Size
13 kB
Vulns
0
Weekly
0

EventDispatcher

Performant, opinionated event dispatcher.

Installation

npm install @pdw.io/event-dispatcher

Usage

Below is a very minimal example. The main thing to note is that the "event names" are strictly numbers (Enum keys in this case), this is where the performance comes from.

import { EventDispatcher } from '@pdw.io/eventdispatcher';

export enum DialogEventName {
  OPEN,
  CLOSE,
}

class Dialog extends EventDispatcher<DialogEventName> {
  private open = false;

  toggle() {
    this.open = !this.open;
    const eventToSend = this.open ? DialogEventName.OPEN : DialogEventName.CLOSE;
    this.dispatchEvent(eventToSend);
  }
}

const dialog = new Dialog();
dialog.addEventListener(DialogEventName.OPEN, callback);

Keywords