Skip to content

Convert any object into an event-emitter, and emits events with the name of any any method called on the object.

License

Notifications You must be signed in to change notification settings

jonschlinkert/to-emitter

Repository files navigation

to-emitter NPM version

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

Install with npm

$ npm i to-emitter --save

Usage

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');

Related projects

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on August 06, 2015.

About

Convert any object into an event-emitter, and emits events with the name of any any method called on the object.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published