diff --git a/.tags b/.tags deleted file mode 100644 index d3d5600..0000000 --- a/.tags +++ /dev/null @@ -1,47 +0,0 @@ -!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ -!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ -!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ -!_TAG_PROGRAM_NAME Exuberant Ctags // -!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ -!_TAG_PROGRAM_VERSION 5.9~svn20110310 // -_log_ examples/browser/basic.js /^function _log_ (){$/;" c -_log_ examples/browser/multipleScopes.js /^function _log_ (){$/;" c -_log_ examples/browser/objectScope.js /^function _log_ (){$/;" c -author package.json /^ "author": "Brandon Nozaki Miller",$/;" f -authors bower.json /^ "authors": [$/;" f -bugs package.json /^ "bugs": {$/;" f -checkScope event-pubsub.js /^function checkScope(){$/;" f -description bower.json /^ "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!",$/;" f -description package.json /^ "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!",$/;" f -directories package.json /^ "directories": {$/;" f -eventLog examples/browser/basic.js /^var eventLog=document.getElementById('events');$/;" v -eventLog examples/browser/multipleScopes.js /^var eventLog=document.getElementById('events');$/;" v -eventLog examples/browser/objectScope.js /^var eventLog=document.getElementById('events');$/;" v -example package.json /^ "example": "examples"$/;" f -homepage bower.json /^ "homepage": "https:\/\/github.com\/RIAEvangelist\/event-pubsub",$/;" f -homepage package.json /^ "homepage": "https:\/\/github.com\/RIAEvangelist\/event-pubsub"$/;" f -ignore bower.json /^ "ignore": [$/;" f -init event-pubsub.js /^function init(scope){$/;" f -keywords bower.json /^ "keywords": [$/;" f -keywords package.json /^ "keywords": [$/;" f -license bower.json /^ "license": "DBAD",$/;" f -license package.json /^ "license": "Unlicense",$/;" f -main bower.json /^ "main": "event-pubsub.js",$/;" f -main package.json /^ "main": "event-pubsub.js",$/;" f -moduleType bower.json /^ "moduleType": [$/;" f -name bower.json /^ "name": "event-pubsub",$/;" f -name package.json /^ "name": "event-pubsub",$/;" f -pub event-pubsub.js /^function pub(type){$/;" f -pubsub examples/node/multipleScopes.js /^var pubsub = require('..\/..\/event-pubsub.js');$/;" v -pubsub examples/node/objectScope.js /^var pubsub = require('..\/..\/event-pubsub.js');$/;" v -repository package.json /^ "repository": {$/;" f -scripts package.json /^ "scripts": {$/;" f -sub event-pubsub.js /^function sub(type,handler){$/;" f -test package.json /^ "test": "echo \\"Error: no test specified\\" && exit 1"$/;" f -thing.id examples/browser/objectScope.js /^var thing={$/;" p -thing.id examples/node/objectScope.js /^var thing={$/;" p -type package.json /^ "type": "git",$/;" f -unsub event-pubsub.js /^function unsub(type,handler){$/;" f -url package.json /^ "url": "https:\/\/github.com\/RIAEvangelist\/event-pubsub.git"$/;" f -url package.json /^ "url": "https:\/\/github.com\/RIAEvangelist\/event-pubsub\/issues"$/;" f -version package.json /^ "version": "2.0.0",$/;" f diff --git a/README.md b/README.md index e68d221..743551b 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ -Event PubSub -============ +# Event PubSub + npm info : ![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) GitHub info : ![event-pubsub GitHub Release](https://img.shields.io/github/release/RIAEvangelist/event-pubsub.svg) ![GitHub license event-pubsub license](https://img.shields.io/github/license/RIAEvangelist/event-pubsub.svg) ![open issues for event-pubsub on GitHub](https://img.shields.io/github/issues/RIAEvangelist/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/) @@ -15,69 +16,130 @@ Easy for any developer level. No frills, just high speed pubsub events! **EXAMPLE FILES** -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 + + + + +``` + +# 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 + + PubSub Example + @@ -85,40 +147,145 @@ Easy for any developer level. No frills, just high speed pubsub events! + +``` + + ##### 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' +); + + +``` diff --git a/bower.json b/bower.json index 357f57b..a207b0a 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "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", "authors": [ "Brandon Nozaki Miller" diff --git a/es5.js b/es5.js new file mode 100644 index 0000000..782ed84 --- /dev/null +++ b/es5.js @@ -0,0 +1,117 @@ +'use strict'; + +function EventPubSub() { + this._events_={}; + this.publish=this.trigger=this.emit=emit; + this.subscribe=this.on=on; + this.unSubscribe=this.off=off; + + function on(type,handler){ + if(!handler){ + const err=new ReferenceError('handler not defined.'); + throw(err); + } + + if(!this._events_[type]){ + this._events_[type]=[]; + } + + this._events_[type].push(handler); + return this; + } + + function off(type,handler){ + if(!this._events_[type]){ + return 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 this; + } + + const handlers=this._events_[type]; + + while(handlers.includes(handler)){ + handlers.splice( + handlers.indexOf(handler), + 1 + ); + } + + if(handlers.length<1){ + delete this._events_[type]; + } + + return this; + } + + function emit(type){ + if(!this._events_[type]){ + return; + } + + arguments.splice=Array.prototype.splice; + arguments.splice(0,1); + + const handlers=this._events_[type]; + + for(let handler of handlers){ + handler.apply(this, arguments); + } + + if(!this._events_['*']){ + return this; + } + + const catchAll=this._events_['*']; + + for(let handler of catchAll){ + handler.apply(this, arguments); + } + + return this; + } + + 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; + }; +} + +module.exports=EventPubSub; diff --git a/event-pubsub-browser-es5.js b/event-pubsub-browser-es5.js new file mode 100644 index 0000000..e348169 --- /dev/null +++ b/event-pubsub-browser-es5.js @@ -0,0 +1,115 @@ +'use strict'; + +window.EventPubSub=function EventPubSub() { + this._events_={}; + this.publish=this.trigger=this.emit=emit; + this.subscribe=this.on=on; + this.unSubscribe=this.off=off; + + function on(type,handler){ + if(!handler){ + const err=new ReferenceError('handler not defined.'); + throw(err); + } + + if(!this._events_[type]){ + this._events_[type]=[]; + } + + this._events_[type].push(handler); + return this; + } + + function off(type,handler){ + if(!this._events_[type]){ + return 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 this; + } + + const handlers=this._events_[type]; + + while(handlers.includes(handler)){ + handlers.splice( + handlers.indexOf(handler), + 1 + ); + } + + if(handlers.length<1){ + delete this._events_[type]; + } + + return this; + } + + function emit(type){ + if(!this._events_[type]){ + return; + } + + arguments.splice=Array.prototype.splice; + arguments.splice(0,1); + + const handlers=this._events_[type]; + + for(let handler of handlers){ + handler.apply(this, arguments); + } + + if(!this._events_['*']){ + return this; + } + + const catchAll=this._events_['*']; + + for(let handler of catchAll){ + handler.apply(this, arguments); + } + + return this; + } + + 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; + }; +} diff --git a/event-pubsub-browser.js b/event-pubsub-browser.js index b92d44d..e43b43b 100644 --- a/event-pubsub-browser.js +++ b/event-pubsub-browser.js @@ -1,122 +1,112 @@ -window.pubsub=( - function(){ - - function sub(type,handler){ - if(!handler){ - var err=new ReferenceError('handler not defined'); - throw(err); - } - - checkScope.apply(this); - - 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; - } - checkScope.apply(this); - - if(handler=='*'){ - delete this._events_[type]; - return; - } - - if(!this._events_[type]) - return; - - for(var i=0, - count=this._events_[type].length; - i= 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; + }; +} diff --git a/event-pubsub.js b/event-pubsub.js index 8debbf3..e52a1af 100644 --- a/event-pubsub.js +++ b/event-pubsub.js @@ -1,116 +1,81 @@ -function sub(type,handler){ - if(!handler){ - var err=new ReferenceError('handler not defined'); - throw(err); - } +'use strict'; - checkScope.apply(this); - - 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; - } - checkScope.apply(this); - - if(handler=='*'){ - delete this._events_[type]; - return; - } - - if(!this._events_[type]) - return; - - for(var i=0, - count=this._events_[type].length; - i + + + Basic PubSub Example + + + + + Check the console + + diff --git a/examples/browser/ES5-extending.js b/examples/browser/ES5-extending.js new file mode 100644 index 0000000..0827577 --- /dev/null +++ b/examples/browser/ES5-extending.js @@ -0,0 +1,79 @@ +function Book(){ + //extend happens below + Object.assign(this,new window.EventPubSub); + //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 erase(count){ + const words=this.words.splice( + -count + ); + this.emit( + 'erased', + words + ) + } + + 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.on( + 'erased', + function(...words){ + console.log('words erased : ',words); + this.read(); + } +); + +book.on( + 'reading', + function(...words){ + console.log('reading book...'); + } +); + +book.add( + 'once','upon','a','time','in','a','cubicle' +); + +book.erase(1); + +book.add( + 'land','far','far','away' +); + +console.log('book final copy reads :'); +book.read(); diff --git a/examples/browser/basic.html b/examples/browser/basic.html index 7972cf9..759d578 100644 --- a/examples/browser/basic.html +++ b/examples/browser/basic.html @@ -3,13 +3,12 @@ Basic PubSub Example + + - - + Check your console - \ No newline at end of file + diff --git a/examples/browser/basic.js b/examples/browser/basic.js index 3d90be7..7bd9065 100644 --- a/examples/browser/basic.js +++ b/examples/browser/basic.js @@ -1,75 +1,34 @@ -var events = new window.pubsub(); - -/************************************\ - * - * The events var was instantiated - * as it's own scope - * - * **********************************/ +var events = new window.EventPubSub; 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', { type:'myObject', data:{ - x:'YAY, Objects!' + x:'YAY, Objects!' } } ) } ); -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' -); \ No newline at end of file + events.emit( + 'hello', + 'world' + ); + + events.emit( + 'hello', + 'again','and again' + ); diff --git a/examples/browser/extending.html b/examples/browser/extending.html new file mode 100644 index 0000000..8e285b3 --- /dev/null +++ b/examples/browser/extending.html @@ -0,0 +1,11 @@ + + + + Basic PubSub Example + + + + + Check the console + + diff --git a/examples/browser/extending.js b/examples/browser/extending.js new file mode 100644 index 0000000..fdd0b4f --- /dev/null +++ b/examples/browser/extending.js @@ -0,0 +1,71 @@ +class Book extends window.EventPubSub{ + constructor(){ + super(); + //now Book has .on, .off, and .emit + + this.words=[]; + } + + add(...words){ + this.words.push(...words); + this.emit( + 'added', + ...words + ); + } + + erase(count){ + const words=this.words.splice( + -count + ); + this.emit( + 'erased', + ...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.on( + 'erased', + function(...words){ + console.log('words erased : ',words); + this.read(); + } +); + +book.on( + 'reading', + function(...words){ + console.log('reading book...'); + } +); + +book.add( + 'once','upon','a','time','in','a','cubicle' +); + +book.erase(1); + +book.add( + 'land','far','far','away' +); + +console.log('book final copy reads :'); +book.read(); diff --git a/examples/browser/multiple.html b/examples/browser/multiple.html new file mode 100644 index 0000000..9609527 --- /dev/null +++ b/examples/browser/multiple.html @@ -0,0 +1,14 @@ + + + + Basic PubSub Example + + + + + + Check your console + + diff --git a/examples/browser/multiple.js b/examples/browser/multiple.js new file mode 100644 index 0000000..c3594bb --- /dev/null +++ b/examples/browser/multiple.js @@ -0,0 +1,75 @@ +const myEvents=new window.EventPubSub; +const myEvents2=new window.EventPubSub; + +myEvents.on( + 'hello', + function(data){ + console.log('myEvents hello event recieved ', data); + } +); + +myEvents.on( + 'hello', + function(data){ + console.log('Second handler listening to myEvents hello event got',data); + myEvents.emit( + 'world', + { + type:'myObject', + data:{ + x:'YAY, Objects!' + } + } + ) + } +); + +myEvents.on( + 'world', + function(data){ + console.log('myEvents World event got',data); + } +); + +/**********************************\ + * + * Demonstrate * event (on all events) + * remove this for less verbose + * example + * + * ********************************/ +myEvents.on( + '*', + function(type){ + console.log('myEvents Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments); + } +); + +/************************************\ + * binding myEvents2 events + * **********************************/ +myEvents2.on( + 'hello', + function(data){ + console.log('myEvents2 Hello event should never be called ', data); + } +); + +myEvents2.on( + 'world', + function(data){ + console.log('myEvents2 World event ',data); + } +); + + +//emiting +myEvents.emit( + 'hello', + 'world' +); + +myEvents2.emit( + 'world', + 'is','round' +); diff --git a/examples/browser/multipleScopes.html b/examples/browser/multipleScopes.html deleted file mode 100644 index e6eb395..0000000 --- a/examples/browser/multipleScopes.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - Basic PubSub Example - - - - - - - \ No newline at end of file diff --git a/examples/browser/multipleScopes.js b/examples/browser/multipleScopes.js deleted file mode 100644 index 11170af..0000000 --- a/examples/browser/multipleScopes.js +++ /dev/null @@ -1,103 +0,0 @@ -/************************************\ - * instantiating myEvents scope - * **********************************/ -var myEvents=new window.pubsub(); - -/************************************\ - * instantiating myEvents2 scope - * **********************************/ -var myEvents2=new window.pubsub(); - - -/************************************\ - * binding myEvents events - * **********************************/ -myEvents.on( - 'hello', - function(data){ - eventLog.log('myEvents hello event recieved ', data); - } -); - -myEvents.on( - 'hello', - function(data){ - eventLog.log('Second handler listening to myEvents hello event got',data); - myEvents.trigger( - 'world', - { - type:'myObject', - data:{ - x:'YAY, Objects!' - } - } - ) - } -); - -myEvents.on( - 'world', - function(data){ - eventLog.log('myEvents World event got',data); - } -); - -/**********************************\ - * - * Demonstrate * event (on all events) - * remove this for less verbose - * example - * - * ********************************/ -myEvents.on( - '*', - function(type){ - eventLog.log('myEvents Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments); - } -); - -/************************************\ - * binding myEvents2 events - * **********************************/ -myEvents2.on( - 'hello', - function(data){ - eventLog.log('myEvents2 Hello event should never be called ', data); - } -); - -myEvents2.on( - 'world', - function(data){ - eventLog.log('myEvents2 World event ',data); - } -); - -/*******************************\ - * - * 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); -} - -/************************************\ - * trigger events for testing - * **********************************/ -myEvents.trigger( - 'hello', - 'world' -); - -myEvents2.trigger( - 'world', - 'is round' -); \ No newline at end of file diff --git a/examples/browser/objectScope.html b/examples/browser/objectScope.html deleted file mode 100644 index e01814b..0000000 --- a/examples/browser/objectScope.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - Basic PubSub Example - - - - - - - \ No newline at end of file diff --git a/examples/browser/objectScope.js b/examples/browser/objectScope.js deleted file mode 100644 index e9e131a..0000000 --- a/examples/browser/objectScope.js +++ /dev/null @@ -1,73 +0,0 @@ -/************************************\ - * - * The events var was instantiated - * as it's own scope - * - * **********************************/ - -var thing={ - id:'my thing' -} -/******************************\ - * - * Create events in the scope - * of the "thing" object - * - * ****************************/ -new window.pubsub(thing); - -thing.on( - 'getID', - function(){ - eventLog.log('things id is : ',this.id); - } -); - -thing.on( - 'setID', - function(id){ - eventLog.log('setting id to : ',id); - this.id=id; - this.trigger('getID'); - } -); - -/**********************************\ - * - * Demonstrate * event (on all events) - * remove this for less verbose - * example - * - * ********************************/ -thing.on( - '*', - function(type){ - eventLog.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); -} - -/************************************\ - * trigger events for testing - * **********************************/ -thing.trigger('getID'); - -thing.trigger( - 'setID', - 'your thing' -) \ No newline at end of file diff --git a/examples/node/basic-es5.js b/examples/node/basic-es5.js new file mode 100644 index 0000000..167af85 --- /dev/null +++ b/examples/node/basic-es5.js @@ -0,0 +1,35 @@ +const Events = require('../../es5.js'); +const events = new Events; + +events.on( + 'hello', + function(data){ + console.log('hello event recieved ', data); + events.emit( + 'world', + { + type:'myObject', + data:{ + x:'YAY, Objects!' + } + } + ) + } +); + +events.on( + '*', + function(type){ + console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments); + } +); + + events.emit( + 'hello', + 'world' + ); + + events.emit( + 'hello', + 'again','and again' + ); diff --git a/examples/node/basic.js b/examples/node/basic.js index 96b5387..98fa05e 100644 --- a/examples/node/basic.js +++ b/examples/node/basic.js @@ -1,11 +1,6 @@ -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; events.on( 'hello', @@ -18,7 +13,7 @@ events.on( 'hello', function(data){ console.log('Second handler listening to hello event got',data); - events.trigger( + events.emit( 'world', { type:'myObject', @@ -54,9 +49,9 @@ events.on( ); /************************************\ - * trigger events for testing + * emit events for testing * **********************************/ -events.trigger( +events.emit( 'hello', 'world' ); diff --git a/examples/node/extending-es5.js b/examples/node/extending-es5.js new file mode 100644 index 0000000..032ed46 --- /dev/null +++ b/examples/node/extending-es5.js @@ -0,0 +1,82 @@ +const Events = require('../../es5.js'); + +function Book(){ + //extend happens below + Object.assign(this,new Events); + //now Book has .on, .off, and .trigger + + 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.trigger( + 'added', + arguments.slice() + ); + } + + function erase(count){ + const words=this.words.splice( + -count + ); + this.trigger( + 'erased', + words + ) + } + + function read(){ + this.trigger( + '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.on( + 'erased', + function(...words){ + console.log('words erased : ',words); + this.read(); + } +); + +book.on( + 'reading', + function(...words){ + console.log('reading book...'); + } +); + +book.add( + 'once','upon','a','time','in','a','cubicle' +); + +book.erase(1); + +book.add( + 'land','far','far','away' +); + +console.log('book final copy reads :'); +book.read(); diff --git a/examples/node/extending.js b/examples/node/extending.js new file mode 100644 index 0000000..3b8a436 --- /dev/null +++ b/examples/node/extending.js @@ -0,0 +1,73 @@ +const Events = require('../../event-pubsub.js'); + +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 + ); + } + + erase(count){ + const words=this.words.splice( + -count + ); + this.emit( + 'erased', + ...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.on( + 'erased', + function(...words){ + console.log('words erased : ',words); + this.read(); + } +); + +book.on( + 'reading', + function(...words){ + console.log('reading book...'); + } +); + +book.add( + 'once','upon','a','time','in','a','cubicle' +); + +book.erase(1); + +book.add( + 'land','far','far','away' +); + +console.log('book final copy reads :'); +book.read(); diff --git a/examples/node/multipleScopes.js b/examples/node/multiple.js similarity index 86% rename from examples/node/multipleScopes.js rename to examples/node/multiple.js index a91e100..ca123c4 100644 --- a/examples/node/multipleScopes.js +++ b/examples/node/multiple.js @@ -1,14 +1,14 @@ -var pubsub = require('../../event-pubsub.js'); +const Events = require('../../event-pubsub.js'); /************************************\ * instantiating myEvents scope * **********************************/ -var myEvents=new pubsub(); +const myEvents=new EventPubSub(); /************************************\ * instantiating myEvents2 scope * **********************************/ -var myEvents2=new pubsub(); +const myEvents2=new EventPubSub(); /************************************\ @@ -25,12 +25,12 @@ myEvents.on( 'hello', function(data){ console.log('Second handler listening to myEvents hello event got',data); - myEvents.trigger( + myEvents.emit( 'world', { type:'myObject', data:{ - x:'YAY, Objects!' + x:'YAY, Objects!' } } ) @@ -45,11 +45,11 @@ myEvents.on( ); /**********************************\ - * + * * Demonstrate * event (on all events) * remove this for less verbose * example - * + * * ********************************/ myEvents.on( '*', @@ -77,14 +77,14 @@ myEvents2.on( /************************************\ - * trigger events for testing + * emit events for testing * **********************************/ -myEvents.trigger( +myEvents.emit( 'hello', 'world' ); -myEvents2.trigger( +myEvents2.emit( 'world', 'is round' -); \ No newline at end of file +); diff --git a/examples/node/objectScope.js b/examples/node/objectScope.js deleted file mode 100644 index 7df02e3..0000000 --- a/examples/node/objectScope.js +++ /dev/null @@ -1,56 +0,0 @@ -var pubsub = require('../../event-pubsub.js'); - -/************************************\ - * - * The events var was instantiated - * as it's own scope - * - * **********************************/ - -var thing={ - id:'my thing' -} -/******************************\ - * - * Create events in the scope - * of the "thing" object - * - * ****************************/ -new pubsub(thing); - -thing.on( - 'getID', - function(){ - console.log('things id is : ',this.id); - } -); - -thing.on( - 'setID', - function(id){ - console.log('setting id to : ',id); - this.id=id; - this.trigger('getID'); - } -); - -/**********************************\ - * - * Demonstrate * event (on all events) - * remove this for less verbose - * example - * - * ********************************/ -thing.on( - '*', - function(type){ - console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments); - } -); - -thing.trigger('getID'); - -thing.trigger( - 'setID', - 'your thing' -) \ No newline at end of file diff --git a/package.json b/package.json index 2935617..51e0759 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "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", "directories": { "example": "examples"