Extend event emitters to allow registering listeners only once.
Install with npm
$ npm i emitter-only --save
var only = require('emitter-only');
Emitter.prototype.only = function () {
return only.apply(this, arguments);
};
var emitter = new Emitter();
emitter.on('foo', function (msg) {
console.log('foo', msg);
});
emitter.only('one-time', 'foo', function (msg) {
console.log('foo 1', msg);
});
emitter.only('one-time', 'foo', function (msg) {
console.log('foo 2', msg);
});
emitter.emit('foo', 'bar');
//=> 'foo bar'
//=> 'foo 1 bar'
Only register an event listener one time for the given name/event
combo.
Params
name
{String}: Name to specify this is a unique listener.event
{String}: Event name to pass to the emitteron
method.fn
{Function}: Event listener function to pass to the emitteron
method.returns
{Object}: Returnthis
for chaining.
component-emitter: Event emitter | homepage
Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Brian Woodward
Copyright © 2015 Brian Woodward Released under the MIT license.
This file was generated by verb-cli on September 23, 2015.