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

eventreactor

v1.0.0

Published

EventEmitters on a syntax suger rush

Downloads

41

Readme

EventReactor, EventEmitters on a sugar rush

Build status: BuildStatus

EventReactor adds additional syntax sugar on top of your existing EventEmitters. It works on top of every EventEmitter inspired module, even EventEmitter2. The EventReactor was created to migrate repeating patterns when working EventEmitters.

New methods, API

Before you can use the EventReactor you have to initialize it. This can be done by simply calling new EventReactor(). This will extend the default EventEmitter. If you don't want the EventEmitter to automatically extend the build-in EventEmitter or only want to use a subset of it's functionality you should supply the constructor with the manual option.

var ER = new EventReactor({ manual: true });

When you invoke the EventReactor using the manual you need to manually attach the EventReactor methods to the prototype of your choosing, for example adding the EventReactor methods to the EventEmitter2 module.

var EventReactor = require('eventreactor')
  , EventEmitter2 = require('eventemitter2').EventEmitter2;

var ER = new EventReactor({ manual: true });

ER.aliases(EventEmitter2.prototype);
ER.every(EventEmitter2.prototype);
ER.either(EventEmitter2.prototype);
ER.multiple(EventEmitter2.prototype);
ER.has(EventEmitter2.prototype);
ER.defer(EventEmitter2.prototype);
ER.delay(EventEmitter2.prototype);
ER.idle(EventEmitter2.prototype);
ER.emit(EventEmitter2.prototype);

If you want to remove the EventReactor extensions you can call the destroy method. It should return the old and potentially overriden methods.

ER.destroy(EventEmitter2.prototype);

EventEmitter.every(event, event, event, callback);

Applies the same callback for all the given events. It expects that the callback is the last argument of the function and that all other arguments are the name of the events you want to listen on.

Example

You find your self applying the same error handling for timeouts, errors and other error related events. This cleans up your code nicely

EventEmitter.every('error', 'timeout', function (e) {
 console.error('(error) ', e.message);
});

EventEmitter.has(event, fn);

Check if the EventEmitter already has this function applied, if your code has multiple parts on where events can be added to your event emitter you might want to check if it's not added already. This simple helper function returns true or false.

Example

function example () {};

if (!EventEmitter.has('example', example)) {
  EventEmitter.on('example', example);
}

EventEmitter.multiple({object});

Sometimes you need to add a lot of event listeners for example when you create a net.Connection. You need to listener for error, close, connect, data, timeout and maybe even for end. That is a lot of events.

Or maybe you are already used to a observer patterns that used objects for listening instead of eventlistener based layout. Anyways, we got you covered.

Example

EventEmitter.multiple({
    error: function () { .. }
  , timeout: function () { .. }
  , connect: function () { .. }
  , close: function () { .. }
});

EventEmitter.idle(event, timeout, callback /*, argument1, argument2 ... */);

Sometimes you want to know when an event has not been fired within a specified time period. You can set an idle timer that fires off a callback for you if the event has gone missing. Once fired, the event will remove itself for you, so don't forget to set it back up after your events start up again.

Don't worry, like a good idle timer, it will reset itself when the event has been fired before the timeout has occurred.

Example

function callback (event) {
  console.log(event + " was never fired");
}

EventEmitter.idle("timeout", 100, callback);

EventEmitter.delay(event, timeout /*, argument1, argument2 .. */);

Delays the emitting of the given event. Much like setTimeout invokes the function after xxx miliseconds.

Example

EventEmitter.on('foo', function (arg, arg1) {
  console.log('args: ', arguments);
});

EventEmitter.delay('foo', 1000, 'arg1', 'arg2');

EventEmitter.defer(event /*, argument1, argument2 .. */);

Defers the emitting of the event until the current call stack has been cleared. Simular to wrapping an emit in a process.nextTick.

Example

EventEmitter.on('pewpew', function () {
  console.log('called second', arguments);
});

EventEmitter.defer('pewpew', 1, 2, 3);

console.log('called first');

Uncaught events and the any listeners

The EventReactor allows you to listen for unlistened events. These events are re-emitted under the uncaughtEvent event name. In addition to this, every emitted event is re-emitted under the *.* event name. This can be useful for debugging or logging your events.

Because these features overrides the emit method, they are not added by default. If you want to leverage these events you need to call the EventReactor#emit method.

Example

// we assume that ER is your EventReactor instance.
ER.emit();

EventEmitter.on('uncaughtEvent', function (event, data) {
  console.log('no listeners for', event, data);
});

EventEmitter.on('*.*', function () {
  console.log('pew pew, captured);
});

EventEmitter.emit('random name');

Aliases

  • EventEmitter.off -> EventEmitter.removeListener
  • EventEmitter.removeEventListener -> EventEmitter.removeListener
  • EventEmitter.addEventListener -> EventEmitter.addListener