@nodeart/event_emitter
Advanced tools
Comparing version 1.0.6 to 1.1.0
{ | ||
"name": "@nodeart/event_emitter", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Simple EventEmitter pattern", | ||
@@ -12,3 +12,2 @@ "main": "./src/index.js", | ||
"start": "node ./src/index.js", | ||
"build": "gulp build", | ||
"test": "node ./tests.js" | ||
@@ -22,10 +21,3 @@ }, | ||
], | ||
"devDependencies": { | ||
"babel-preset-es2015": "^6.22.0", | ||
"gulp": "^3.9.1", | ||
"gulp-babel": "^6.1.2", | ||
"gulp-browserify": "^0.5.1", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-uglify": "^2.0.1" | ||
}, | ||
"devDependencies": {}, | ||
"engines": { | ||
@@ -32,0 +24,0 @@ "node": ">=6.0.0" |
@@ -0,0 +0,0 @@ [![bitHound Overall Score](https://www.bithound.io/github/NodeArt/EventEmitter/badges/score.svg)](https://www.bithound.io/github/NodeArt/EventEmitter) |
124
src/index.js
@@ -0,32 +1,93 @@ | ||
/** | ||
* @author Ivan Prodaiko <prodaiko94@gmail.com> | ||
*/ | ||
"use strict"; | ||
const subscribe = once => { | ||
const subscribe = times => { | ||
return function(eventName, ...args) { | ||
this.events[eventName] = args.reduce((acc, fn) => { | ||
acc.push({ fn: fn, once: once }); | ||
this._events[eventName] = args.reduce((acc, fn) => { | ||
const toPush = { fn }; | ||
if (times) { | ||
Object.assign(toPush, { times, count: 0 }); | ||
} | ||
acc.push(toPush); | ||
return acc; | ||
}, this.events[eventName] || []); | ||
}, this._events[eventName] || []); | ||
return this; | ||
}; | ||
}, | ||
on = subscribe(false), | ||
once = subscribe(true); | ||
on = subscribe(), | ||
once = subscribe(1); | ||
/** | ||
* Creates a new EventEmitter | ||
* @constructor | ||
*/ | ||
function EventEmitter() { | ||
this.events = {}; | ||
/** | ||
* @private _events | ||
*/ | ||
Object.defineProperty(this, '_events', { value: {}, writable: true }); | ||
} | ||
EventEmitter.prototype.on = function (...args) { | ||
return on.apply(this, args); | ||
/** | ||
* | ||
* @param eventName | ||
* @type string | ||
* @param args | ||
* @type Function | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.on = function (eventName, ...args) { | ||
return on.apply(this, [eventName, ...args]); | ||
}; | ||
EventEmitter.prototype.once = function (...args) { | ||
return once.apply(this, args); | ||
/** | ||
* | ||
* @param eventName | ||
* @type string | ||
* @param args | ||
* @type Function | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.once = function (eventName, ...args) { | ||
return once.apply(this, [eventName, ...args]); | ||
}; | ||
/** | ||
* | ||
* @param eventName | ||
* @type string | ||
* @param times | ||
* @type number | ||
* @param args | ||
* @type Function | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.times = function (eventName, times, ...args) { | ||
return times > 0 ? subscribe(times).apply(this, [eventName, ...args]) : this; | ||
}; | ||
/** | ||
* | ||
* @param eventName | ||
* @type string | ||
* @param ctx | ||
* @param args | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.emit = function(eventName, ctx, ...args) { | ||
if (!this.events[eventName]) return this; | ||
this.events[eventName] = this.events[eventName].filter(elem => { | ||
if (!this._events[eventName]) return this; | ||
this._events[eventName] = this._events[eventName].filter(elem => { | ||
elem.fn.apply(ctx, args); | ||
return !elem.once; | ||
if (elem.times) { | ||
elem.count += 1; | ||
} | ||
return elem.count ? (elem.times !== elem.count) : true; | ||
}); | ||
@@ -36,13 +97,21 @@ return this; | ||
/** | ||
* | ||
* @param eventName | ||
* @type string | ||
* @param fns | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.off = function(eventName, ...fns) { | ||
if (!this.events[eventName]) return this; | ||
if (!this._events[eventName]) return this; | ||
if (fns.length) { | ||
const tasksLeft = this.events[eventName].filter(listener => !fns.includes(listener.fn)); | ||
const tasksLeft = this._events[eventName].filter(listener => !fns.includes(listener.fn)); | ||
if (tasksLeft.length) { | ||
this.events[eventName] = tasksLeft; | ||
this._events[eventName] = tasksLeft; | ||
} else { | ||
delete this.events[eventName]; | ||
delete this._events[eventName]; | ||
} | ||
} else { | ||
delete this.events[eventName]; | ||
delete this._events[eventName]; | ||
} | ||
@@ -52,7 +121,22 @@ return this; | ||
/** | ||
* | ||
* @return {EventEmitter} | ||
*/ | ||
EventEmitter.prototype.offAll = function () { | ||
this.events = {}; | ||
this._events = {}; | ||
return this; | ||
}; | ||
/** | ||
* | ||
* @param successor | ||
* @type Object | ||
*/ | ||
EventEmitter.extend = function (successor) { | ||
EventEmitter.call(successor); | ||
Object.setPrototypeOf(successor, EventEmitter.prototype) | ||
}; | ||
if (typeof module !== 'undefined' && module.exports) { | ||
@@ -59,0 +143,0 @@ module.exports = EventEmitter; |
@@ -27,3 +27,3 @@ /** | ||
assert.equal(Object.keys(events.events['event1']).length, 2, 'Once test error'); | ||
assert.equal(Object.keys(events._events['event1']).length, 2, 'Once test error'); | ||
@@ -37,3 +37,3 @@ const fn = (...args) => assert.deepEqual(args, [1, 2, 3], 'Args test error'); | ||
assert.equal(events.events['event2'], undefined, 'Off test error'); | ||
assert.equal(events._events['event2'], undefined, 'Off test error'); | ||
@@ -44,2 +44,2 @@ events | ||
assert.deepEqual(events.events, {}, 'OffAll test error'); | ||
assert.deepEqual(events._events, {}, 'OffAll test error'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
0
1
0
6807
7
157
1