Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nodeart/event_emitter

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nodeart/event_emitter - npm Package Compare versions

Comparing version 1.0.6 to 1.1.0

12

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc