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.

74 lines
1.3 KiB

7 years ago
  1. # Emitter [![Build Status](https://travis-ci.org/component/emitter.png)](https://travis-ci.org/component/emitter)
  2. Event emitter component.
  3. ## Installation
  4. ```
  5. $ component install component/emitter
  6. ```
  7. ## API
  8. ### Emitter(obj)
  9. The `Emitter` may also be used as a mixin. For example
  10. a "plain" object may become an emitter, or you may
  11. extend an existing prototype.
  12. As an `Emitter` instance:
  13. ```js
  14. var Emitter = require('emitter');
  15. var emitter = new Emitter;
  16. emitter.emit('something');
  17. ```
  18. As a mixin:
  19. ```js
  20. var Emitter = require('emitter');
  21. var user = { name: 'tobi' };
  22. Emitter(user);
  23. user.emit('im a user');
  24. ```
  25. As a prototype mixin:
  26. ```js
  27. var Emitter = require('emitter');
  28. Emitter(User.prototype);
  29. ```
  30. ### Emitter#on(event, fn)
  31. Register an `event` handler `fn`.
  32. ### Emitter#once(event, fn)
  33. Register a single-shot `event` handler `fn`,
  34. removed immediately after it is invoked the
  35. first time.
  36. ### Emitter#off(event, fn)
  37. * Pass `event` and `fn` to remove a listener.
  38. * Pass `event` to remove all listeners on that event.
  39. * Pass nothing to remove all listeners on all events.
  40. ### Emitter#emit(event, ...)
  41. Emit an `event` with variable option args.
  42. ### Emitter#listeners(event)
  43. Return an array of callbacks, or an empty array.
  44. ### Emitter#hasListeners(event)
  45. Check if this emitter has `event` handlers.
  46. ## License
  47. MIT