JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q21063F
  • License ISC

Object based/oriented event emitter/dispatcher for use with TypeScript or JS

Package Exports

  • advanced-event-dispatcher
  • advanced-event-dispatcher/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (advanced-event-dispatcher) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

github CI

advanced-event-dispatcher

Object based/oriented event dispatcher/emitter for use with TypeScript or JS.

It uses class type of event as an event target to add event handlers and an instance of the event will be passed to an event handler when it will be dispatched. So an event and its data contained in the same object instance.

The module uses modern browser API and if you want to launch you code in the old browsers, probably you need to add polyfills for Map class.

Install

npm i advanced-event-dispatcher

Import

//es5
const {BaseEvent, EventDispatcher} = require("advanced-event-dispatcher");
//es6+ and ts
import {BaseEvent, EventDispatcher} from "advanced-event-dispatcher";

Define an event

//Declare an event
class Event extends BaseEvent {
    constructor(readonly message: string = "none") {
        super();
    }
}

Create instance of event dispatcher

const dispatcher = new EventDispatcher();

Add event handler

const handler = (e:Event) => console.log(e.message);

dispatcher.addEventHandler(Event, handler);

Dispatching events

dispatcher.dispatchEvent(new Event("Hello from advanced-event-dispatcher!"));

Remove event handler

dispatcher.removeEventHandler(Event, handler);
dispatcher.dispatchEvent(new Event("This event will not be handled"));

EventBus example

class EventBus extends EventDispatcher {
    static readonly instance = new EventBus();
}

class Observer {
    constructor() {
        EventBus.instance.addEventHandler(Event, this.onEvent, this);
    }

    onEvent(e:Event) {
        console.log(this.constructor.name, ":", e.message);
    }
}

const c = new Observer();
EventBus.instance.dispatchEvent(new Event("Hello from EventBus!"));

Additional examples

See full examples code, including JavaScript, here