Skip to content

Latest commit

 

History

History
138 lines (99 loc) · 3.94 KB

README.md

File metadata and controls

138 lines (99 loc) · 3.94 KB

event-dispatching

Task

Allows to register subscribers and dispatch events across the application by using the dependency injector Inversify.

Disclaimer

Updated for personal use this package is an update (or refactoring) of the pleerock/event-dispatch module

Installation

  1. Install module:

    npm install event-dispatching --save

  2. ES6 features are used, so you may want to install es6-shim too:

    npm install es6-shim --save

    if you are building nodejs app, you may want to require("es6-shim"); in your app. or if you are building web app, you man want to add <script src="path-to-shim/es6-shim.js"> on your page.

Usage

Inversify DI and "event-dispatching" usage or declaration

Some of the package usages may be found inside the "sample" directory

import { Container } from "inversify";
import eventDispatching, { TYPES } from "event-dispatching";
import "es6-shim"; // -- Not required --

// create container from inversify module
const container = new Container();

// Import and bootstrap the "event-dispatching" module bootstraper
eventDispatching({ container });

// Call the event dispatcher service
const eventDispatcher = container.get<EventDispatcher>(TYPES.EventDispatcher);

Classes and anotations on the required classes or methods

Simply create a class and put annotations on its methods:

import { injectable, inject } from "inversify";
import { EventSubscriber, On } from "event-dispatching";
import { AWESOME_TYPES as TYPES } from "my-awesome-module";

@EventSubscriber()
@injectable()
export class UserEventSubscriber {
  private _myAwesomeStatusProvider: AwesomeStatusProvider;

  constructor(
    @inject(TYPES.AwesomeStatusProvider)
    _awesomeStatusProvider: AwesomeStatusProvider
  ) {
    this._myAwesomeStatusProvider = _awesomeStatusProvider;
  }

  @On("onUserCreate")
  onUserCreate(user: User) {
    console.log("User " + user.name + " created!");
  }

  @On("onStatusUpdate")
  updateUserStatus(status: string) {
    console.log("New status: " + status);
  }

  @On("onAsyncStatusUpdate")
  async onAsyncStatusUpdate(): Promise<{ status: any }> {
    const status: any = await this._myAwesomeStatusProvider.loadMyAwesomeStatus();

    return {
      status,
    };
  }
}

Then use EventDispatcher class to dispatch events:

import { EventDispatcher, TYPES } from "event-dispatching";
import { UserEventSubscriber } from "./subscriber/UserEventSubscriber";

// note that all your subscribers must be imported somewhere in the app, so they are getting registered
// on node you can also require the whole directory using [require all](https://www.npmjs.com/package/require-all) package

container
  .bind<UserEventSubscriber>(Symbole.for("UserEventSubscriber"))
  .to(UserEventSubscriber);

// Call the event dispatcher service
const eventDispatcher: EventDispatcher = container.get<EventDispatcher>(
  TYPES.EventDispatcher
);

// Dispatch with Object
eventDispatcher.dispatch("onUserCreate", new User("Johny"));

// Dispatch with simple string
eventDispatcher.dispatch("onStatusUpdate", "hello world");

// Dispatch asynchronously
eventDispatcher
  .asyncDispatch("onAsyncStatusUpdate")
  .then((result: any[]) => {
    for (const key in result) {
      const data = result[key];

      if (data.status) {
        console.log("The actual loaded status is: " + data.status);
      }
    }
  })
  .catch((error) => {
    console.log("Something went wrong while loading the status");
  });

Samples

Take a look on samples in ./sample for more examples of usages.

Todos

  • Make more samples
  • Make it more general with ioc adapter to include any kind of DI
  • cover with tests
  • more documentation