nanoevents
Advanced tools
Comparing version 0.4.0 to 0.4.1
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 0.4.1 | ||
* Fix clashing with `Object` methods (by Anton Khlynovskiy). | ||
* Reduce size (by Anton Khlynovskiy). | ||
## 0.4 | ||
@@ -5,0 +9,0 @@ * Add `nanoevents/unbind-all` helper (by Igor Deryabin). |
53
index.js
@@ -33,3 +33,3 @@ /** | ||
function add (events, event, cb) { | ||
function add (events, event, cb, once) { | ||
if (typeof cb !== 'function') { | ||
@@ -39,13 +39,17 @@ throw new Error('Listener must be a function') | ||
var added = true | ||
var l = { fn: cb } | ||
l.rm = function () { | ||
if (!added) return | ||
added = false | ||
var list = events[event] | ||
if (list.length > 1) { | ||
list.splice(list.indexOf(l), 1) | ||
} else { | ||
delete events[event] | ||
var listener = { | ||
fn: cb, | ||
once: once, | ||
rm: function () { | ||
var list = events[event] | ||
if (list) { | ||
var index = list.indexOf(listener) | ||
if (index > -1) { | ||
if (list[1]) { // list[1] === list.length > 1 | ||
list.splice(index, 1) | ||
} else { | ||
delete events[event] | ||
} | ||
} | ||
} | ||
} | ||
@@ -55,7 +59,8 @@ } | ||
if (events[event]) { | ||
events[event].push(l) | ||
events[event].push(listener) | ||
} else { | ||
events[event] = [l] | ||
events[event] = [listener] | ||
} | ||
return l | ||
return listener | ||
} | ||
@@ -100,5 +105,3 @@ | ||
once: function once (event, cb) { | ||
var l = add(this.events, event, cb) | ||
l.once = true | ||
return l.rm | ||
return add(this.events, event, cb, true).rm | ||
}, | ||
@@ -119,12 +122,10 @@ | ||
var list = this.events[event] | ||
if (!list || !list.length) return false | ||
if (!list || !list[0]) return false // list[0] === Array.isArray(list) | ||
var copy = list.slice(0) | ||
list = list.slice() | ||
var args = [].slice.call(arguments, 1) | ||
for (var i = 0; i < copy.length; i++) { | ||
var l = copy[i] | ||
l.fn.apply(this, args) | ||
if (l.once) l.rm() | ||
var args = list.slice.call(arguments, 1) | ||
for (var i = 0; list[i]; i++) { // list[i] === i < list.length | ||
list[i].fn.apply(this, args) | ||
if (list[i].once) list[i].rm() | ||
} | ||
@@ -131,0 +132,0 @@ |
{ | ||
"name": "nanoevents", | ||
"version": "0.4.0", | ||
"description": "Small and simple events API", | ||
"version": "0.4.1", | ||
"description": "Simple and tiny (254 bytes) event emitter library", | ||
"keywords": [ | ||
@@ -24,3 +24,3 @@ "EventEmitter", | ||
"eslint": "^4.6.1", | ||
"eslint-config-logux": "^16.1.1", | ||
"eslint-config-logux": "^16.2.0", | ||
"eslint-config-standard": "^10.2.1", | ||
@@ -38,5 +38,5 @@ "eslint-plugin-es5": "^1.1.0", | ||
"pre-commit": "^1.2.2", | ||
"rimraf": "^2.6.1", | ||
"size-limit": "^0.11.1", | ||
"yaspeller-ci": "^0.6.0" | ||
"rimraf": "^2.6.2", | ||
"size-limit": "^0.11.3", | ||
"yaspeller-ci": "^0.7.0" | ||
}, | ||
@@ -67,3 +67,3 @@ "scripts": { | ||
"path": "index.js", | ||
"limit": "270 B" | ||
"limit": "254 B" | ||
} | ||
@@ -70,0 +70,0 @@ ], |
# Nano Events | ||
Small and simple events API. | ||
Simple and tiny event emitter library for JavaScript. | ||
* No node.js [EventEmitter] compatibility. | ||
* Only 270 bytes (minified and gzipped). It uses [Size Limit] to control size. | ||
* Only 254 bytes (minified and gzipped). It uses [Size Limit] to control size. | ||
* `on` and `once` methods return `unbind` function. You don’t need to save | ||
@@ -8,0 +8,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
9023
133