You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.4 KiB

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