• Stars
    star
    3,271
  • Rank 13,655 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

EventEmitter3 - Because there's also a number 2. And we're faster.

EventEmitter3

Version npmCICoverage Status

Sauce Test Status

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 eventemitter3

CDN

Recommended CDN:

https://unpkg.com/eventemitter3@latest/dist/eventemitter3.umd.min.js

Usage

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

var EventEmitter = require('eventemitter3');

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('event-name', emitted, context);
EE.on('another-event', emitted, context);
EE.removeListener('another-event', 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 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. Note that you will have to run an additional npm i in the benchmarks folder before npm run benchmark.

License

MIT

More Repositories

1

primus

โšก Primus, the creator god of the transformers & an abstraction layer for real-time to prevent module lock-in.
JavaScript
4,469
star
2

ejson

EJSON - Extended JSON from Meteor available for Node.js and the front-end in one handy neat package. Made compatible for Primus.
JavaScript
87
star
3

ultron

Ultron is high-intelligence robot. It gathers intel so it can start improving upon his rudimentary design.
JavaScript
63
star
4

substream

Volatile namespaces on top of Primus streams
JavaScript
33
star
5

access-control

Easily handle HTTP Access Control (CORS) in your applications
JavaScript
33
star
6

forwarded-for

Abstraction for retrieving ip address information from a Node.js connection. Searches for proxy headers before degrading req.address
JavaScript
21
star
7

primus-emit

Emitting events between server and client.
JavaScript
19
star
8

metroplex

The metroplex project keeps track of all connections in redis so it can find the server address for each given spark id.
JavaScript
17
star
9

omega-supreme

Omega supreme adds messaging between multiple primus servers. It needs a server address, spark id and message and the server will then send the correct message to the connected client.
JavaScript
17
star
10

deumdify

Browserify plugin to expose a standalone bundle as a property of the global object.
JavaScript
15
star
11

primacron

Primacron is high level interface written on top of Primus
JavaScript
12
star
12

create-server

Easily create a HTTP server for a users given options.
JavaScript
12
star
13

mirage

On Cybertron, he was one of the planetโ€™s elite upper class, in Primus he provides a transparent persistent session id.
JavaScript
10
star
14

sockjs-client-node

SockJS client for Node.js
JavaScript
8
star
15

fortress-maximus

Despite his great power, size, and rank, Fortress Maximus is a weary and reluctant warrior. Fighting is against his pacifist nature and now spends his time validating.
JavaScript
8
star
16

emits

emits returns a function which will emit and parse the specified event.
JavaScript
7
star
17

binary-pack

A module to package both browser and node versions of @ericz's binarypack
JavaScript
5
star
18

primus-analytics

Deep integration of Google Analytics in to Primus using event tracking
JavaScript
4
star
19

asyncemit

Asynchronously emit event an event based on the arguments length.
JavaScript
4
star
20

ironhide

An opinionated way on how you could use Primus with React.
JavaScript
2
star
21

lessons

Workshopper lessons for Primus
JavaScript
1
star