| 1 |
# tiny-emitter |
| 2 |
|
| 3 |
A tiny (less than 1k) event emitter library. |
| 4 |
|
| 5 |
## Install |
| 6 |
|
| 7 |
### npm |
| 8 |
|
| 9 |
``` |
| 10 |
npm install tiny-emitter --save |
| 11 |
``` |
| 12 |
|
| 13 |
## Usage |
| 14 |
|
| 15 |
```js |
| 16 |
var Emitter = require('tiny-emitter'); |
| 17 |
var emitter = new Emitter(); |
| 18 |
|
| 19 |
emitter.on('some-event', function (arg1, arg2, arg3) { |
| 20 |
// |
| 21 |
}); |
| 22 |
|
| 23 |
emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value'); |
| 24 |
``` |
| 25 |
|
| 26 |
Alternatively, you can skip the initialization step by requiring `tiny-emitter/instance` instead. This pulls in an already initialized emitter. |
| 27 |
|
| 28 |
```js |
| 29 |
var emitter = require('tiny-emitter/instance'); |
| 30 |
|
| 31 |
emitter.on('some-event', function (arg1, arg2, arg3) { |
| 32 |
// |
| 33 |
}); |
| 34 |
|
| 35 |
emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value'); |
| 36 |
``` |
| 37 |
|
| 38 |
## Instance Methods |
| 39 |
|
| 40 |
### on(event, callback[, context]) |
| 41 |
|
| 42 |
Subscribe to an event |
| 43 |
|
| 44 |
* `event` - the name of the event to subscribe to |
| 45 |
* `callback` - the function to call when event is emitted |
| 46 |
* `context` - (OPTIONAL) - the context to bind the event callback to |
| 47 |
|
| 48 |
### once(event, callback[, context]) |
| 49 |
|
| 50 |
Subscribe to an event only **once** |
| 51 |
|
| 52 |
* `event` - the name of the event to subscribe to |
| 53 |
* `callback` - the function to call when event is emitted |
| 54 |
* `context` - (OPTIONAL) - the context to bind the event callback to |
| 55 |
|
| 56 |
### off(event[, callback]) |
| 57 |
|
| 58 |
Unsubscribe from an event or all events. If no callback is provided, it unsubscribes you from all events. |
| 59 |
|
| 60 |
* `event` - the name of the event to unsubscribe from |
| 61 |
* `callback` - the function used when binding to the event |
| 62 |
|
| 63 |
### emit(event[, arguments...]) |
| 64 |
|
| 65 |
Trigger a named event |
| 66 |
|
| 67 |
* `event` - the event name to emit |
| 68 |
* `arguments...` - any number of arguments to pass to the event subscribers |
| 69 |
|
| 70 |
## Test and Build |
| 71 |
|
| 72 |
Build (Tests, Browserifies, and minifies) |
| 73 |
|
| 74 |
``` |
| 75 |
npm install |
| 76 |
npm run build |
| 77 |
``` |
| 78 |
|
| 79 |
Test |
| 80 |
|
| 81 |
``` |
| 82 |
npm install |
| 83 |
npm test |
| 84 |
``` |
| 85 |
|
| 86 |
## License |
| 87 |
|
| 88 |
[MIT](https://github.com/scottcorgan/tiny-emitter/blob/master/LICENSE) |