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

events-intercept

v2.0.0

Published

event interceptors - like middleware for EventEmitter

Downloads

2,558,377

Readme

#events-intercept

Build Status Coverage Status

The node EventEmitter is very powerful. However, at times it could be valuable to intercept events before they reach their handlers, to modify the data, or emit other events. That's a job for event-intercept.

##Installation

npm install events-intercept

##Standalone Usage

The module contains a constructor, EventEmitter, which inherits from the standard node events.EventEmitter.

var EventEmitter = require('events-intercept').EventEmitter;
var emitter = new EventEmitter();

In our application, we have an object that will emit a data event, and pass it a single argument.

emitter.emit('data', 'myData')

It is very easy to listen for this event and handle it

emitter.on('data', function(arg) {
	console.log(arg);
}); //logs 'myData'

However, we want to intercept that event and modify the data. We can do that by setting an interceptor with intercept(event, interceptor). It is passed all arguments that would be passed to the emitter, as well as a standard node callback. In this case, let's just add a prefix on to the data.

emitter.intercept('data', function(arg, done) {
	return done(null, 'intercepted ' + arg);
});

This code will be executed before the handler, and the new argument will be passed on to the handler appropriately.

emitter.emit('data', 'some other data');
//logs 'intercepted some other data'

If multiple interceptors are added to a single event, they will be called in the order that they are added, like async.waterfall.

Here's that sample code all together. Of course, intercept supports proper function chaining.

var eventsIntercept = require('events-intercept');
var emitter = new eventsIntercept.EventEmitter();

emitter
.on('data', function(arg) {
	console.log(arg);
}).intercept('data', function(arg, done) {
	return done(null, 'intercepted ' + arg);
}).emit('data', 'myData');
//logs 'intercepted myData'

Please see test/intercept.js for more complete samples.

##Calling Separate Events

There may be times when you want to intercept one event and call another. Luckily, all intercept handlers are called with the EventEmitter as the this context, so you can emit events yourself.

emitter.intercept('data', function(done) {
	this
	.emit('otherData')
	.emit('thirdData');
	return done(null);
});
//emits 'data', 'otherData', and 'thirdData'

Remember, emitting an event that you are intercepting will cause a loop, so be careful.

In fact, an interceptor do not need to call the callback at all, which means that the event that was intercepted will never be called at all.

emitter.intercept('data', function(done) {
	this
	.emit('otherData')
	.emit('thirdData');
});
//emits 'otherData' and 'thirdData' but not 'data'

##Utilities

events-intercept supports all of the useful utilities that the standard EventEmitter supports:

  • interceptors(type) returns an array of all interceptors (functions) for the given type.
  • removeInterceptor(type, interceptor) removes an interceptor of a given type. You must pass in the interceptor function.
  • removeAllInterceptors(type) removes all interceptors for a given type.
  • removeAllInterceptors() removes all interceptors. Will remove the removeInterceptor event last, so they will all get triggered.
  • the EventEmitter will throw a warning if more than 10 interceptors are added to a single event, as this could represent a memory leak. setMaxInterceptors(n) allows you to change that. Set it to 0 for no limit.

All of these are demonstrated in the tests.

##Patching

Of course, many EventEmitters that you have the pleasure of using will not have the foresight to use event-intercept. Thankfully, Javascript is awesome, it's possible to monkey patch the interception capabilities onto an existing object. Just call

var events = require('events');
var eventsIntercept = require('events-intercept');

var emitter = new events.EventEmitter();

eventsIntercept.patch(emitter)

emitter
.on('data', function(arg) {
	console.log(arg);
}).intercept('data', function(arg, done) {
	return done(null, 'intercepted ' + arg);
}).emit('data', 'myData');
//logs 'intercepted myData'

Now, you should be able to call intercept on the standard EventEmitter.

This is also shown in test/intercept.js.