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.

89 lines
3.6 KiB

7 years ago
  1. # EventEmitter3
  2. [![Version npm](https://img.shields.io/npm/v/eventemitter3.svg?style=flat-square)](http://browsenpm.org/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` event is removed as the use-cases for this functionality are
  13. really just edge cases.
  14. - No `setMaxListeners` and it's pointless memory leak warnings. If you want to
  15. add `end` listeners you should be able to do that without modules complaining.
  16. - No `listenerCount` function. Use `EE.listeners(event).length` instead.
  17. - Support for custom context for events so there is no need to use `fn.bind`.
  18. - `listeners` method can do existence checking instead of returning only arrays.
  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 # npm
  26. $ component install primus/eventemitter3 # Component
  27. $ bower install eventemitter3 # Bower
  28. ```
  29. ## Usage
  30. After installation the only thing you need to do is require the module:
  31. ```js
  32. var EventEmitter = require('eventemitter3');
  33. ```
  34. And you're ready to create your own EventEmitter instances. For the API
  35. documentation, please follow the official Node.js documentation:
  36. http://nodejs.org/api/events.html
  37. ### Contextual emits
  38. We've upgraded the API of the `EventEmitter.on`, `EventEmitter.once` and
  39. `EventEmitter.removeListener` to accept an extra argument which is the `context`
  40. or `this` value that should be set for the emitted events. This means you no
  41. longer have the overhead of an event that required `fn.bind` in order to get a
  42. custom `this` value.
  43. ```js
  44. var EE = new EventEmitter()
  45. , context = { foo: 'bar' };
  46. function emitted() {
  47. console.log(this === context); // true
  48. }
  49. EE.once('event-name', emitted, context);
  50. EE.on('another-event', emitted, context);
  51. EE.removeListener('another-event', emitted, context);
  52. ```
  53. ### Existence
  54. To check if there is already a listener for a given event you can supply the
  55. `listeners` method with an extra boolean argument. This will transform the
  56. output from an array, to a boolean value which indicates if there are listeners
  57. in place for the given event:
  58. ```js
  59. var EE = new EventEmitter();
  60. EE.once('event-name', function () {});
  61. EE.on('another-event', function () {});
  62. EE.listeners('event-name', true); // returns true
  63. EE.listeners('unknown-name', true); // returns false
  64. ```
  65. ## License
  66. [MIT](LICENSE)