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 :
  
GitHub info :
  
-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
+
+
-
- -
-
PubSub Basic Events Example
-
-
-
+ Check your console
-