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

vertebrate-event-emitter

v5.0.0

Published

An event emitter implementation robust against memory leaks.

Downloads

38

Readme

Vertebrate event emitter

This repository contains an implementation of an event emitter. Working with emitters can be frustrating. These frustrations led me to make an ES2015 based implementation contained in this repository.

Key features:

  • Based on WeakMap so you don't have to unregister callbacks when you drop references to an emitter.
  • Unregister a callback using a reference (like setTimeout).
  • Give a registered callback a call count to unregister itself.

See below for why these are important.

This library comes with both UMD and ES2015 versions. If you're using rollup, it'll use the ES2015 version. Node will use the UMD version automatically. Since this library provides UMD and ES2015 versions, it's supported by every major module system (or no module system at all). It has no production dependencies, making it easy to include too!

API

Class EventEmitter

The EventEmitter is used to construct new emitter objects. It takes no arguments.

const emitter = new EventEmitter();

reference = emitter.on(name, callback, count = Infinity)

Add a listener function callback to the emitter for the given name. name must be a string. count is the number of times the event can be called before the listener is automatically unregistered. count defaults to Infinity when not given. When given it must be a positive integer greater than 1, or Infinity.

A reference object is returned, which may later be used to unregister the listener.

// Callback called every time 'event-name' triggered.
emitter.on('event-name', callback);

// Callback each time 'event-name' triggered, unregistering after 10 calls.
emitter.on('event-name', callback, 10);

The count parameter can be used to simulate a Node.js style once method.

emitter.off(reference)

Unregister an event listener using the reference object returned by emitter.on.

// The on method returns a reference object.
const ref = emitter.on('event-name', callback);

// Use the off method to unregister the callback.
emitter.off(ref);

emitter.allOff(name = undefined)

When called with a name string, all events for that name are removed from the emitter. When called without a name string, all events for all names are removed.

emitter.trigger(name, ...args)

Trigger all handlers for the given name with the remaining arguments args. The callbacks are called with the emitter as this.

function testCallback(a, b, c) {
  console.log(a, b, c);
}

emitter.on('some-event', testCallback);

emitter.trigger('some-event', 1, 2, 3) // logs: 1, 2, 3

emitter.emit(name, ...args)

Alias for emitter.trigger.

Problems with existing emitters

Emitters make memory leaks too easy to create

For example, if you add an event listener to a Backbone event emitter using on, it will stay there until something removes it. Backbone tries to get around this with listenTo, which allows the emitter itself to unregister events in batch. This is no fault of existing implementations. JavaScript itself made it an impossible problem to solve until recently.

Luckily, one of the earlier features of ES2015 to make it into browsers was WeakMap, which allows the garbage collector to clean up members when no other references to them remain. The Vertebrate event emitter uses these to avoid memory leaks.

Most implementations are keyed on event name and a callback

In Node, you might have code like:

import EventEmitter from 'events';

const emitter = new EventEmitter();

function testCallback() {
  console.log('Hello, world!');
}

emitter.on('test', testCallback); // Add a listener to the 'test' event.

emitter.removeListener('test', testCallback); // Remove the listener.

That looks fine, but what happens when you add the same callback for an event twice? Does the callback get called twice per emission, or just once? If twice, what happens when you remove the listener? Does it remove both or just one?

This ambiguity bothers me.

When a Vertebrate emitter has a listener registered for an event, it returns a reference object, a lot like setTimeout does. Unregistering the event is done using this reference object:

import EventEmitter from 'vertebrate-event-emitter';

const emitter = new EventEmitter();

function testCallback() {
  console.log('Hello, world!');
}

const ref = emitter.on('test', testCallback); // Add a listener.

emitter.off(ref); // Remove the listener. No need to use the event name.

You get a fresh reference object each time a listener is registered, so the ambiguity never arises.