npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

event-dispatching

v0.0.6

Published

Dispatching and listening for application events in Typescript

Downloads

150

Readme

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