nanoevents
Advanced tools
Comparing version
# Change Log | ||
This project adheres to [Semantic Versioning](http://semver.org/). | ||
## 1.0 | ||
* Remove `NanoEvents#once` method. | ||
* `NanoEvents#emit` doesn’t return boolean anymore. | ||
* Check arguments types only in development. | ||
* Reduce size. | ||
## 0.4.1 | ||
@@ -5,0 +11,0 @@ * Fix clashing with `Object` methods (by Anton Khlynovskiy). |
81
index.js
@@ -12,5 +12,2 @@ /** | ||
* } | ||
* once() { | ||
* return this.emitter.once.apply(this.events, arguments) | ||
* } | ||
* tick() { | ||
@@ -34,34 +31,2 @@ * this.emitter.emit('tick') | ||
function add (events, event, cb, once) { | ||
if (typeof cb !== 'function') { | ||
throw new Error('Listener must be a function') | ||
} | ||
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] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (events[event]) { | ||
events[event].push(listener) | ||
} else { | ||
events[event] = [listener] | ||
} | ||
return listener | ||
} | ||
NanoEvents.prototype = { | ||
@@ -87,20 +52,17 @@ | ||
on: function on (event, cb) { | ||
return add(this.events, event, cb).rm | ||
}, | ||
if (process.env.NODE_ENV !== 'production' && typeof cb !== 'function') { | ||
throw new Error('Listener must be a function') | ||
} | ||
/** | ||
* Add a one-time listener for a given event. | ||
* | ||
* @param {string} event The event name. | ||
* @param {function} cb The listener function. | ||
* | ||
* @return {function} Unbind listener from event. | ||
* | ||
* @example | ||
* const unbind = ee.once('tick', (tickType, tickDuration) => { | ||
* works = true | ||
* }) | ||
*/ | ||
once: function once (event, cb) { | ||
return add(this.events, event, cb, true).rm | ||
var events = this.events | ||
if (events[event]) { | ||
events[event].push(cb) | ||
} else { | ||
events[event] = [cb] | ||
} | ||
return function () { | ||
events[event].splice(events[event].indexOf(cb) >>> 0, 1) | ||
} | ||
}, | ||
@@ -114,3 +76,3 @@ | ||
* | ||
* @returns {boolean} `true` if the event had listeners, else `false`. | ||
* @returns {undefined} | ||
* | ||
@@ -122,13 +84,8 @@ * @example | ||
var list = this.events[event] | ||
if (!list || !list[0]) return false // list[0] === Array.isArray(list) | ||
if (!list || !list[0]) return // list[0] === Array.isArray(list) | ||
list = list.slice() | ||
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() | ||
} | ||
return true | ||
list.slice().map(function (i) { | ||
i.apply(this, args) | ||
}) | ||
} | ||
@@ -135,0 +92,0 @@ } |
{ | ||
"name": "nanoevents", | ||
"version": "0.4.1", | ||
"description": "Simple and tiny (254 bytes) event emitter library", | ||
"version": "1.0.0", | ||
"description": "Simple and tiny (130 bytes) event emitter library", | ||
"keywords": [ | ||
@@ -11,3 +11,2 @@ "EventEmitter", | ||
"event", | ||
"once", | ||
"pub/sub", | ||
@@ -24,3 +23,3 @@ "publish", | ||
"docdash": "^0.4.0", | ||
"eslint": "^4.6.1", | ||
"eslint": "^4.7.0", | ||
"eslint-config-logux": "^16.2.0", | ||
@@ -30,3 +29,3 @@ "eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-jest": "^21.0.2", | ||
"eslint-plugin-jest": "^21.1.0", | ||
"eslint-plugin-node": "^5.1.1", | ||
@@ -36,5 +35,5 @@ "eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"jest": "^21.0.2", | ||
"jsdoc": "^3.5.4", | ||
"lint-staged": "^4.1.3", | ||
"jest": "^21.1.0", | ||
"jsdoc": "^3.5.5", | ||
"lint-staged": "^4.2.1", | ||
"pre-commit": "^1.2.2", | ||
@@ -64,3 +63,6 @@ "rimraf": "^2.6.2", | ||
"eslintConfig": { | ||
"extends": "eslint-config-logux/browser" | ||
"extends": "eslint-config-logux/browser", | ||
"rules": { | ||
"no-invalid-this": "off" | ||
} | ||
}, | ||
@@ -70,3 +72,3 @@ "size-limit": [ | ||
"path": "index.js", | ||
"limit": "254 B" | ||
"limit": "130 B" | ||
} | ||
@@ -73,0 +75,0 @@ ], |
@@ -5,7 +5,7 @@ # Nano Events | ||
* No node.js [EventEmitter] compatibility. | ||
* 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 | ||
* No Node.js [EventEmitter] compatibility. | ||
* Only 130 bytes (minified and gzipped). It uses [Size Limit] to control size. | ||
* `on` method returns `unbind` function. You don’t need to save | ||
callback to variable for `removeListener`. | ||
* No aliases, just `emit`, `on`, `once` methods. | ||
* No aliases, just `emit` and `on` methods. | ||
@@ -19,5 +19,2 @@ ```js | ||
}) | ||
emitter.once('tick', () => { | ||
works = true | ||
}) | ||
@@ -53,5 +50,2 @@ function disable () { | ||
} | ||
once() { | ||
return this.emitter.once.apply(this.events, arguments) | ||
} | ||
tick() { | ||
@@ -66,17 +60,13 @@ this.emitter.emit('tick') | ||
There are 2 methods to add listener for specific event: | ||
`on` and one-time `once`. | ||
Use `on` method to add listener for specific event: | ||
```js | ||
emitter.on('tick', number => { | ||
console.log('on ' + number) | ||
console.log(number) | ||
}) | ||
emitter.once('tick', number => { | ||
console.log('once ' + number) | ||
}) | ||
emitter.emit('tick', 1) | ||
// Prints "on 1" and "once 1" | ||
// Prints "1" | ||
emitter.emit('tick', 2) | ||
// Prints "on 2" | ||
// Prints "2" | ||
``` | ||
@@ -87,3 +77,3 @@ | ||
Methods `on` and `once` return `unbind` function. Call it and this listener | ||
Methods `on` returns `unbind` function. Call it and this listener | ||
will be removed from event. | ||
@@ -90,0 +80,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
-50%7973
-11.64%94
-29.32%133
-6.99%