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

single-event-emitter

v0.3.0

Published

One event emitter per event. Like C# delegates & events, or RxJS multicast observables.

Downloads

6

Readme

single-event-emitter

Version npm Build Status Dependencies Coverage Status

single-event-emitter is a fork of EventEmitter3, with one difference: you create a single emitter for each event. This is the pattern used in C# events & delegates, and it's very similar to RxJS multicast observables. It's easier to setup and use with typechecked APIs. It plays nice with composition, since each event is an object that you can pass a reference to.

class MyFoo {
  onClick = new EventEmitter();
  onDispose = new EventEmitter();
  onFoo = new EventEmitter();
  onBar = new EventEmitter();
}
const t = new MyFoo();
t.onClick.once(() => {/* event handler */});
t.onClick.emit();

The EventEmitter3 README is below, mostly unmodified. I might update it to fully match this project's API, but probably not. The API differences are pretty obvious: If an eventemitter would ask for an event name, this API doesn't. Each emitter emits a single event; no event names required. Parts of the API that e.g. return a list of all event names have obviously been removed.

TypeScript declarations are included; use them if you're confused.

TODO

Add helpers to:

  • re-emit EventEmitter / DOM events as a SingleEventEmitter.
  • re-emit SingleEventEmitter on node EventEmitter / DOM events.
  • bind an event, returning a disposer function.

Add a few proxy utilities:

  • debounce?
  • event filter
    • filter(emitter, () => boolean) returns new Emitter
    • filterTo(emitter, targetEmitter, () => boolean)

Don't go overboard; complicated stuff can be handled with RxJS.

Make emitter a function synonymous with .on() method

  • E.g., should be able to thisObject.onClick(callback)

EventEmitter3 is a high performance EventEmitter. It has been micro-optimized for various of code paths making this, one of, if not the fastest EventEmitter available for Node.js and browsers. The module is API compatible with the EventEmitter that ships by default with Node.js but there are some slight differences:

  • Domain support has been removed.
  • We do not throw an error when you emit an error event and nobody is listening.
  • The newListener and removeListener events have been removed as they are useful only in some uncommon use-cases.
  • The setMaxListeners, getMaxListeners, prependListener and prependOnceListener methods are not available.
  • Support for custom context for events so there is no need to use fn.bind.
  • The removeListener method removes all matching listeners, not only the first.

It's a drop in replacement for existing EventEmitters, but just faster. Free performance, who wouldn't want that? The EventEmitter is written in EcmaScript 3 so it will work in the oldest browsers and node versions that you need to support.

Installation

$ npm install --save single-event-emitter

CDN

Recommended CDN:

https://unpkg.com/single-event-emitter@latest/umd/single-event-emitter.min.js

Usage

After installation the only thing you need to do is require the module:

var EventEmitter = require('single-event-emitter');

And you're ready to create your own EventEmitter instances. For the API documentation, please follow the official Node.js documentation:

http://nodejs.org/api/events.html

Contextual emits

We've upgraded the API of the EventEmitter.on, EventEmitter.once and EventEmitter.removeListener to accept an extra argument which is the context or this value that should be set for the emitted events. This means you no longer have the overhead of an event that required fn.bind in order to get a custom this value.

var EE = new EventEmitter()
  , context = { foo: 'bar' };

function emitted() {
  console.log(this === context); // true
}

EE.once(emitted, context);
EE.removeListener(emitted, context);

Tests and benchmarks

This module is well tested. You can run:

  • npm test to run the tests under Node.js.
  • npm run test-browser to run the tests in real browsers via Sauce Labs.

We also have a set of benchmarks to compare EventEmitter3 with some available alternatives. To run the benchmarks run cd benchmarks ; npm install ; cd .. ; npm run benchmark.

Tests and benchmarks are not included in the npm package. If you want to play with them you have to clone the GitHub repository.

License

MIT