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

he-event-system

v2.0.1

Published

register and fire events with promises returns without EventEmmiter

Downloads

30

Readme

he-event-system

@licence GPL.v3

* v2 *

this version is a completely recreated lib so most of APIs changed

please be careful when upgrading.

Install

npm install he-event-system --save

quick How to use

@notice all paths must be relative to the root path of the project

first you should define an Event class like this

./path/to/Event.js

class Event {
  /**
   * @desc  the event class is a holder to all information that listeners need to accomplish thier tasks
   *        and may be contain any required methods or params.
   * @param {Array<Any>} args [array of any arguments passed to the event instance]
   */
  constructor(arg1, arg2, arg3) {
    // any setup for the event
    this.args = args;
  }
}
module.exports = Event;

then you should define at least one Listener Class to this event

./path/to/listener.js

class Listener {
  /**
   * any pre setup before running handler()
   */
  constructor() {}
  
  /**
   * @desc   the main method that hold logic for the listener, it may be
   *         contain any methods or proberties that needed.
   * @param  {Object} event [the event Instance of the Event class]
   * @return {Promise<Any>}
   */
  handler(event) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('hello from listener one');
      });

      /**
       * here we called resolve outside the setTimeout
       * so the listener will run Async
       * if we need the listeners to run in order, just change
       * the place of resolve() and put it inside the Async operation.
       */
      resolve();
    });
  }
}
module.exports = Listener;

finally we start creating the Dispatcher Instance

const DispatcherCore = require('he-event-system');

const EventsListeners = [
    "./path/to/Event": [
      "./path/to/listener1",
      "./path/to/listener2",
      ...
    ],
    "./path/to/Event2": [
      "./path/to/listener1",
      "./path/to/listener2",
      ...
    ]
];

/**
 * create new instance of DispatcherCore
 * @param {Object} EventsListeners [optional -> you can pass object contains all your events and listeners instead of passing them individually]
 */
const Dispatcher = new DispatcherCore(EventsListeners);

add more than listener at once

Dispatcher.addListeners('./path/to/event', [
    './path/to/listener1',
    './path/to/listener2'
]);

add listeners one by one Dispatcher.addListener('./path/to/event', './path/to/listener');

remove one listener Dispatcher.removeListener('./path/to/event', './path/to/listener');

remove all listeners. Dispatcher.removeListeners('./path/to/event');

alias for removeListeners() Dispatcher.stop('./path/to/event');

grab all listeners Dispatcher.getListeners('./path/to/event');

get the number of listeners for an event Dispatcher.countListeners('./path/to/event');

dispatch an event

this will dispatch all listeners registered with this event in Order every listener can modify the next one (if it runs sync) or stop the loop the final data returned is the data that all listeners modify it (if listeners run sync) the err is the first err returned by any listener

Dispatcher.fire('./path/to/event', 'argOne', 'argTwo', 'argThree').then((data) => {

    console.log('success -> ' + data);

}, (err) => {

    console.log('err -> ' + err);

});

the main object that contains all events with listeners Dispatcher.events;

I'm Welcoming with any comment or advise or you can open new issue on github

Todo List

  1. add Support for Logging and Logging Level (errors, warnings, successes)