nanoevents
Advanced tools
Comparing version 0.1.0 to 0.2.0
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 0.2 | ||
* Throw a error on non-function listener. | ||
## 0.1 | ||
* Initial release. |
41
index.js
@@ -22,3 +22,3 @@ /** | ||
*/ | ||
function NanoEvents () { | ||
module.exports = function NanoEvents () { | ||
/** | ||
@@ -32,6 +32,10 @@ * Event names in keys and arrays with listeners in values. | ||
function add (events, event, cb) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('Expected event listener to be a function') | ||
} | ||
var added = true | ||
var listener = { fn: cb } | ||
var l = { fn: cb } | ||
listener.rm = function () { | ||
l.rm = function () { | ||
if (!added) return | ||
@@ -41,3 +45,3 @@ added = false | ||
if (list.length > 1) { | ||
list.splice(list.indexOf(listener), 1) | ||
list.splice(list.indexOf(l), 1) | ||
} else { | ||
@@ -49,19 +53,11 @@ delete events[event] | ||
if (events[event]) { | ||
events[event].push(listener) | ||
events[event].push(l) | ||
} else { | ||
events[event] = [listener] | ||
events[event] = [l] | ||
} | ||
return listener | ||
return l | ||
} | ||
function run (l, args) { | ||
var array = new Array(args.length - 1) | ||
for (var i = 1; i < args.length; i++) { | ||
array[i - 1] = args[i] | ||
} | ||
return l.fn.apply(this, array) | ||
} | ||
module.exports.prototype = { | ||
NanoEvents.prototype = { | ||
/** | ||
@@ -122,4 +118,7 @@ * Add a listener for a given event. | ||
var i | ||
var rm = [] | ||
var args = new Array(arguments.length - 1) | ||
for (var i = 1; i < arguments.length; i++) { | ||
args[i - 1] = arguments[i] | ||
} | ||
@@ -129,8 +128,6 @@ for (i = 0; i < list.length; i++) { | ||
if (l.once) rm.push(l) | ||
run(l, arguments) | ||
l.fn.apply(this, args) | ||
} | ||
for (i = 0; i < rm.length; i++) { | ||
rm[i].rm() | ||
} | ||
for (i = 0; i < rm.length; i++) rm[i].rm() | ||
@@ -141,3 +138,1 @@ return true | ||
} | ||
module.exports = NanoEvents |
{ | ||
"name": "nanoevents", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Small and simple events API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,3 +6,3 @@ # Nano Events | ||
* No node.js [EventEmitter] compatibility. | ||
* Only 923 bytes after Uglify. | ||
* Only 921 bytes after Uglify. | ||
* `on` and `once` methods return `unbind` function. You don’t need to save | ||
@@ -9,0 +9,0 @@ callback to variable for `removeListener`. |
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
7491
118