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

eventemitter3

Package Overview
Dependencies
Maintainers
3
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventemitter3 - npm Package Compare versions

Comparing version 2.0.3 to 3.0.0

6

index.d.ts

@@ -21,6 +21,10 @@ export as namespace EventEmitter;

*/
listeners(event: string | symbol, exists: boolean): Array<ListenerFn> | boolean;
listeners(event: string | symbol): Array<ListenerFn>;
/**
* Return the number of listeners listening to a given event.
*/
listenerCount(event: string | symbol): number;
/**
* Calls each of the listeners registered for a given event.

@@ -27,0 +31,0 @@ */

161

index.js

@@ -11,3 +11,3 @@ 'use strict';

* @constructor
* @api private
* @private
*/

@@ -37,6 +37,6 @@ function Events() {}

* @param {Function} fn The listener function.
* @param {Mixed} context The context to invoke the listener with.
* @param {*} context The context to invoke the listener with.
* @param {Boolean} [once=false] Specify if the listener is a one-time listener.
* @constructor
* @api private
* @private
*/

@@ -50,2 +50,40 @@ function EE(fn, context, once) {

/**
* Add a listener for a given event.
*
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
* @param {(String|Symbol)} event The event name.
* @param {Function} fn The listener function.
* @param {*} context The context to invoke the listener with.
* @param {Boolean} once Specify if the listener is a one-time listener.
* @returns {EventEmitter}
* @private
*/
function addListener(emitter, event, fn, context, once) {
if (typeof fn !== 'function') {
throw new TypeError('The listener must be a function');
}
var listener = new EE(fn, context || emitter, once)
, evt = prefix ? prefix + event : event;
if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
else emitter._events[evt] = [emitter._events[evt], listener];
return emitter;
}
/**
* Clear event by name.
*
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
* @param {(String|Symbol)} evt The Event name.
* @private
*/
function clearEvent(emitter, evt) {
if (--emitter._eventsCount === 0) emitter._events = new Events();
else delete emitter._events[evt];
}
/**
* Minimal `EventEmitter` interface that is molded against the Node.js

@@ -55,3 +93,3 @@ * `EventEmitter` interface.

* @constructor
* @api public
* @public
*/

@@ -68,3 +106,3 @@ function EventEmitter() {

* @returns {Array}
* @api public
* @public
*/

@@ -92,17 +130,15 @@ EventEmitter.prototype.eventNames = function eventNames() {

*
* @param {String|Symbol} event The event name.
* @param {Boolean} exists Only check if there are listeners.
* @returns {Array|Boolean}
* @api public
* @param {(String|Symbol)} event The event name.
* @returns {Array} The registered listeners.
* @public
*/
EventEmitter.prototype.listeners = function listeners(event, exists) {
EventEmitter.prototype.listeners = function listeners(event) {
var evt = prefix ? prefix + event : event
, available = this._events[evt];
, handlers = this._events[evt];
if (exists) return !!available;
if (!available) return [];
if (available.fn) return [available.fn];
if (!handlers) return [];
if (handlers.fn) return [handlers.fn];
for (var i = 0, l = available.length, ee = new Array(l); i < l; i++) {
ee[i] = available[i].fn;
for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
ee[i] = handlers[i].fn;
}

@@ -114,7 +150,23 @@

/**
* Return the number of listeners listening to a given event.
*
* @param {(String|Symbol)} event The event name.
* @returns {Number} The number of listeners.
* @public
*/
EventEmitter.prototype.listenerCount = function listenerCount(event) {
var evt = prefix ? prefix + event : event
, listeners = this._events[evt];
if (!listeners) return 0;
if (listeners.fn) return 1;
return listeners.length;
};
/**
* Calls each of the listeners registered for a given event.
*
* @param {String|Symbol} event The event name.
* @param {(String|Symbol)} event The event name.
* @returns {Boolean} `true` if the event had listeners, else `false`.
* @api public
* @public
*/

@@ -176,17 +228,10 @@ EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {

*
* @param {String|Symbol} event The event name.
* @param {(String|Symbol)} event The event name.
* @param {Function} fn The listener function.
* @param {Mixed} [context=this] The context to invoke the listener with.
* @param {*} [context=this] The context to invoke the listener with.
* @returns {EventEmitter} `this`.
* @api public
* @public
*/
EventEmitter.prototype.on = function on(event, fn, context) {
var listener = new EE(fn, context || this)
, evt = prefix ? prefix + event : event;
if (!this._events[evt]) this._events[evt] = listener, this._eventsCount++;
else if (!this._events[evt].fn) this._events[evt].push(listener);
else this._events[evt] = [this._events[evt], listener];
return this;
return addListener(this, event, fn, context, false);
};

@@ -197,17 +242,10 @@

*
* @param {String|Symbol} event The event name.
* @param {(String|Symbol)} event The event name.
* @param {Function} fn The listener function.
* @param {Mixed} [context=this] The context to invoke the listener with.
* @param {*} [context=this] The context to invoke the listener with.
* @returns {EventEmitter} `this`.
* @api public
* @public
*/
EventEmitter.prototype.once = function once(event, fn, context) {
var listener = new EE(fn, context || this, true)
, evt = prefix ? prefix + event : event;
if (!this._events[evt]) this._events[evt] = listener, this._eventsCount++;
else if (!this._events[evt].fn) this._events[evt].push(listener);
else this._events[evt] = [this._events[evt], listener];
return this;
return addListener(this, event, fn, context, true);
};

@@ -218,8 +256,8 @@

*
* @param {String|Symbol} event The event name.
* @param {(String|Symbol)} event The event name.
* @param {Function} fn Only remove the listeners that match this function.
* @param {Mixed} context Only remove the listeners that have this context.
* @param {*} context Only remove the listeners that have this context.
* @param {Boolean} once Only remove one-time listeners.
* @returns {EventEmitter} `this`.
* @api public
* @public
*/

@@ -231,4 +269,3 @@ EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {

if (!fn) {
if (--this._eventsCount === 0) this._events = new Events();
else delete this._events[evt];
clearEvent(this, evt);
return this;

@@ -241,8 +278,7 @@ }

if (
listeners.fn === fn
&& (!once || listeners.once)
&& (!context || listeners.context === context)
listeners.fn === fn &&
(!once || listeners.once) &&
(!context || listeners.context === context)
) {
if (--this._eventsCount === 0) this._events = new Events();
else delete this._events[evt];
clearEvent(this, evt);
}

@@ -252,5 +288,5 @@ } else {

if (
listeners[i].fn !== fn
|| (once && !listeners[i].once)
|| (context && listeners[i].context !== context)
listeners[i].fn !== fn ||
(once && !listeners[i].once) ||
(context && listeners[i].context !== context)
) {

@@ -265,4 +301,3 @@ events.push(listeners[i]);

if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
else if (--this._eventsCount === 0) this._events = new Events();
else delete this._events[evt];
else clearEvent(this, evt);
}

@@ -276,5 +311,5 @@

*
* @param {String|Symbol} [event] The event name.
* @param {(String|Symbol)} [event] The event name.
* @returns {EventEmitter} `this`.
* @api public
* @public
*/

@@ -286,6 +321,3 @@ EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {

evt = prefix ? prefix + event : event;
if (this._events[evt]) {
if (--this._eventsCount === 0) this._events = new Events();
else delete this._events[evt];
}
if (this._events[evt]) clearEvent(this, evt);
} else {

@@ -306,9 +338,2 @@ this._events = new Events();

//
// This function doesn't apply anymore.
//
EventEmitter.prototype.setMaxListeners = function setMaxListeners() {
return this;
};
//
// Expose the prefix.

@@ -315,0 +340,0 @@ //

{
"name": "eventemitter3",
"version": "2.0.3",
"version": "3.0.0",
"description": "EventEmitter3 focuses on performance while maintaining a Node.js AND browser compatible interface.",

@@ -8,9 +8,13 @@ "main": "index.js",

"scripts": {
"build": "mkdir -p umd && browserify index.js -s EventEmitter3 | uglifyjs -m -o umd/eventemitter3.min.js",
"build": "mkdir -p umd && browserify index.js -s EventEmitter3 | uglifyjs -cm -o umd/eventemitter3.min.js",
"benchmark": "find benchmarks/run -name '*.js' -exec benchmarks/start.sh {} \\;",
"test": "nyc --reporter=html --reporter=text mocha",
"test-browser": "zuul -- test.js",
"prepublish": "npm run build",
"sync": "node versions.js"
"test": "nyc --reporter=html --reporter=text mocha test/test.js",
"test-browser": "node test/browser.js",
"prepublishOnly": "npm run build"
},
"files": [
"index.js",
"index.d.ts",
"umd"
],
"repository": {

@@ -42,12 +46,12 @@ "type": "git",

},
"pre-commit": "sync, test",
"devDependencies": {
"assume": "~1.4.1",
"browserify": "~14.1.0",
"mocha": "~3.2.0",
"nyc": "~10.2.0",
"assume": "~1.5.0",
"browserify": "~14.5.0",
"mocha": "~4.0.0",
"nyc": "~11.3.0",
"pre-commit": "~1.2.0",
"uglify-js": "~2.8.20",
"zuul": "~3.11.1"
"sauce-browsers": "~1.0.0",
"sauce-test": "~1.3.3",
"uglify-js": "~3.2.0"
}
}

@@ -16,10 +16,7 @@ # EventEmitter3

listening.
- The `newListener` event is removed as the use-cases for this functionality are
really just edge cases.
- No `setMaxListeners` and its pointless memory leak warnings. If you want to
add `end` listeners you should be able to do that without modules complaining.
- No `listenerCount` method. Use `EE.listeners(event).length` instead.
- The `newListener` and `removeListener` events have been removed as they
are useful only in some uncommon use-cases.
- The `setMaxListeners`, `getMaxListeners`, `prependListener` and
`preperndOnceListener` methods are not available.
- Support for custom context for events so there is no need to use `fn.bind`.
- The `listeners` method can do existence checking instead of returning only
arrays.
- The `removeListener` method removes all matching listeners, not only the

@@ -36,7 +33,13 @@ first.

```bash
$ npm install --save eventemitter3 # npm
$ component install primus/eventemitter3 # Component
$ bower install eventemitter3 # Bower
$ npm install --save eventemitter3
```
## CDN
Recommended CDN:
```text
https://unpkg.com/eventemitter3@latest/umd/eventemitter3.min.js
```
## Usage

@@ -76,18 +79,2 @@

### Existence
To check if there is already a listener for a given event you can supply the
`listeners` method with an extra boolean argument. This will transform the
output from an array, to a boolean value which indicates if there are listeners
in place for the given event:
```js
var EE = new EventEmitter();
EE.once('event-name', function () {});
EE.on('another-event', function () {});
EE.listeners('event-name', true); // returns true
EE.listeners('unknown-name', true); // returns false
```
### Tests and benchmarks

@@ -94,0 +81,0 @@

@@ -1,1 +0,1 @@

(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var t;if(typeof window!=="undefined"){t=window}else if(typeof global!=="undefined"){t=global}else if(typeof self!=="undefined"){t=self}else{t=this}t.EventEmitter3=e()}})(function(){var e,t,n;return function e(t,n,r){function s(o,f){if(!n[o]){if(!t[o]){var u=typeof require=="function"&&require;if(!f&&u)return u(o,!0);if(i)return i(o,!0);var c=new Error("Cannot find module '"+o+"'");throw c.code="MODULE_NOT_FOUND",c}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(e,t,n){"use strict";var r=Object.prototype.hasOwnProperty,s="~";function i(){}if(Object.create){i.prototype=Object.create(null);if(!(new i).__proto__)s=false}function o(e,t,n){this.fn=e;this.context=t;this.once=n||false}function f(){this._events=new i;this._eventsCount=0}f.prototype.eventNames=function e(){var t=[],n,i;if(this._eventsCount===0)return t;for(i in n=this._events){if(r.call(n,i))t.push(s?i.slice(1):i)}if(Object.getOwnPropertySymbols){return t.concat(Object.getOwnPropertySymbols(n))}return t};f.prototype.listeners=function e(t,n){var r=s?s+t:t,i=this._events[r];if(n)return!!i;if(!i)return[];if(i.fn)return[i.fn];for(var o=0,f=i.length,u=new Array(f);o<f;o++){u[o]=i[o].fn}return u};f.prototype.emit=function e(t,n,r,i,o,f){var u=s?s+t:t;if(!this._events[u])return false;var c=this._events[u],l=arguments.length,h,v;if(c.fn){if(c.once)this.removeListener(t,c.fn,undefined,true);switch(l){case 1:return c.fn.call(c.context),true;case 2:return c.fn.call(c.context,n),true;case 3:return c.fn.call(c.context,n,r),true;case 4:return c.fn.call(c.context,n,r,i),true;case 5:return c.fn.call(c.context,n,r,i,o),true;case 6:return c.fn.call(c.context,n,r,i,o,f),true}for(v=1,h=new Array(l-1);v<l;v++){h[v-1]=arguments[v]}c.fn.apply(c.context,h)}else{var a=c.length,p;for(v=0;v<a;v++){if(c[v].once)this.removeListener(t,c[v].fn,undefined,true);switch(l){case 1:c[v].fn.call(c[v].context);break;case 2:c[v].fn.call(c[v].context,n);break;case 3:c[v].fn.call(c[v].context,n,r);break;case 4:c[v].fn.call(c[v].context,n,r,i);break;default:if(!h)for(p=1,h=new Array(l-1);p<l;p++){h[p-1]=arguments[p]}c[v].fn.apply(c[v].context,h)}}}return true};f.prototype.on=function e(t,n,r){var i=new o(n,r||this),f=s?s+t:t;if(!this._events[f])this._events[f]=i,this._eventsCount++;else if(!this._events[f].fn)this._events[f].push(i);else this._events[f]=[this._events[f],i];return this};f.prototype.once=function e(t,n,r){var i=new o(n,r||this,true),f=s?s+t:t;if(!this._events[f])this._events[f]=i,this._eventsCount++;else if(!this._events[f].fn)this._events[f].push(i);else this._events[f]=[this._events[f],i];return this};f.prototype.removeListener=function e(t,n,r,o){var f=s?s+t:t;if(!this._events[f])return this;if(!n){if(--this._eventsCount===0)this._events=new i;else delete this._events[f];return this}var u=this._events[f];if(u.fn){if(u.fn===n&&(!o||u.once)&&(!r||u.context===r)){if(--this._eventsCount===0)this._events=new i;else delete this._events[f]}}else{for(var c=0,l=[],h=u.length;c<h;c++){if(u[c].fn!==n||o&&!u[c].once||r&&u[c].context!==r){l.push(u[c])}}if(l.length)this._events[f]=l.length===1?l[0]:l;else if(--this._eventsCount===0)this._events=new i;else delete this._events[f]}return this};f.prototype.removeAllListeners=function e(t){var n;if(t){n=s?s+t:t;if(this._events[n]){if(--this._eventsCount===0)this._events=new i;else delete this._events[n]}}else{this._events=new i;this._eventsCount=0}return this};f.prototype.off=f.prototype.removeListener;f.prototype.addListener=f.prototype.on;f.prototype.setMaxListeners=function e(){return this};f.prefixed=s;f.EventEmitter=f;if("undefined"!==typeof t){t.exports=f}},{}]},{},[1])(1)});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).EventEmitter3=e()}}(function(){return function e(t,n,r){function o(s,f){if(!n[s]){if(!t[s]){var c="function"==typeof require&&require;if(!f&&c)return c(s,!0);if(i)return i(s,!0);var u=new Error("Cannot find module '"+s+"'");throw u.code="MODULE_NOT_FOUND",u}var a=n[s]={exports:{}};t[s][0].call(a.exports,function(e){var n=t[s][1][e];return o(n||e)},a,a.exports,e,t,n,r)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<r.length;s++)o(r[s]);return o}({1:[function(e,t,n){"use strict";function r(){}function o(e,t,n,r,o){if("function"!=typeof n)throw new TypeError("The listener must be a function");var i=new function(e,t,n){this.fn=e,this.context=t,this.once=n||!1}(n,r||e,o),s=c?c+t:t;return e._events[s]?e._events[s].fn?e._events[s]=[e._events[s],i]:e._events[s].push(i):(e._events[s]=i,e._eventsCount++),e}function i(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function s(){this._events=new r,this._eventsCount=0}var f=Object.prototype.hasOwnProperty,c="~";Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(c=!1)),s.prototype.eventNames=function(){var e,t,n=[];if(0===this._eventsCount)return n;for(t in e=this._events)f.call(e,t)&&n.push(c?t.slice(1):t);return Object.getOwnPropertySymbols?n.concat(Object.getOwnPropertySymbols(e)):n},s.prototype.listeners=function(e){var t=c?c+e:e,n=this._events[t];if(!n)return[];if(n.fn)return[n.fn];for(var r=0,o=n.length,i=new Array(o);r<o;r++)i[r]=n[r].fn;return i},s.prototype.listenerCount=function(e){var t=c?c+e:e,n=this._events[t];return n?n.fn?1:n.length:0},s.prototype.emit=function(e,t,n,r,o,i){var s=c?c+e:e;if(!this._events[s])return!1;var f,u,a=this._events[s],l=arguments.length;if(a.fn){switch(a.once&&this.removeListener(e,a.fn,void 0,!0),l){case 1:return a.fn.call(a.context),!0;case 2:return a.fn.call(a.context,t),!0;case 3:return a.fn.call(a.context,t,n),!0;case 4:return a.fn.call(a.context,t,n,r),!0;case 5:return a.fn.call(a.context,t,n,r,o),!0;case 6:return a.fn.call(a.context,t,n,r,o,i),!0}for(u=1,f=new Array(l-1);u<l;u++)f[u-1]=arguments[u];a.fn.apply(a.context,f)}else{var p,v=a.length;for(u=0;u<v;u++)switch(a[u].once&&this.removeListener(e,a[u].fn,void 0,!0),l){case 1:a[u].fn.call(a[u].context);break;case 2:a[u].fn.call(a[u].context,t);break;case 3:a[u].fn.call(a[u].context,t,n);break;case 4:a[u].fn.call(a[u].context,t,n,r);break;default:if(!f)for(p=1,f=new Array(l-1);p<l;p++)f[p-1]=arguments[p];a[u].fn.apply(a[u].context,f)}}return!0},s.prototype.on=function(e,t,n){return o(this,e,t,n,!1)},s.prototype.once=function(e,t,n){return o(this,e,t,n,!0)},s.prototype.removeListener=function(e,t,n,r){var o=c?c+e:e;if(!this._events[o])return this;if(!t)return i(this,o),this;var s=this._events[o];if(s.fn)s.fn!==t||r&&!s.once||n&&s.context!==n||i(this,o);else{for(var f=0,u=[],a=s.length;f<a;f++)(s[f].fn!==t||r&&!s[f].once||n&&s[f].context!==n)&&u.push(s[f]);u.length?this._events[o]=1===u.length?u[0]:u:i(this,o)}return this},s.prototype.removeAllListeners=function(e){var t;return e?(t=c?c+e:e,this._events[t]&&i(this,t)):(this._events=new r,this._eventsCount=0),this},s.prototype.off=s.prototype.removeListener,s.prototype.addListener=s.prototype.on,s.prefixed=c,s.EventEmitter=s,void 0!==t&&(t.exports=s)},{}]},{},[1])(1)});
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