[6a3a178] | 1 | # EventEmitter3
|
---|
| 2 |
|
---|
| 3 | [![Version npm](https://img.shields.io/npm/v/eventemitter3.svg?style=flat-square)](https://www.npmjs.com/package/eventemitter3)[![Build Status](https://img.shields.io/travis/primus/eventemitter3/master.svg?style=flat-square)](https://travis-ci.org/primus/eventemitter3)[![Dependencies](https://img.shields.io/david/primus/eventemitter3.svg?style=flat-square)](https://david-dm.org/primus/eventemitter3)[![Coverage Status](https://img.shields.io/coveralls/primus/eventemitter3/master.svg?style=flat-square)](https://coveralls.io/r/primus/eventemitter3?branch=master)[![IRC channel](https://img.shields.io/badge/IRC-irc.freenode.net%23primus-00a8ff.svg?style=flat-square)](https://webchat.freenode.net/?channels=primus)
|
---|
| 4 |
|
---|
| 5 | [![Sauce Test Status](https://saucelabs.com/browser-matrix/eventemitter3.svg)](https://saucelabs.com/u/eventemitter3)
|
---|
| 6 |
|
---|
| 7 | EventEmitter3 is a high performance EventEmitter. It has been micro-optimized
|
---|
| 8 | for various of code paths making this, one of, if not the fastest EventEmitter
|
---|
| 9 | available for Node.js and browsers. The module is API compatible with the
|
---|
| 10 | EventEmitter that ships by default with Node.js but there are some slight
|
---|
| 11 | differences:
|
---|
| 12 |
|
---|
| 13 | - Domain support has been removed.
|
---|
| 14 | - We do not `throw` an error when you emit an `error` event and nobody is
|
---|
| 15 | listening.
|
---|
| 16 | - The `newListener` and `removeListener` events have been removed as they
|
---|
| 17 | are useful only in some uncommon use-cases.
|
---|
| 18 | - The `setMaxListeners`, `getMaxListeners`, `prependListener` and
|
---|
| 19 | `prependOnceListener` methods are not available.
|
---|
| 20 | - Support for custom context for events so there is no need to use `fn.bind`.
|
---|
| 21 | - The `removeListener` method removes all matching listeners, not only the
|
---|
| 22 | first.
|
---|
| 23 |
|
---|
| 24 | It's a drop in replacement for existing EventEmitters, but just faster. Free
|
---|
| 25 | performance, who wouldn't want that? The EventEmitter is written in EcmaScript 3
|
---|
| 26 | so it will work in the oldest browsers and node versions that you need to
|
---|
| 27 | support.
|
---|
| 28 |
|
---|
| 29 | ## Installation
|
---|
| 30 |
|
---|
| 31 | ```bash
|
---|
| 32 | $ npm install --save eventemitter3
|
---|
| 33 | ```
|
---|
| 34 |
|
---|
| 35 | ## CDN
|
---|
| 36 |
|
---|
| 37 | Recommended CDN:
|
---|
| 38 |
|
---|
| 39 | ```text
|
---|
| 40 | https://unpkg.com/eventemitter3@latest/umd/eventemitter3.min.js
|
---|
| 41 | ```
|
---|
| 42 |
|
---|
| 43 | ## Usage
|
---|
| 44 |
|
---|
| 45 | After installation the only thing you need to do is require the module:
|
---|
| 46 |
|
---|
| 47 | ```js
|
---|
| 48 | var EventEmitter = require('eventemitter3');
|
---|
| 49 | ```
|
---|
| 50 |
|
---|
| 51 | And you're ready to create your own EventEmitter instances. For the API
|
---|
| 52 | documentation, please follow the official Node.js documentation:
|
---|
| 53 |
|
---|
| 54 | http://nodejs.org/api/events.html
|
---|
| 55 |
|
---|
| 56 | ### Contextual emits
|
---|
| 57 |
|
---|
| 58 | We've upgraded the API of the `EventEmitter.on`, `EventEmitter.once` and
|
---|
| 59 | `EventEmitter.removeListener` to accept an extra argument which is the `context`
|
---|
| 60 | or `this` value that should be set for the emitted events. This means you no
|
---|
| 61 | longer have the overhead of an event that required `fn.bind` in order to get a
|
---|
| 62 | custom `this` value.
|
---|
| 63 |
|
---|
| 64 | ```js
|
---|
| 65 | var EE = new EventEmitter()
|
---|
| 66 | , context = { foo: 'bar' };
|
---|
| 67 |
|
---|
| 68 | function emitted() {
|
---|
| 69 | console.log(this === context); // true
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | EE.once('event-name', emitted, context);
|
---|
| 73 | EE.on('another-event', emitted, context);
|
---|
| 74 | EE.removeListener('another-event', emitted, context);
|
---|
| 75 | ```
|
---|
| 76 |
|
---|
| 77 | ### Tests and benchmarks
|
---|
| 78 |
|
---|
| 79 | This module is well tested. You can run:
|
---|
| 80 |
|
---|
| 81 | - `npm test` to run the tests under Node.js.
|
---|
| 82 | - `npm run test-browser` to run the tests in real browsers via Sauce Labs.
|
---|
| 83 |
|
---|
| 84 | We also have a set of benchmarks to compare EventEmitter3 with some available
|
---|
| 85 | alternatives. To run the benchmarks run `npm run benchmark`.
|
---|
| 86 |
|
---|
| 87 | Tests and benchmarks are not included in the npm package. If you want to play
|
---|
| 88 | with them you have to clone the GitHub repository.
|
---|
| 89 | Note that you will have to run an additional `npm i` in the benchmarks folder
|
---|
| 90 | before `npm run benchmark`.
|
---|
| 91 |
|
---|
| 92 | ## License
|
---|
| 93 |
|
---|
| 94 | [MIT](LICENSE)
|
---|