Convert any object into an event-emitter, and emits events with the name of any any method called on the object.
Wraps each method on an object so that an event is emitted with the name of the method along with the arguments passed to the method.
Example
// an old object with a property that has a function value
var obj = {cache: {}};
obj.set = function(key, val) {
obj[key] = val;
return obj;
};
// pass an instance of your favorite event emitter,
// see `example.js` for a working example
toEmitter(emitter, obj);
// now `obj` is an emitter, and `obj.set` will emit a `set` event.
obj.on('set', function(key, value) {
console.log('set:', key, value);
});
obj.set('foo', 'bar');
Install with npm
$ npm i to-emitter --save
var Emitter = require('component-emitter');
var toEmitter = require('to-emitter');
var obj = {
cache: {},
set: function (key, val) {
obj.cache[key] = val
return obj;
},
get: function (key) {
return obj.cache[key];
}
};
obj.set('foo', 'bar');
var emitter = Emitter({});
toEmitter(emitter, obj);
obj.on('set', function (key, val) {
console.log('set:', key, val);
});
obj.on('get', function (key) {
console.log('get:', key);
});
obj.set('a', 'b');
obj.set('c', 'd');
obj.get('a');
obj.get('c');
- collection-visit: Visit a method over the items in an object, or map visit over the objects… more
- minimist-events: Add events to minimist, ~30 sloc.
- map-visit: Map
visit
over an array of objects. - object-visit: Call the given method on each value in the given object.
Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Jon Schlinkert
Copyright © 2015 Jon Schlinkert Released under the MIT license.
This file was generated by verb-cli on August 06, 2015.