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

component-emitter

Package Overview
Dependencies
Maintainers
33
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

component-emitter - npm Package Compare versions

Comparing version 1.3.1 to 2.0.0

index.d.ts

227

index.js

@@ -0,175 +1,106 @@

function Emitter(object) {
if (object) {
return mixin(object);
}
/**
* Expose `Emitter`.
*/
if (typeof module !== 'undefined') {
module.exports = Emitter;
this._callbacks = new Map();
}
/**
* Initialize a new `Emitter`.
*
* @api public
*/
function Emitter(obj) {
if (obj) return mixin(obj);
};
/**
* Mixin the emitter properties.
*
* @param {Object} obj
* @return {Object}
* @api private
*/
function mixin(obj) {
for (var key in Emitter.prototype) {
obj[key] = Emitter.prototype[key];
}
return obj;
function mixin(object) {
Object.assign(object, Emitter.prototype);
object._callbacks = new Map();
return object;
}
/**
* Listen on the given `event` with `fn`.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.on =
Emitter.prototype.addEventListener = function(event, fn){
this._callbacks = this._callbacks || {};
(this._callbacks['$' + event] = this._callbacks['$' + event] || [])
.push(fn);
return this;
Emitter.prototype.on = function (event, listener) {
const callbacks = this._callbacks.get(event) ?? [];
callbacks.push(listener);
this._callbacks.set(event, callbacks);
return this;
};
/**
* Adds an `event` listener that will be invoked a single
* time then automatically removed.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.once = function (event, listener) {
const on = (...arguments_) => {
this.off(event, on);
listener.apply(this, arguments_);
};
Emitter.prototype.once = function(event, fn){
function on() {
this.off(event, on);
fn.apply(this, arguments);
}
on.fn = fn;
this.on(event, on);
return this;
on.fn = listener;
this.on(event, on);
return this;
};
/**
* Remove the given callback for `event` or all
* registered callbacks.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.off = function (event, listener) {
if (event === undefined && listener === undefined) {
this._callbacks.clear();
return this;
}
Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener = function(event, fn){
this._callbacks = this._callbacks || {};
if (listener === undefined) {
this._callbacks.delete(event);
return this;
}
// all
if (0 == arguments.length) {
this._callbacks = {};
return this;
}
const callbacks = this._callbacks.get(event);
if (callbacks) {
for (const [index, callback] of callbacks.entries()) {
if (callback === listener || callback.fn === listener) {
callbacks.splice(index, 1);
break;
}
}
// specific event
var callbacks = this._callbacks['$' + event];
if (!callbacks) return this;
if (callbacks.length === 0) {
this._callbacks.delete(event);
} else {
this._callbacks.set(event, callbacks);
}
}
// remove all handlers
if (1 == arguments.length) {
delete this._callbacks['$' + event];
return this;
}
return this;
};
// remove specific handler
var cb;
for (var i = 0; i < callbacks.length; i++) {
cb = callbacks[i];
if (cb === fn || cb.fn === fn) {
callbacks.splice(i, 1);
break;
}
}
Emitter.prototype.emit = function (event, ...arguments_) {
const callbacks = this._callbacks.get(event);
if (callbacks) {
// Create a copy of the callbacks array to avoid issues if it's modified during iteration
const callbacksCopy = [...callbacks];
// Remove event specific arrays for event types that no
// one is subscribed for to avoid memory leak.
if (callbacks.length === 0) {
delete this._callbacks['$' + event];
}
for (const callback of callbacksCopy) {
callback.apply(this, arguments_);
}
}
return this;
return this;
};
/**
* Emit `event` with the given args.
*
* @param {String} event
* @param {Mixed} ...
* @return {Emitter}
*/
Emitter.prototype.listeners = function (event) {
return this._callbacks.get(event) ?? [];
};
Emitter.prototype.emit = function(event){
this._callbacks = this._callbacks || {};
Emitter.prototype.listenerCount = function (event) {
if (event) {
return this.listeners(event).length;
}
var args = new Array(arguments.length - 1)
, callbacks = this._callbacks['$' + event];
let totalCount = 0;
for (const callbacks of this._callbacks.values()) {
totalCount += callbacks.length;
}
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i) {
callbacks[i].apply(this, args);
}
}
return this;
return totalCount;
};
/**
* Return array of callbacks for `event`.
*
* @param {String} event
* @return {Array}
* @api public
*/
Emitter.prototype.listeners = function(event){
this._callbacks = this._callbacks || {};
return this._callbacks['$' + event] || [];
Emitter.prototype.hasListeners = function (event) {
return this.listenerCount(event) > 0;
};
/**
* Check if this emitter has `event` handlers.
*
* @param {String} event
* @return {Boolean}
* @api public
*/
// Aliases
Emitter.prototype.addEventListener = Emitter.prototype.on;
Emitter.prototype.removeListener = Emitter.prototype.off;
Emitter.prototype.removeEventListener = Emitter.prototype.off;
Emitter.prototype.removeAllListeners = Emitter.prototype.off;
Emitter.prototype.hasListeners = function(event){
return !! this.listeners(event).length;
};
if (typeof module !== 'undefined') {
module.exports = Emitter;
}
{
"name": "component-emitter",
"description": "Event emitter",
"version": "1.3.1",
"license": "MIT",
"funding": "https://github.com/sponsors/sindresorhus",
"devDependencies": {
"mocha": "*",
"should": "*"
},
"component": {
"scripts": {
"emitter/index.js": "index.js"
}
},
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/sindresorhus/component-emitter.git"
},
"scripts": {
"test": "make test"
},
"files": [
"index.js",
"LICENSE"
]
"name": "component-emitter",
"version": "2.0.0",
"description": "Simple event emitter",
"license": "MIT",
"repository": "sindresorhus/component-emitter",
"funding": "https://github.com/sponsors/sindresorhus",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"main": "./index.js",
"types": "./index.d.ts",
"sideEffects": false,
"engines": {
"node": ">=18"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"event",
"emitter",
"events",
"emit",
"listener",
"pubsub",
"observe"
],
"devDependencies": {
"ava": "^5.3.1",
"xo": "^0.56.0"
},
"xo": {
"rules": {
"unicorn/prefer-module": "off"
}
}
}
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