Giter VIP home page Giter VIP logo

event-dispatching's Introduction

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

event-dispatching's People

Contributors

mduvernon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.