eventemitter2
Advanced tools
Comparing version 0.4.3 to 0.4.6
@@ -27,7 +27,12 @@ ;!function(exports, undefined) { | ||
// | ||
// Attention, function return type now is array, always ! | ||
// It has zero elements if no any matches found and one or more | ||
// elements (leafs) if there are matches | ||
// | ||
function searchListenerTree(handlers, type, tree, i) { | ||
if (!tree) { | ||
return; | ||
return []; | ||
} | ||
var listeners, leaf, len, branch, xTree, xxTree, isolatedBranch, endReached, | ||
var listeners=[], leaf, len, branch, xTree, xxTree, isolatedBranch, endReached, | ||
typeLength = type.length, currentType = type[i], nextType = type[i+1]; | ||
@@ -41,3 +46,3 @@ if (i === typeLength && tree._listeners) { | ||
handlers && handlers.push(tree._listeners); | ||
return tree; | ||
return [tree]; | ||
} else { | ||
@@ -47,3 +52,3 @@ for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) { | ||
} | ||
return tree; | ||
return [tree]; | ||
} | ||
@@ -60,3 +65,3 @@ } | ||
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) { | ||
listeners = searchListenerTree(handlers, type, tree[branch], i+1); | ||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+1)); | ||
} | ||
@@ -69,3 +74,3 @@ } | ||
// The next element has a _listeners, add it to the handlers. | ||
listeners = searchListenerTree(handlers, type, tree, typeLength); | ||
listeners = listeners.concat(searchListenerTree(handlers, type, tree, typeLength)); | ||
} | ||
@@ -77,10 +82,10 @@ | ||
if(tree[branch]._listeners && !endReached) { | ||
listeners = searchListenerTree(handlers, type, tree[branch], typeLength); | ||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], typeLength)); | ||
} | ||
listeners = searchListenerTree(handlers, type, tree[branch], i); | ||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i)); | ||
} else if(branch === nextType) { | ||
listeners = searchListenerTree(handlers, type, tree[branch], i+2); | ||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+2)); | ||
} else { | ||
// No match on this one, shift into the tree but not in the type array. | ||
listeners = searchListenerTree(handlers, type, tree[branch], i); | ||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i)); | ||
} | ||
@@ -92,3 +97,3 @@ } | ||
listeners = searchListenerTree(handlers, type, tree[currentType], i+1); | ||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[currentType], i+1)); | ||
} | ||
@@ -325,2 +330,8 @@ | ||
EventEmitter.prototype.on = function(type, listener) { | ||
if (typeof type === 'function') { | ||
this.onAny(type); | ||
return this; | ||
} | ||
if (typeof listener !== 'function') { | ||
@@ -398,10 +409,7 @@ throw new Error('on only accepts instances of Function'); | ||
var handlers; | ||
var handlers,leafs=[]; | ||
if(this.wildcard) { | ||
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice(); | ||
var leaf = searchListenerTree.call(this, null, ns, this.listenerTree, 0); | ||
if('undefined' === typeof leaf) { return this; } | ||
handlers = leaf._listeners; | ||
leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0); | ||
} | ||
@@ -412,29 +420,44 @@ else { | ||
handlers = this._events[type]; | ||
leafs.push({_listeners:handlers}); | ||
} | ||
if (isArray(handlers)) { | ||
for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) { | ||
var leaf = leafs[iLeaf]; | ||
handlers = leaf._listeners; | ||
if (isArray(handlers)) { | ||
var position = -1; | ||
var position = -1; | ||
for (var i = 0, length = handlers.length; i < length; i++) { | ||
if (handlers[i] === listener || | ||
(handlers[i].listener && handlers[i].listener === listener) || | ||
(handlers[i]._origin && handlers[i]._origin === listener)) { | ||
position = i; | ||
break; | ||
for (var i = 0, length = handlers.length; i < length; i++) { | ||
if (handlers[i] === listener || | ||
(handlers[i].listener && handlers[i].listener === listener) || | ||
(handlers[i]._origin && handlers[i]._origin === listener)) { | ||
position = i; | ||
break; | ||
} | ||
} | ||
} | ||
if (position < 0) { | ||
return this; | ||
} | ||
if (position < 0) { | ||
return this; | ||
} | ||
if(this.wildcard) { | ||
leaf._listeners.splice(position, 1) | ||
if(this.wildcard) { | ||
leaf._listeners.splice(position, 1) | ||
} | ||
else { | ||
this._events[type].splice(position, 1); | ||
} | ||
if (handlers.length === 0) { | ||
if(this.wildcard) { | ||
delete leaf._listeners; | ||
} | ||
else { | ||
delete this._events[type]; | ||
} | ||
} | ||
} | ||
else { | ||
this._events[type].splice(position, 1); | ||
} | ||
if (handlers.length === 0) { | ||
else if (handlers === listener || | ||
(handlers.listener && handlers.listener === listener) || | ||
(handlers._origin && handlers._origin === listener)) { | ||
if(this.wildcard) { | ||
@@ -448,12 +471,2 @@ delete leaf._listeners; | ||
} | ||
else if (handlers === listener || | ||
(handlers.listener && handlers.listener === listener) || | ||
(handlers._origin && handlers._origin === listener)) { | ||
if(this.wildcard) { | ||
delete leaf._listeners; | ||
} | ||
else { | ||
delete this._events[type]; | ||
} | ||
} | ||
@@ -489,6 +502,8 @@ return this; | ||
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice(); | ||
var leaf = searchListenerTree.call(this, null, ns, this.listenerTree, 0); | ||
var leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0); | ||
if('undefined' === typeof leaf) { return this; } | ||
leaf._listeners = null; | ||
for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) { | ||
var leaf = leafs[iLeaf]; | ||
leaf._listeners = null; | ||
} | ||
} | ||
@@ -538,2 +553,2 @@ else { | ||
}(typeof window !== 'undefined' ? window : exports); | ||
}(typeof process !== 'undefined' && process.title ? exports : window); |
{ | ||
"name": "eventemitter2", | ||
"version": "0.4.3", | ||
"version": "0.4.6", | ||
"description": "A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support.", | ||
@@ -5,0 +5,0 @@ "keywords": ["event", "events", "emitter", "eventemitter"], |
@@ -67,14 +67,16 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
process, | ||
ArrayBuffer, | ||
Int8Array, | ||
Uint8Array, | ||
Int16Array, | ||
Uint16Array, | ||
Int32Array, | ||
Uint32Array, | ||
Float32Array, | ||
Float64Array, | ||
DataView, | ||
global.ArrayBuffer!==undefined?ArrayBuffer:null, | ||
global.Int8Array!==undefined?Int8Array:null, | ||
global.Uint8Array!==undefined?Uint8Array:null, | ||
global.Int16Array!==undefined?Int16Array:null, | ||
global.Uint16Array!==undefined?Uint16Array:null, | ||
global.Int32Array!==undefined?Int32Array:null, | ||
global.Uint32Array!==undefined?Uint32Array:null, | ||
global.Float32Array!==undefined?Float32Array:null, | ||
global.Float64Array!==undefined?Float64Array:null, | ||
global.DataView!==undefined?DataView:null, | ||
AssertionError, | ||
global]; | ||
global, | ||
events | ||
]; | ||
@@ -81,0 +83,0 @@ if (global.errno) { |
@@ -171,4 +171,23 @@ var simpleEvents = require('nodeunit').testCase; | ||
}, | ||
'9. onAny alias' : function (test) { | ||
var emitter = this.emitter; | ||
var type = 'somelistenerbar'; | ||
var f = function () { | ||
test.ok(true, 'the event was fired'); | ||
}; | ||
emitter.on(f); | ||
emitter.emit('foo'); | ||
emitter.emit('bar'); | ||
test.expect(2); | ||
test.done(); | ||
} | ||
}); |
@@ -211,4 +211,75 @@ var simpleEvents= require('nodeunit').testCase; | ||
test.done(); | ||
}, | ||
'8. Its ok to listen on wildcard, so it is ok to remove it.' : function (test) { | ||
var emitter = this.emitter, | ||
type1 = '*.wild.card', | ||
type2 = 'just.another.event', | ||
listeners; | ||
var f = function () { | ||
test.ok(true, 'event was raised'); | ||
}; | ||
emitter.on(type2, f); | ||
emitter.on(type1, f); | ||
//remove | ||
emitter.removeListener(type1, f); | ||
listeners = emitter.listeners(type1); | ||
test.equal(listeners.length, 0, 'should be 0'); | ||
test.expect(1); | ||
test.done(); | ||
}, | ||
'9. And (8) should not depend on order of listening.' : function (test) { | ||
var emitter = this.emitter, | ||
type1 = '*.wild.card', | ||
type2 = 'just.another.event', | ||
listeners; | ||
var f = function () { | ||
test.ok(true, 'event was raised'); | ||
}; | ||
emitter.on(type1, f); | ||
emitter.on(type2, f); | ||
//remove | ||
emitter.removeListener(type1, f); | ||
listeners = emitter.listeners(type1); | ||
test.equal(listeners.length, 0, 'should be 0'); | ||
test.expect(1); | ||
test.done(); | ||
}, | ||
'10. Reporting many listeners on wildcard all should removed.' : function (test) { | ||
var emitter = this.emitter, | ||
type1 = '*.wild.card', | ||
type2 = 'exact.wild.card', | ||
listeners; | ||
var f = function () { | ||
test.ok(true, 'event was raised'); | ||
}; | ||
emitter.on(type1, f); | ||
emitter.on(type2, f); | ||
// check number of listeners by wild card | ||
listeners = emitter.listeners(type1); | ||
test.equal(listeners.length, 2, 'should only have 2'); | ||
// remove by wild card should remove both | ||
emitter.removeListener(type1, f); | ||
listeners = emitter.listeners(type1); | ||
test.equal(listeners.length, 0, 'should be 0'); | ||
test.expect(2); | ||
test.done(); | ||
} | ||
}); |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
97802
2649
0