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

eventbusjs

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventbusjs - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

2

lib/eventbus.min.js

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

(function(root,factory){if(typeof exports==="object"&&typeof module==="object")module.exports=factory();else if(typeof define==="function"&&define.amd)define("EventBus",[],factory);else if(typeof exports==="object")exports["EventBus"]=factory();else root["EventBus"]=factory()})(this,function(){var EventBusClass={};EventBusClass=function(){this.listeners={}};EventBusClass.prototype={addEventListener:function(type,callback,scope){var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>3?args.splice(3,args.length-1):[];if(typeof this.listeners[type]!="undefined"){this.listeners[type].push({scope:scope,callback:callback,args:args})}else{this.listeners[type]=[{scope:scope,callback:callback,args:args}]}},removeEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;var newArray=[];for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if(listener.scope==scope&&listener.callback==callback){}else{newArray.push(listener)}}this.listeners[type]=newArray}},hasEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;if(callback===undefined&&scope===undefined){return numOfCallbacks>0}for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if((scope?listener.scope==scope:true)&&listener.callback==callback){return true}}}return false},dispatch:function(type,target){var numOfListeners=0;var event={type:type,target:target};var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>2?args.splice(2,args.length-1):[];args=[event].concat(args);if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if(listener&&listener.callback){var concatArgs=args.concat(listener.args);listener.callback.apply(listener.scope,concatArgs);numOfListeners+=1}}}},getEvents:function(){var str="";for(var type in this.listeners){var numOfCallbacks=this.listeners[type].length;for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];str+=listener.scope&&listener.scope.className?listener.scope.className:"anonymous";str+=" listen for '"+type+"'\n"}}return str}};var EventBus=new EventBusClass;return EventBus});
(function(root,factory){if(typeof exports==="object"&&typeof module==="object")module.exports=factory();else if(typeof define==="function"&&define.amd)define("EventBus",[],factory);else if(typeof exports==="object")exports["EventBus"]=factory();else root["EventBus"]=factory()})(this,function(){var EventBusClass={};EventBusClass=function(){this.listeners={}};EventBusClass.prototype={addEventListener:function(type,callback,scope){var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>3?args.splice(3,args.length-1):[];if(typeof this.listeners[type]!="undefined"){this.listeners[type].push({scope:scope,callback:callback,args:args})}else{this.listeners[type]=[{scope:scope,callback:callback,args:args}]}},removeEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;var newArray=[];for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if(listener.scope==scope&&listener.callback==callback){}else{newArray.push(listener)}}this.listeners[type]=newArray}},hasEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;if(callback===undefined&&scope===undefined){return numOfCallbacks>0}for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if((scope?listener.scope==scope:true)&&listener.callback==callback){return true}}}return false},dispatch:function(type,target){var event={type:type,target:target};var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>2?args.splice(2,args.length-1):[];args=[event].concat(args);if(typeof this.listeners[type]!="undefined"){var listeners=this.listeners[type].slice();var numOfCallbacks=listeners.length;for(var i=0;i<numOfCallbacks;i++){var listener=listeners[i];if(listener&&listener.callback){var concatArgs=args.concat(listener.args);listener.callback.apply(listener.scope,concatArgs)}}}},getEvents:function(){var str="";for(var type in this.listeners){var numOfCallbacks=this.listeners[type].length;for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];str+=listener.scope&&listener.scope.className?listener.scope.className:"anonymous";str+=" listen for '"+type+"'\n"}}return str}};var EventBus=new EventBusClass;return EventBus});
{
"name": "eventbusjs",
"version": "0.1.0",
"version": "0.2.0",
"description": "Managing events with JavaScript",

@@ -5,0 +5,0 @@ "main": "lib/eventbus.min.js",

@@ -66,3 +66,3 @@ # Simple JavaScript class for managing events in JavaScript

```
## Usage

@@ -125,1 +125,21 @@

## Example of usage EventBus.removeEventListener
To remove EventListener you have to pass same instance of callback
```js
/* Wrong - callback functions are different instances */
EventBus.addEventListener('EXAMPLE_EVENT', function() {
console.log('example callback');
});
EventBus.removeEventListener('EXAMPLE_EVENT', function() {
console.log('example callback');
});
/* Correct - callback function is the same instance */
var handler = function() {
console.log('example callback');
};
EventBus.addEventListener('EXAMPLE_EVENT', handler);
EventBus.removeEventListener('EXAMPLE_EVENT', handler);
```

@@ -17,3 +17,3 @@ (function (root, factory) {

EventBusClass.prototype = {
addEventListener:function(type, callback, scope) {
addEventListener: function(type, callback, scope) {
var args = [];

@@ -23,3 +23,3 @@ var numOfArgs = arguments.length;

args.push(arguments[i]);
}
}
args = args.length > 3 ? args.splice(3, args.length-1) : [];

@@ -32,3 +32,3 @@ if(typeof this.listeners[type] != "undefined") {

},
removeEventListener:function(type, callback, scope) {
removeEventListener: function(type, callback, scope) {
if(typeof this.listeners[type] != "undefined") {

@@ -40,3 +40,3 @@ var numOfCallbacks = this.listeners[type].length;

if(listener.scope == scope && listener.callback == callback) {
} else {

@@ -49,3 +49,3 @@ newArray.push(listener);

},
hasEventListener:function(type, callback, scope) {
hasEventListener: function(type, callback, scope) {
if(typeof this.listeners[type] != "undefined") {

@@ -65,7 +65,6 @@ var numOfCallbacks = this.listeners[type].length;

},
dispatch:function(type, target) {
var numOfListeners = 0;
dispatch: function(type, target) {
var event = {
type:type,
target:target
type: type,
target: target
};

@@ -76,13 +75,15 @@ var args = [];

args.push(arguments[i]);
};
};
args = args.length > 2 ? args.splice(2, args.length-1) : [];
args = [event].concat(args);
if(typeof this.listeners[type] != "undefined") {
var numOfCallbacks = this.listeners[type].length;
var listeners = this.listeners[type].slice();
var numOfCallbacks = listeners.length;
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
if(listener && listener.callback) {
var listener = listeners[i];
if(listener && listener.callback) {
var concatArgs = args.concat(listener.args);
listener.callback.apply(listener.scope, concatArgs);
numOfListeners += 1;
}

@@ -92,3 +93,3 @@ }

},
getEvents:function() {
getEvents: function() {
var str = "";

@@ -108,2 +109,2 @@ for(var type in this.listeners) {

return EventBus;
});
});

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