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

eventish

v1.0.3

Published

A flexible wrapper over NodeJS raw events emitter API.

Downloads

6

Readme

Eventish is a lightweight NodeJS library, wrapping over the low-level EventEmitter API for handling events. Set up your events using different formats and trigger them in any convenient format of your choice

It was inspired by Adebayoh Shalvah's burns library for event set-up and broadcasting.

What you get

  • A flexible wrapper over raw NodeJS EventEmitter API
  • Asychronous handling of events i.e if you so choose
  • The ability to add event handlers in different formats and any where in your code
  • Default event handlers for a group of events
  • The ability to trigger events in any format of your choice

How to use

npm install --save eventish
const eventish = require('eventish');

Define an event handler:

// event_handlers/member.js

function send_new_member_email(data){
    mailer.send_email(`Hello ${data.name}, thanks for joining our community. `);
}

function send_password_email(data){
    mailer.send_email(` Hi ${data.name}, please follow this link to set up your password `)
}

function send_privacy_update_email(data){
    mailer.send_email(` Hello ${data.name}, we updated our privacy policy based on GDPR issues`);
}

Register the event and attach the handler:

var new_member = require('./event_handlers/member');

// we can use an object of events and their corresponding handlers
eventish.setEvent({new_member:member.send_new_member_email,privacy_policy:member.send_privacy_update_email});

// we can also pass two arguments to the the eventish.setEvent method, any of the event an its array of handlers or the event and its single handler or an array of events and their generic handler. They are shown below:

eventish.setEvent('new_member',member.send_new_member_email); // event and its handler

eventish.setEvent('new_member',[member.send_new_member_email, member.send_password_email]) // an event and its array of handlers

eventish.setEvent(['new_member_password','old_member_password_update'], member.send_password_email);

You can trigger the events any time any where in your code.

eventish.trigger_event('new_member', {
    name: new_member.name, 
    id: new_member.id
});

Setting up events

Setting up events is very easy. You call the set_event method with an event name with the corresponding handler function in any of the following forms.

eventish.setEvent('new_member',[member.send_new_member_email, member.send_password_email]) 

eventish.setEvent('new_member',member.send_new_member_email); // event and its handler

eventish.setEvent({new_member:[member.send_new_member_email],privacy_policy:member.send_privacy_update_email});

Handlers definition

Handlers are the agents that do work whenever any of the events you set up are triggered. They are normal functions that receive optional data arguments on triggering of the event.

function send_new_member_email(data){
  mailer.send_email(` Welcome to the brother hood ${data.name}`);
  console.log(data);
}

Setting a default event handler for a group of events

It is possible to specify a all_events_handler. Eventish will set an event handler for a group of events in an array

function all_events_handler(data){ 
  console.log(data)
}
eventish.set_event(['new_member','old_member','password_rest'],all_events_handler);

// when you trigger the above events, the `all_events_handler` would be called

Triggering events

In order to trigger an event, call trigger_event with the name of the event:

eventish.trigger_event('new_member');

You may also pass in a payload containing data to be transmitted with the event:

eventish.trigger_event('new_member', {
    name:'Ajah',
    id: 7138,
});

or an array of events may be passed in at ones

  eventish.trigger_event(['event1','event2','event3']);

or you may pass in an object of events and their corresponding data as follows:

  eventish.trigger_event({event1:{name:'ajah'}, event2:['apples','oranges'], event3:'ajah'});

Asynchronous form

It is possible to pass an optional options object as a parameter to set if you want the handler to be called asynchronously. That feature is yet to be implemented

Contributing

In case you have any ideas, features you would like to be included or any bug fixes, you can send a PR.

(Requires Node v6 or above)

  • Clone the repo
git clone https://github.com/ChukwuEmekaAjah/eventish.git

Work still to be done

  • add support for asychronous handler calling with an options object