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

event-pubsub

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-pubsub - npm Package Compare versions

Comparing version 2.2.0 to 3.0.0

es5.js

2

bower.json
{
"name": "event-pubsub",
"description": "Pubsub events for Node and the browser allowing event scoping and multiple scopes. Easy for any developer level. No frills, just high speed pubsub events!",
"description": "Super light and fast Extensible PubSub events and EventEmitters for Node and the browser with support for ES6 by default, and ES5 versions for older verions of node and older IE/Safari versions. Easy for any developer level. No frills, just high speed pubsub events!",
"main": "event-pubsub.js",

@@ -5,0 +5,0 @@ "authors": [

@@ -1,122 +0,112 @@

window.pubsub=(
function(){
'use strict';
function sub(type,handler){
if(!handler){
var err=new ReferenceError('handler not defined');
throw(err);
}
window.EventPubSub=class EventPubSub {
constructor(scope){
this._events_={};
this.publish=this.trigger=this.emit;
this.subscribe=this.on;
this.unSubscribe=this.off;
}
checkScope.apply(this);
on(type,handler){
if(!handler){
const err=new ReferenceError('handler not defined.');
throw(err);
}
if(!this._events_[type])
this._events_[type]=[];
if(!this._events_[type]){
this._events_[type]=[];
}
this._events_[type].push(handler);
this._events_[type].push(handler);
return this;
}
off(type,handler){
if(!this._events_[type]){
return this;
}
function unsub(type,handler){
if(!handler){
var err=new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler');
throw err;
}
checkScope.apply(this);
if(!handler){
var err=new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler');
throw err;
}
if(handler=='*'){
delete this._events_[type];
return;
}
if(handler=='*'){
delete this._events_[type];
return this;
}
if(!this._events_[type])
return;
const handlers=this._events_[type];
for(var i=0,
count=this._events_[type].length;
i<count;
i++
){
if(this._events_[type][i]==handler){
this._events_[type].splice(i,1);
}
}
while(handlers.includes(handler)){
handlers.splice(
handlers.indexOf(handler),
1
);
}
if(this._events_[type].length<1){
delete this._events_[type];
}
if(handlers.length<1){
delete this._events_[type];
}
function pub(type){
checkScope.apply(this);
return this;
}
if(this._events_['*'] && type!='*'){
var params=Array.prototype.slice.call(arguments);
params.unshift('*');
this.trigger.apply(this,params);
}
emit(type,...args){
if(!this._events_[type]){
return;
}
if(!this._events_[type])
return;
const handlers=this._events_[type];
for(var i=0,
events=this._events_[type],
count=events.length,
args=Array.prototype.slice.call(arguments,1);
i<count;
i++){
events[i].apply(this, args);
}
for(let handler of handlers){
handler.apply(this, args);
}
function checkScope(){
if(!this._events_)
this._events_={};
if(!this._events_['*']){
return this;
}
function init(scope){
if(!scope)
return {
on:sub,
off:unsub,
trigger:pub
};
const catchAll=this._events_['*'];
scope.on=(
function(scope){
return function(){
sub.apply(
scope,
Array.prototype.slice.call(arguments)
);
}
}
)(scope);
scope.off=(
function(scope){
return function(){
unsub.apply(
scope,
Array.prototype.slice.call(arguments)
);
}
}
)(scope);
scope.trigger=(
function(scope){
return function(){
pub.apply(
scope,
Array.prototype.slice.call(arguments)
);
}
}
)(scope);
scope._events_={};
for(let handler of catchAll){
handler.apply(this, args);
}
return init;
return this;
}
}
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.includes called on null or undefined');
}
)();
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
}
return false;
};
}

@@ -1,116 +0,81 @@

function sub(type,handler){
if(!handler){
var err=new ReferenceError('handler not defined');
throw(err);
'use strict';
class EventPubSub {
constructor(scope){
this._events_={};
this.publish=this.trigger=this.emit;
this.subscribe=this.on;
this.unSubscribe=this.off;
}
checkScope.apply(this);
on(type,handler){
if(!handler){
const err=new ReferenceError('handler not defined.');
throw(err);
}
if(!this._events_[type])
this._events_[type]=[];
if(!this._events_[type]){
this._events_[type]=[];
}
this._events_[type].push(handler);
}
function unsub(type,handler){
if(!handler){
var err=new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler');
throw err;
this._events_[type].push(handler);
return this;
}
checkScope.apply(this);
if(handler=='*'){
delete this._events_[type];
return;
}
off(type,handler){
if(!this._events_[type]){
return this;
}
if(!this._events_[type])
return;
if(!handler){
var err=new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler');
throw err;
}
for(var i=0,
count=this._events_[type].length;
i<count;
i++
){
if(this._events_[type][i]==handler){
this._events_[type].splice(i,1);
if(handler=='*'){
delete this._events_[type];
return this;
}
}
if(this._events_[type].length<1){
delete this._events_[type];
}
}
const handlers=this._events_[type];
function pub(type){
checkScope.apply(this);
while(handlers.includes(handler)){
handlers.splice(
handlers.indexOf(handler),
1
);
}
if(this._events_['*'] && type!='*'){
var params=Array.prototype.slice.call(arguments);
params.unshift('*');
this.trigger.apply(this,params);
}
if(handlers.length<1){
delete this._events_[type];
}
if(!this._events_[type])
return;
for(var i=0,
events=this._events_[type],
count=events.length,
args=Array.prototype.slice.call(arguments,1);
i<count;
i++){
events[i].apply(this, args);
return this;
}
}
function checkScope(){
if(!this._events_)
this._events_={};
}
emit(type,...args){
if(!this._events_[type]){
return;
}
function init(scope){
if(!scope)
return {
on:sub,
off:unsub,
trigger:pub
};
const handlers=this._events_[type];
scope.on=(
function(scope){
return function(){
sub.apply(
scope,
Array.prototype.slice.call(arguments)
);
}
for(let handler of handlers){
handler.apply(this, args);
}
)(scope);
scope.off=(
function(scope){
return function(){
unsub.apply(
scope,
Array.prototype.slice.call(arguments)
);
}
if(!this._events_['*']){
return this;
}
)(scope);
scope.trigger=(
function(scope){
return function(){
pub.apply(
scope,
Array.prototype.slice.call(arguments)
);
}
const catchAll=this._events_['*'];
for(let handler of catchAll){
handler.apply(this, args);
}
)(scope);
scope._events_={};
return this;
}
}
module.exports=init
module.exports=EventPubSub;

@@ -1,22 +0,8 @@

var events = new window.pubsub();
var events = new window.EventPubSub;
/************************************\
*
* The events var was instantiated
* as it's own scope
*
* **********************************/
events.on(
'hello',
function(data){
eventLog.log('hello event recieved ', data);
}
);
events.on(
'hello',
function(data){
eventLog.log('Second handler listening to hello event got',data);
events.trigger(
console.log('hello event recieved ', data);
events.emit(
'world',

@@ -26,3 +12,3 @@ {

data:{
x:'YAY, Objects!'
x:'YAY, Objects!'
}

@@ -35,43 +21,16 @@ }

events.on(
'world',
function(data){
eventLog.log('World event got',data);
events.off('*');
eventLog.log('Removed all events')
}
);
/**********************************\
*
* Demonstrate * event (on all events)
* remove this for less verbose
* example
*
* ********************************/
events.on(
'*',
function(type){
eventLog.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
}
);
/*******************************\
*
* Prep HTML for logging
*
* *****************************/
var eventLog=document.getElementById('events');
//not using console.log incase it doesn't work in some browser. *TLDT (Too lazy didn't test)*
eventLog.log=_log_;
function _log_ (){
var events=Array.prototype.slice.call(arguments),
newEvent=document.createElement('li');
newEvent.innerHTML=events.join(' ');
this.appendChild(newEvent);
}
events.trigger(
'hello',
'world'
);
events.emit(
'hello',
'world'
);
events.emit(
'hello',
'again','and again'
);

@@ -1,9 +0,4 @@

var events = new require('../../event-pubsub.js')();
const Events = new require('../../event-pubsub.js');
/************************************\
*
* The events var was instantiated
* as it's own scope
*
* **********************************/
const events=new Events;

@@ -21,3 +16,3 @@ events.on(

console.log('Second handler listening to hello event got',data);
events.trigger(
events.emit(
'world',

@@ -58,7 +53,7 @@ {

/************************************\
* trigger events for testing
* emit events for testing
* **********************************/
events.trigger(
events.emit(
'hello',
'world'
);
{
"name": "event-pubsub",
"version": "2.2.0",
"description": "Pubsub events for Node and the browser allowing event scoping and multiple scopes. Easy for any developer level. No frills, just high speed pubsub events!",
"version": "3.0.0",
"description": "Super light and fast Extensible PubSub events and EventEmitters for Node and the browser with support for ES6 by default, and ES5 versions for older verions of node and older IE/Safari versions. Easy for any developer level. No frills, just high speed pubsub events!",
"main": "event-pubsub.js",

@@ -6,0 +6,0 @@ "directories": {

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

Event PubSub
============
# Event PubSub
npm info :

@@ -9,5 +9,6 @@ ![event-pubsub npm version](https://img.shields.io/npm/v/event-pubsub.svg) ![total npm downloads for event-pubsub](https://img.shields.io/npm/dt/event-pubsub.svg) ![monthly npm downloads for event-pubsub](https://img.shields.io/npm/dm/event-pubsub.svg)

Pubsub events for Node and the browser allowing event scoping and multiple scopes.
Easy for any developer level. No frills, just high speed pubsub events!
***Super light and fast*** Extensible PubSub events and EventEmitters for Node and the browser with support for ES6 by default, and ES5 versions for older verions of node and older IE/Safari versions.
Easy for any developer level. No frills, just high speed events following the publisher subscriber pattern!
[Pretty GitHub.io site](http://riaevangelist.github.io/event-pubsub/)

@@ -19,60 +20,118 @@

1. [Node Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/node)
2. [Browser Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/browser)
1. [Node Event PubSub Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/node)
2. [Browser Event PubSub Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/browser)
**Node Install**
``npm install event-pubsub``
`npm i --save event-pubsub`
By default the ES6 version will be used. you can use the es5 version for older versions of node by requiring `event-pubsub/es5.js`.
**Browser Install**
*see browser examples above or below*
*see browser examples above or below*
---
### Basic Example
---
***NOTE - the only diffeence between node and browser code is how the ``events`` variable is created***
* node ``var events = new require('../../event-pubsub.js')();``
* browser ``var events = new window.pubsub();``
```html
<script src='path/to/event-pubsub-browser.js'></script>
<!-- OR ES5 for older browser support
<script src='path/to/event-pubsub-browser-es5.js'></script>
-->
```
# Methods
|Method|Arguments|Description|
|------|---------|-----------|
|subscribe|type (string), handler(function)|will bind the `handler` function to the the `type` event. Just like `addEventListener` in the browser|
|on|same as above|same as above|
|unSubscribe|type (string), handler(function or *)|will ***un***bind the `handler` function from the the `type` event. If the `handler` is `*`, all handlers for the event type will be removed. Just like `removeEventListener` in the browser, but also can remove all event handlers for the type.|
|off|same as above|same as above|
|publish|type (string), ...data arguments|will call all `handler` functions bound to the event `type` and pass all `...data arguments` to those handlers|
|emit|same as above|same as above|
|trigger|same as above|same as above|
While `publish`, `subscribe`, and `unSubscribe` are the proper method names for the publisher/subscriber model, we have included `on`, `off`, and `emit` as well because these are the most common event method names, and shorter. We have also kept the `trigger` method as an alias for `publish` and `emit` for backwards compatability with earlier versions of `event-pubsub`.
# The ` * ` type
The ` * ` type is a special event type that will be triggered by ***any publish or emit*** the handlers for these should expect the first argument to be the type and all arguments after to be data arguments.
## Basic Examples
***NOTE - the only difference between node and browser code is how the `events` variable is created***
* node ` const events = new Events `
* browser `const events = new window.EventPubSub;`
#### Node
var events = new require('../../event-pubsub.js')();
```javascript
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
}
);
const Events = new require('event-pubsub');
const events=new Events;
events.on(
'*',
function(type){
console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
}
);
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
events.emit(
'world',
{
type:'myObject',
data:{
x:'YAY, Objects!'
}
}
)
}
);
events.on(
'removeEvents',
function(){
events.off('*','*');
console.log('Removed all events');
}
);
events.on(
'world',
function(data){
console.log('World event got',data);
events.off('*','*');
console.log('Removed all events');
}
);
/************************************\
* trigger events for testing
* **********************************/
events.trigger(
'hello',
'world'
);
events.trigger(
'removeEvents'
);
events.emit(
'hello',
'world'
);
```
#### Basic Chaining
```javascript
events.on(
'hello',
someFunction
).on(
'goodbye',
anotherFunction
).emit(
'hello',
'world'
);
events.emit(
'goodbye',
'complexity'
).off(
'hello',
'*'
);
```
#### Browser
##### HTML
```html
<!DOCTYPE html>

@@ -83,2 +142,5 @@ <html>

<script src='../../event-pubsub-browser.js'></script>
<!-- OR ES5 for older browser support
<script src='../../event-pubsub-browser-es5.js'></script>
-->
<script src='yourAmazingCode.js'></script>

@@ -91,38 +153,143 @@ </head>

```
##### Inside Your Amazing Code
var events = new window.pubsub();
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
}
);
```javascript
events.on(
'*',
function(type){
console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
}
);
var events = new window.EventPubSub();
events.on(
'removeEvents',
function(){
events.off('*','*');
console.log('Removed all events');
}
);
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
events.emit(
'world',
{
type:'myObject',
data:{
x:'YAY, Objects!'
}
}
)
}
);
/************************************\
* trigger events for testing
* **********************************/
events.trigger(
'hello',
'world'
);
events.emit(
'hello',
'world'
);
events.trigger(
'removeEvents'
);
events.emit(
'hello',
'again','and again'
);
```
### Basic Event Emitter and/or Extending Event PubSub
```javascript
const Events = require('event-pubsub');
class Book extends Events{
constructor(){
super();
//now Book has .on, .off, and .emit
this.words=[];
}
add(...words){
this.words.push(...words);
this.emit(
'added',
...words
);
}
read(){
this.emit(
'reading'
);
console.log(this.words.join(' '));
}
}
const book=new Book;
book.on(
'added',
function(...words){
console.log('words added : ',words);
this.read();
}
);
book.add(
'once','upon','a','time','in','a','cubicle'
);
```
##### ES5 extention example
```javascript
const Events = require('event-pubsub/es5.js');
function Book(){
//extend happens below
Object.assign(this,new Events);
//now Book has .on, .off, and .emit
this.words=[];
this.add=add;
this.erase=erase;
this.read=read;
function add(){
arguments.slice=Array.prototype.slice;
this.words=this.words.concat(
arguments.slice()
);
this.emit(
'added',
arguments.slice()
);
}
function read(){
this.emit(
'reading'
);
console.log(this.words.join(' '));
}
return this;
};
const book=new Book;
book.on(
'added',
function(...words){
console.log('words added : ',words);
this.read();
}
);
book.add(
'once','upon','a','time','in','a','cubicle'
);
```

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