ak-eventemitter
Advanced tools
Comparing version 0.0.1 to 0.0.2
236
index.js
@@ -1,235 +0,1 @@ | ||
'use strict'; | ||
/** | ||
* Export `EventEmitter` | ||
* | ||
* @param {Object} config (optional) | ||
* @return {EventEmitter} | ||
*/ | ||
var EventEmitter = module.exports = function (config) { | ||
this.tree = {'children': {}}; | ||
this.delimiter = (config || {}).delimiter || '.'; | ||
}; | ||
/** | ||
* Call all callbacks for given tree | ||
* | ||
* @see #_searchTree(); | ||
* | ||
* @param {Object} tree | ||
* @param {arguments} args | ||
*/ | ||
EventEmitter.prototype._emit = function (tree, args) { | ||
var callbacks = tree.callbacks; | ||
if (! callbacks) { | ||
return this; | ||
} | ||
var argc = args.length; | ||
for ( | ||
var i = 0, | ||
len = callbacks.length, | ||
callback; | ||
i < len; | ||
i += 1 | ||
) { | ||
callback = callbacks[i]; | ||
if (argc === 1) { | ||
callback.fn.call(callback.context, args[0]); | ||
} else if (argc === 2) { | ||
callback.fn.call(callback.context, args[0], args[1]); | ||
} else { | ||
callback.fn.apply(callback.context, args); | ||
} | ||
if (callback.once) { | ||
callbacks.splice(i, 1); | ||
if (callbacks.length === 0) { | ||
tree.callbacks = undefined; | ||
} | ||
} | ||
} | ||
}; | ||
/** | ||
* Parse given tree for given ns | ||
* | ||
* @see #emit(); | ||
* | ||
* @param {Object} tree | ||
* @param {Array} ns | ||
* @param {Integer} start | ||
* @param {arguments} args | ||
*/ | ||
EventEmitter.prototype._searchTree = function (tree, ns, start, args) { | ||
for (var i = start, | ||
len = ns.length, | ||
currentNs, | ||
currentTree, | ||
wildTree; | ||
i < len; | ||
i += 1 | ||
) { | ||
if (wildTree = tree.children['*']) { | ||
if (wildTree.callbacks) { | ||
this._emit(wildTree, args); | ||
} | ||
this._searchTree(wildTree, ns, i + 1, args); | ||
} | ||
currentNs = ns[i]; | ||
currentTree = tree.children[currentNs]; | ||
if (! currentTree) { | ||
return this; | ||
} | ||
tree = currentTree; | ||
} | ||
if (currentTree) { | ||
this._emit(currentTree, args); | ||
} | ||
}; | ||
/** | ||
* Add event listener | ||
* | ||
* @param {String} ns | ||
* @param {Function} callback | ||
* @param {Object} options (optional) | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.on = function (ns, callback, options) { | ||
ns = ns.split(this.delimiter); | ||
var tree = this.tree; | ||
var currentNs; | ||
var currentTree; | ||
for (var i = 0, len = ns.length; i < len; i += 1) { | ||
currentNs = ns[i]; | ||
if (! (currentTree = tree.children[currentNs])) { | ||
currentTree = tree.children[currentNs] = {'children': {}}; | ||
} | ||
tree = currentTree; | ||
} | ||
if (! tree.callbacks) { | ||
tree.callbacks = []; | ||
} | ||
tree.callbacks.push({ | ||
'fn': callback, | ||
'context': ! options || ! options.context ? this : options.context, | ||
'once': !! (options && options.once) | ||
}); | ||
return this; | ||
}; | ||
/** | ||
* Remove event listener | ||
* | ||
* @param {String} ns | ||
* @param {Function} callback | ||
* @param {Object} options (optional) | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.off = function (ns, callback, options) { | ||
if (! ns) { | ||
this.tree = {'children': {}}; | ||
return this; | ||
} | ||
ns = ns.split(this.delimiter); | ||
var tree = this.tree; | ||
var currentNs; | ||
var currentTree; | ||
for (var i = 0, len = ns.length; i < len; i += 1) { | ||
currentNs = ns[i]; | ||
if (! (currentTree = tree.children[currentNs])) { | ||
return this; | ||
} | ||
tree = currentTree; | ||
} | ||
if (! callback) { | ||
tree.callbacks = undefined; | ||
return this; | ||
} | ||
if (! tree.callbacks) { | ||
return this; | ||
} | ||
for ( | ||
var i2 = 0, | ||
callbacks = tree.callbacks, | ||
len2 = callbacks.length, | ||
currentCallback; | ||
i2 < len2; | ||
i2 += 1 | ||
) { | ||
currentCallback = callbacks[i2]; | ||
if (currentCallback.fn === callback) { | ||
if (options && options.context && options.context !== currentCallback.context) { | ||
continue; | ||
} | ||
callbacks[i2].splice(i2, 1); | ||
break; | ||
} | ||
} | ||
if (! callbacks.length) { | ||
tree.callbacks = undefined; | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Emit event | ||
* | ||
* @param {String} ns | ||
* @param {*} ... (optional) | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.emit = function (ns) { | ||
ns = ns.split(this.delimiter); | ||
this._searchTree(this.tree, ns, 0, arguments); | ||
return this; | ||
}; | ||
/** | ||
* Add event listener for once | ||
* | ||
* @param {String} ns | ||
* @param {Function} callback | ||
* @param {Object} options (optional) | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.once = function (ns, callback, options) { | ||
options = options ? options : {}; | ||
options.once = true; | ||
this.on(ns, callback, options); | ||
return this; | ||
}; | ||
module.exports = process.env.TEST_COVERAGE ? require('./lib-cov/eventemitter') : require('./lib/eventemitter'); |
{ | ||
"name": "ak-eventemitter", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "eventemitter with namespaces", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -57,6 +57,10 @@ /*global describe, it*/ | ||
var emitter = new EventEmitter(); | ||
emitter.on('hari.*', function () { | ||
emitter.on('hari.*', function (ns, a) { | ||
assert.equal(arguments.length, 2); | ||
assert.equal(ns, 'hari.bol'); | ||
assert.equal(a, 11); | ||
done(); | ||
}); | ||
emitter.emit('hari.bol'); | ||
emitter.emit('hari.bol', 11); | ||
}); | ||
@@ -149,7 +153,15 @@ it('ON: hari.*, EMIT: bolo.hari, hari.bol', function (done) { | ||
describe('#off()', function () { | ||
it('ON: hari.*, EMIT hari.bol, OFF: hari.*, EMIT: hari.hari', function (done) { | ||
it('ON: hari.hari, hari.*, EMIT: hari.bol, OFF: hari.*, EMIT: hari.hari', function (done) { | ||
var counter = 0; | ||
var emitter = new EventEmitter(); | ||
emitter.once('hari.*', function () { | ||
done(); | ||
emitter.on('hari.hari', function () { | ||
if (counter === 1) { | ||
done(); | ||
} else { | ||
throw new Error('Should not be triggered more than once.'); | ||
} | ||
}); | ||
emitter.on('hari.*', function () { | ||
counter += 1; | ||
}); | ||
emitter.emit('hari.bol'); | ||
@@ -159,5 +171,19 @@ emitter.off('hari.*'); | ||
}); | ||
it('ON: hari.*, EMIT: hari.bol, OFF: hari.hari, EMIT: hari.hari', function (done) { | ||
var counter = 0; | ||
var emitter = new EventEmitter(); | ||
emitter.on('hari.*', function () { | ||
counter += 1; | ||
}); | ||
emitter.emit('hari.bol'); | ||
emitter.off('hari.hari'); | ||
emitter.emit('hari.hari'); | ||
if (counter === 2) { | ||
done(); | ||
} | ||
}); | ||
it('ON: hari.*, EMIT: hari.bol, OFF, EMIT: hari.hari', function (done) { | ||
var emitter = new EventEmitter(); | ||
emitter.once('hari.*', function () { | ||
emitter.on('hari.*', function () { | ||
done(); | ||
@@ -169,3 +195,65 @@ }); | ||
}); | ||
it('ON: hari.bol(fn1), hari.bol(fn2), EMIT: hari.bol, OFF(fn1), EMIT: hari.bol', function (done) { | ||
var counter = 0; | ||
var emitter = new EventEmitter(); | ||
var fn1 = function () { | ||
counter += 1; | ||
}; | ||
var fn2 = function () { | ||
if (counter === 1) { | ||
done(); | ||
} else { | ||
throw new Error('Should not be triggered more than once.'); | ||
} | ||
}; | ||
emitter.on('hari.bol', fn1); | ||
emitter.on('hari.bol', fn2); | ||
emitter.emit('hari.bol'); | ||
emitter.off('hari.bol', fn2); | ||
emitter.emit('hari.bol'); | ||
emitter.off('hari.bol', fn1); | ||
emitter.emit('hari.bol'); | ||
}); | ||
it('ON: hari.bol(fn1, ctx1), hari.bol(fn1, ctx2), EMIT: hari.bol, OFF(fn1, ctx1), EMIT: hari.bol', function (done) { | ||
var counter1 = 0; | ||
var counter2 = 0; | ||
var ctx1 = {'x': 1}; | ||
var ctx2 = {'x': 2}; | ||
var emitter = new EventEmitter(); | ||
var fn = function () { | ||
if (this.x === 1) { | ||
counter1 += 1; | ||
} else if (this.x === 2) { | ||
counter2 += 1; | ||
} | ||
}; | ||
emitter.on('hari.bol', fn, ctx1); | ||
emitter.on('hari.bol', fn, ctx2); | ||
emitter.emit('hari.bol'); | ||
emitter.off('hari.bol', fn, ctx2); | ||
emitter.emit('hari.bol'); | ||
emitter.off('hari.bol', fn, ctx1); | ||
emitter.emit('hari.bol'); | ||
/* NOTE for test coverage */ | ||
emitter.off('hari.bol', fn); | ||
if (counter1 === 2 && counter2 === 1) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
// delimiter | ||
describe('delimiter', function () { | ||
it('ON: hari::*, EMIT: hari.bol, hari::bol', function (done) { | ||
var emitter = new EventEmitter({'delimiter': '::'}); | ||
emitter.on('hari::*', function () { | ||
done(); | ||
}); | ||
emitter.emit('hari.bol'); | ||
emitter.emit('hari::bol'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17674
10
550
1