Comparing version 1.0.0 to 1.0.1
12
index.js
module.exports = function() { | ||
var subscribers = [] | ||
return { | ||
on: function (name, f) { | ||
on: function (eventName, cb) { | ||
subscribers.push({ | ||
name: name, | ||
f: f | ||
eventName: eventName, | ||
cb: cb | ||
}) | ||
}, | ||
trigger: function (name, data) { | ||
trigger: function (eventName, data) { | ||
subscribers | ||
.filter(function (subscriber) { | ||
return subscriber.name === name | ||
return subscriber.eventName === eventName | ||
}) | ||
.forEach(function (subscriber) { | ||
subscriber.f(data) | ||
subscriber.cb(data) | ||
}) | ||
@@ -18,0 +18,0 @@ } |
{ | ||
"name": "emitter20", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "An event emitter in 20 lines of code.", | ||
@@ -5,0 +5,0 @@ "license": "ISC", |
@@ -19,5 +19,7 @@ # emitter20 | ||
var emitter = new Emitter() | ||
emitter.on('karate-chop', function() { | ||
console.log('Haiaaaaaa!') | ||
}) | ||
emitter.trigger('karate-chop') // Haiaaaaa!' | ||
@@ -30,10 +32,27 @@ ``` | ||
var emitter = new Emitter() | ||
emitter.on('welcome', function(name) { | ||
console.log(`Welcome {name}!`) | ||
}) | ||
emitter.trigger('welcome', 'bob') // Welcome bob! | ||
``` | ||
Can be used as a mixin: | ||
```js | ||
var assign = require('lodash.assign') | ||
var obj = { a: 1, b: 2 } | ||
assign(obj, new Emitter()) | ||
obj.on('karate-chop', function() { | ||
console.log('Haiaaaaaa!') | ||
}) | ||
obj.trigger('karate-chop') // Haiaaaaa!' | ||
``` | ||
## License | ||
ISC © [Raine Lourie](https://github.com/metaraine) |
3877
9
43
57