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