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

jellyemitter

v1.0.14

Published

A generic event emitter that doesn't totally suck.

Downloads

6

Readme

jellyemitter Build Status

A generic event emitter that doesn't totally suck.

Installation

npm install --save jellyemitter

Usage

var JellyEmitter = require('jellyemitter')
var emitter = new JellyEmitter

emitter.on('foo', function () {console.log('foo!')})
emitter.emit('foo') // => "foo!"

How is this different from the Node.js EventEmitter?

  • All events are treated equally ("error", "newListener", and "removeListener" are not special)
  • No intrusive inspection with listenerCount() or listeners()
  • No max listeners warning (annoying and useless)
  • Only 704 bytes (minified and gzipped)

Other than that, it's basically the same. You get emit, on, once, addListener, and removeListener.

Design details

There's no removeAllListeners()

Okay I lied. You can do _removeAllListeners([eventName]) (underscored), but you should only do that under one of two conditions:

  • You are an API developer and you really know what you're doing
  • You are an API consumer and you really know what you're doing, and you know your code is the only code to touch that event emitter

I underscored this method because of how dangerous it is. If you don't understand why it's dangerous, you probably shouldn't be using it.

Inheritance

When you inherit from JellyEmitter, you don't have to call the JellyEmitter constructor. Inheriting the prototype is enough.

Performance

JellyEmitter takes advantage of smart V8 optimizations, just like EventEmitter. In most cases you'll find it performs even faster than EventEmitter.

Browser Support

  • Chrome 5+
  • Firefox 4+
  • Safari 5+
  • Opera 11.6+
  • Internet Explorer 9+

Super-Secret Trick (for API developers)

One cool thing you can do with JellyEmitter, but not EventEmitter, is the ability to wrap your event listeners in closures, but still allow the listener to be removed with the inner (unwrapped) function.

Example:

var original = function () {...};
var wrapper = function () {
	doSomeStuff();
	return original.apply(this, arguments)
};

wrapper.originalListener = original;
emitter.on('foo', wrapper);

// Then you can do this...
emitter.removeListener('foo', original)

If you give an event listener the .originalListener property, that listener can only be removed by referencing the value of .originalListener, and not by referencing the function that was actually registered.

This is useful for API developers who wish to give custom functionality to some (or all) events. It allows the API consumer to still have the power to remove their listeners, even though they were wrapped by the API developer.

Caution:

When you use the .originalListener property on a wrapped function, JellyEmitter can no longer disinguish between the wrapped function and the original. This means, if you add both the original and the wrapped listener to the JellyEmitter, a call to .removeListener(eventName, original) will just remove the first version that was added, regardless of which version you intended to remove. Therefore, if you add a wrapped listener, you should never allow the unwrapped version to be added (to the same event). You should also never add the same function wrapped in two different ways (to the same event).

In short, when you use the .originalListener property, you are saying "this outer function is totally representing the original, and no other listener will do such—not even the original—unless it is a clone of this outer function".