eventemitter2
Advanced tools
Comparing version 0.4.1 to 0.4.3
@@ -1,5 +0,6 @@ | ||
;!function(exports, undefined) { | ||
var isArray = Array.isArray; | ||
var isArray = Array.isArray ? Array.isArray : function _isArray(obj) { | ||
return Object.prototype.toString.call(obj) === "[object Array]"; | ||
}; | ||
var defaultMaxListeners = 10; | ||
@@ -12,7 +13,5 @@ | ||
function configure(conf) { | ||
if (conf) { | ||
this.wildcard = conf.wildcard; | ||
this.delimiter = conf.delimiter || '.'; | ||
conf.delimiter && (this.delimiter = conf.delimiter); | ||
conf.wildcard && (this.wildcard = conf.wildcard); | ||
if (this.wildcard) { | ||
@@ -33,6 +32,5 @@ this.listenerTree = new Object; | ||
} | ||
var listeners; | ||
if (i === type.length && tree._listeners) { | ||
var listeners, leaf, len, branch, xTree, xxTree, isolatedBranch, endReached, | ||
typeLength = type.length, currentType = type[i], nextType = type[i+1]; | ||
if (i === typeLength && tree._listeners) { | ||
// | ||
@@ -46,3 +44,3 @@ // If at the end of the event(s) list and the tree has listeners | ||
} else { | ||
for (var leaf = 0, len = tree._listeners.length; leaf < len; leaf++) { | ||
for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) { | ||
handlers && handlers.push(tree._listeners[leaf]); | ||
@@ -54,3 +52,3 @@ } | ||
if (type[i] === '*' || tree[type[i]]) { | ||
if ((currentType === '*' || currentType === '**') || tree[currentType]) { | ||
// | ||
@@ -60,4 +58,4 @@ // If the event emitted is '*' at this part | ||
// | ||
if (type[i] === '*') { | ||
for (var branch in tree) { | ||
if (currentType === '*') { | ||
for (branch in tree) { | ||
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) { | ||
@@ -68,9 +66,32 @@ listeners = searchListenerTree(handlers, type, tree[branch], i+1); | ||
return listeners; | ||
} else if(currentType === '**') { | ||
endReached = (i+1 === typeLength || (i+2 === typeLength && nextType === '*')); | ||
if(endReached && tree._listeners) { | ||
// The next element has a _listeners, add it to the handlers. | ||
listeners = searchListenerTree(handlers, type, tree, typeLength); | ||
} | ||
for (branch in tree) { | ||
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) { | ||
if(branch === '*' || branch === '**') { | ||
if(tree[branch]._listeners && !endReached) { | ||
listeners = searchListenerTree(handlers, type, tree[branch], typeLength); | ||
} | ||
listeners = searchListenerTree(handlers, type, tree[branch], i); | ||
} else if(branch === nextType) { | ||
listeners = 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); | ||
} | ||
} | ||
} | ||
return listeners; | ||
} | ||
listeners = searchListenerTree(handlers, type, tree[type[i]], i+1); | ||
listeners = searchListenerTree(handlers, type, tree[currentType], i+1); | ||
} | ||
if (tree['*']) { | ||
xTree = tree['*']; | ||
if (xTree) { | ||
// | ||
@@ -80,7 +101,39 @@ // If the listener tree will allow any match for this part, | ||
// | ||
searchListenerTree(handlers, type, tree['*'], i+1); | ||
searchListenerTree(handlers, type, xTree, i+1); | ||
} | ||
xxTree = tree['**']; | ||
if(xxTree) { | ||
if(i < typeLength) { | ||
if(xxTree._listeners) { | ||
// If we have a listener on a '**', it will catch all, so add its handler. | ||
searchListenerTree(handlers, type, xxTree, typeLength); | ||
} | ||
// Build arrays of matching next branches and others. | ||
for(branch in xxTree) { | ||
if(branch !== '_listeners' && xxTree.hasOwnProperty(branch)) { | ||
if(branch === nextType) { | ||
// We know the next element will match, so jump twice. | ||
searchListenerTree(handlers, type, xxTree[branch], i+2); | ||
} else if(branch === currentType) { | ||
// Current node matches, move into the tree. | ||
searchListenerTree(handlers, type, xxTree[branch], i+1); | ||
} else { | ||
isolatedBranch = {}; | ||
isolatedBranch[branch] = xxTree[branch]; | ||
searchListenerTree(handlers, type, { '**': isolatedBranch }, i+1); | ||
} | ||
} | ||
} | ||
} else if(xxTree._listeners) { | ||
// We have reached the end and still on a '**' | ||
searchListenerTree(handlers, type, xxTree, typeLength); | ||
} else if(xxTree['*'] && xxTree['*']._listeners) { | ||
searchListenerTree(handlers, type, xxTree['*'], typeLength); | ||
} | ||
} | ||
return listeners; | ||
}; | ||
} | ||
@@ -90,2 +143,11 @@ function growListenerTree(type, listener) { | ||
type = typeof type === 'string' ? type.split(this.delimiter) : type.slice(); | ||
// | ||
// Looks for too consecutive '**', if so, don't add the event at all. | ||
// | ||
for(var i = 0, len = type.length; i+1 < len; i++) { | ||
if(type[i] === '**' && type[i+1] === '**') { | ||
return; | ||
} | ||
} | ||
@@ -117,3 +179,3 @@ var tree = this.listenerTree; | ||
var m = defaultMaxListeners; | ||
var m = this._events.maxListeners || defaultMaxListeners; | ||
@@ -145,2 +207,4 @@ if (m > 0 && tree._listeners.length > m) { | ||
EventEmitter.prototype.delimiter = '.'; | ||
EventEmitter.prototype.setMaxListeners = function(n) { | ||
@@ -169,3 +233,3 @@ this._events || init.call(this); | ||
} | ||
fn.apply(null, arguments); | ||
fn.apply(this, arguments); | ||
}; | ||
@@ -265,2 +329,5 @@ | ||
EventEmitter.prototype.on = function(type, listener) { | ||
if (typeof listener !== 'function') { | ||
throw new Error('on only accepts instances of Function'); | ||
} | ||
this._events || init.call(this); | ||
@@ -463,4 +530,10 @@ | ||
exports.EventEmitter2 = EventEmitter; | ||
if (typeof define === 'function' && define.amd) { | ||
define(function() { | ||
return EventEmitter; | ||
}); | ||
} else { | ||
exports.EventEmitter2 = EventEmitter; | ||
} | ||
}(typeof exports === 'undefined' ? window : exports); | ||
}(typeof window !== 'undefined' ? window : exports); |
{ | ||
"name": "eventemitter2", | ||
"version": "0.4.1", | ||
"version": "0.4.3", | ||
"description": "A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support.", | ||
@@ -11,3 +11,3 @@ "keywords": ["event", "events", "emitter", "eventemitter"], | ||
], | ||
"contributers": [ | ||
"contributors": [ | ||
"Charlie Robbins <charlie@nodejitsu.com> http://twitter.com/indexzero", | ||
@@ -19,2 +19,3 @@ "Jameson Lee <jameson@nodejitsu.com> http://twitter.com/Jamesonjlee", | ||
"licenses": [{"type": "MIT"}], | ||
"homepage": "https://github.com/hij1nx/EventEmitter2", | ||
"repositories": [{ | ||
@@ -21,0 +22,0 @@ "type": "git", |
@@ -1,2 +0,1 @@ | ||
# EventEmitter2 | ||
@@ -6,20 +5,2 @@ | ||
# ATTENTION! BREAKING CHANGES TO BE COMPATIBLE WITH NODE.JS EVENT EMITTER. | ||
Wrong | ||
```javascript | ||
server.on('foo.*', function(event, value1, value2) { // DOES NOT PASS THE EVENT AS FIRST ARGUMENT. | ||
console.log(event, value1, value2); | ||
}); | ||
``` | ||
Right | ||
```javascript | ||
server.on('foo.*', function(value1, value2) { | ||
console.log(this.event, value1, value2); | ||
}); | ||
``` | ||
## Features | ||
@@ -208,7 +189,2 @@ | ||
#### emitter.emitAll(event, [arg1], [arg2], [...]) | ||
Execute each of the listeners that may be listening for the specified event name in order with the list of arguments. | ||
## Test coverage | ||
@@ -215,0 +191,0 @@ |
@@ -67,2 +67,13 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
process, | ||
ArrayBuffer, | ||
Int8Array, | ||
Uint8Array, | ||
Int16Array, | ||
Uint16Array, | ||
Int32Array, | ||
Uint32Array, | ||
Float32Array, | ||
Float64Array, | ||
DataView, | ||
AssertionError, | ||
global]; | ||
@@ -69,0 +80,0 @@ |
@@ -246,4 +246,43 @@ | ||
test.done(); | ||
}, | ||
'11. Listeners with multi-level wildcards': function (test) { | ||
var emitter = this.emitter; | ||
var i = 0; | ||
var f = function (n) { | ||
return function() { | ||
//console.log('Event', n, 'fired by', this.event); | ||
test.ok(true, 'the event was fired'); | ||
}; | ||
}; | ||
emitter.on('**.test', f(i++)); // 0: 0 + 1 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 | ||
emitter.on('**.bar.**', f(i++)); // 1: 0 + 1 + 1 + 1 + 1 + 0 + 0 + 1 + 1 + 1 | ||
emitter.on('**.*', f(i++)); // 2: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 | ||
emitter.on('*.**', f(i++)); // 3: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 | ||
emitter.on('**', f(i++)); // 4: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 | ||
emitter.on('other.**', f(i++)); // 5: 1 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 1 + 1 | ||
emitter.on('foo.**.test', f(i++)); // 6: 0 + 1 + 0 + 0 + 1 + 0 + 1 + 1 + 1 + 1 | ||
emitter.on('test.**', f(i++)); // 7: 0 + 0 + 0 + 0 + 1 + 1 + 0 + 0 + 1 + 1 | ||
// Add forbidden patterns for safety purpose. | ||
emitter.on('**.**', f(i++)); | ||
emitter.on('a.b.**.**', f(i++)); | ||
emitter.on('**.**.a.b', f(i++)); | ||
emitter.on('a.b.**.**.a.b', f(i++)); | ||
emitter.emit('other.emit'); // 4 | ||
emitter.emit('foo.bar.test'); // 6 | ||
emitter.emit('foo.bar.test.bar.foo.test.foo'); // 4 | ||
emitter.emit('bar.bar.bar.bar.bar.bar'); // 4 | ||
emitter.emit('**.*'); // 8 | ||
emitter.emit('test'); // 5 | ||
emitter.emit('foo.test'); // 5 | ||
emitter.emit('foo.**.*'); // 6 | ||
emitter.emit('**.test'); // 8 | ||
emitter.emit('**.test.**'); // 8 | ||
//emitter.emit('*.**.test.**.a'); // 0 | ||
test.expect(58); | ||
test.done(); | ||
} | ||
}); |
@@ -804,2 +804,27 @@ var basicEvents = require('nodeunit').testCase; | ||
} */ | ||
'29. No warning should be raised if we set maxListener to be greater before adding' : function (test) { | ||
var emitter = this.emitter; | ||
var type = 'test29.*'; | ||
// set to 20 | ||
emitter.setMaxListeners(20); | ||
for (var i = 0; i < 15 ; i++) { | ||
emitter.on(type, function () { | ||
test.ok(true, 'event was raised'); | ||
}); | ||
} | ||
// require('eyes').inspect(emitter._events); | ||
var listeners = emitter.listeners(type); | ||
test.equal(listeners.length, 15, 'should have 15'); | ||
test.ok(!(emitter.listenerTree[ 'test29' ]['*']._listeners.warned), 'should not have been set'); | ||
test.expect(2); | ||
test.done(); | ||
} | ||
}); |
@@ -176,4 +176,42 @@ var simpleEvents = require('nodeunit').testCase; | ||
test.done(); | ||
}, | ||
'9. Listeners on **, **::*, **::test with emissions from foo::test and other::emit': function (test) { | ||
var emitter = this.emitter; | ||
var f = function () { | ||
test.ok(true, 'the event was fired'); | ||
}; | ||
emitter.on('**::test', f); | ||
emitter.on('**::*', f); | ||
emitter.on('**', f); | ||
emitter.emit('other::emit'); // 2 | ||
emitter.emit('foo::test'); // 3 | ||
test.expect(5); | ||
test.done(); | ||
}, | ||
'10. Listeners on **, **::*, foo.test with emissions from **, **::* and foo.test': function (test) { | ||
var emitter = this.emitter, i = 0; | ||
var f = function (n) { | ||
return function() { | ||
//console.log(n, this.event); | ||
test.ok(true, 'the event was fired'); | ||
}; | ||
}; | ||
emitter.on('foo::test', f(i++)); | ||
emitter.on('**::*', f(i++)); | ||
emitter.on('**', f(i++)); | ||
emitter.emit('**::*'); // 3 | ||
emitter.emit('foo::test'); // 3 | ||
emitter.emit('**'); // 3 | ||
test.expect(9); | ||
test.done(); | ||
} | ||
}); |
@@ -29,2 +29,3 @@ | ||
var countWildcard = 0; | ||
var counMultiLevelWildcard = 0; | ||
var countAny = 0; | ||
@@ -37,5 +38,10 @@ | ||
++countWildcard; | ||
console.log(this.event, name) | ||
console.log(this.event, name); | ||
assert.equal(this.event, name); | ||
}); | ||
e.on('**', function(name) { | ||
++counMultiLevelWildcard; | ||
console.log(this.event, name); | ||
assert.equal(this.event, name); | ||
}); | ||
e.onAny(function(name) { | ||
@@ -50,3 +56,4 @@ ++countAny; | ||
assert.equal(countWildcard, 2); | ||
assert.equal(counMultiLevelWildcard, 2); | ||
assert.equal(countAny, 2); | ||
}); |
@@ -11,3 +11,5 @@ var basicEvents = require('nodeunit').testCase; | ||
testName + '.ns1.ns2', | ||
testName + '.ns2.*' | ||
testName + '.ns2.*', | ||
testName + '.**', | ||
testName = '.ns2.**' | ||
]; | ||
@@ -22,3 +24,3 @@ | ||
return eventNames; | ||
}; | ||
} | ||
@@ -25,0 +27,0 @@ module.exports = basicEvents({ |
@@ -161,4 +161,37 @@ var simpleEvents = require('nodeunit').testCase; | ||
test.done(); | ||
}, | ||
'8. Emitting with a multi-level wildcard on once': function(test) { | ||
var emitter = this.emitter, i = 0; | ||
var type = 'test1.**'; | ||
var functionA = function(n) { | ||
return function() { | ||
//console.log(n, this.event); | ||
test.ok(true, 'Event was fired'); | ||
}; | ||
} | ||
emitter.once(type, functionA(i++)); | ||
emitter.on(type,functionA(i++)); | ||
emitter.emit(type); //2 | ||
emitter.emit(type); //1 | ||
test.expect(3); | ||
test.done(); | ||
}, | ||
'9. Emitting with a multi-level wildcard targeted at once' : function (test) { | ||
var emitter = this.emitter; | ||
var type = 'test1.foo.bar'; | ||
var type2 = 'test1.**'; | ||
var functionA = function() { test.ok(true, 'Event was fired'); }; | ||
emitter.once(type, functionA); | ||
emitter.emit(type2); | ||
emitter.emit(type2); | ||
test.expect(1); | ||
test.done(); | ||
} | ||
}); |
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
94723
2567
1
0
203
1