es6 update, examples and chaining

This commit is contained in:
SSMP 2016-09-30 03:58:44 -07:00
parent f955b96dd2
commit 5d9d6ceeec
26 changed files with 1151 additions and 702 deletions

47
.tags
View file

@ -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

279
README.md
View file

@ -1,13 +1,14 @@
Event PubSub # Event PubSub
============
npm info : 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) ![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 : 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) ![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. ***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!
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/) [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** **EXAMPLE FILES**
1. [Node Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/node) 1. [Node Event PubSub 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) 2. [Browser Event PubSub Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/browser)
**Node Install** **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** **Browser Install**
*see browser examples above or below* *see browser examples above or below*
--- ```html
### Basic Example
--- <script src='path/to/event-pubsub-browser.js'></script>
***NOTE - the only diffeence between node and browser code is how the ``events`` variable is created*** <!-- OR ES5 for older browser support
* node ``var events = new require('../../event-pubsub.js')();`` <script src='path/to/event-pubsub-browser-es5.js'></script>
* browser ``var events = new window.pubsub();`` -->
```
# 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 #### Node
var events = new require('../../event-pubsub.js')(); ```javascript
events.on( const Events = new require('event-pubsub');
const events=new Events;
events.on(
'hello', 'hello',
function(data){ function(data){
console.log('hello event recieved ', 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.on( events.on(
'removeEvents', 'world',
function(){ function(data){
console.log('World event got',data);
events.off('*','*'); events.off('*','*');
console.log('Removed all events'); console.log('Removed all events');
} }
); );
/************************************\ events.emit(
* trigger events for testing
* **********************************/
events.trigger(
'hello', 'hello',
'world' 'world'
); );
events.trigger(
'removeEvents'
);
```
#### Basic Chaining
```javascript
events.on(
'hello',
someFunction
).on(
'goodbye',
anotherFunction
).emit(
'hello',
'world'
);
events.emit(
'goodbye',
'complexity'
).off(
'hello',
'*'
);
```
#### Browser #### Browser
##### HTML ##### HTML
```html
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>PubSub Example</title> <title>PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script> <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> <script src='yourAmazingCode.js'></script>
</head> </head>
<body> <body>
@ -85,40 +147,145 @@ Easy for any developer level. No frills, just high speed pubsub events!
</body> </body>
</html> </html>
```
##### Inside Your Amazing Code ##### Inside Your Amazing Code
var events = new window.pubsub();
events.on( ```javascript
var events = new window.EventPubSub();
events.on(
'hello', 'hello',
function(data){ function(data){
console.log('hello event recieved ', 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.on(
'removeEvents',
function(){
events.off('*','*');
console.log('Removed all events');
} }
); );
/************************************\ events.emit(
* trigger events for testing
* **********************************/
events.trigger(
'hello', 'hello',
'world' 'world'
); );
events.trigger( events.emit(
'removeEvents' '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'
);
```

View file

@ -1,6 +1,6 @@
{ {
"name": "event-pubsub", "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", "main": "event-pubsub.js",
"authors": [ "authors": [
"Brandon Nozaki Miller" "Brandon Nozaki Miller"

117
es5.js Normal file
View file

@ -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;

115
event-pubsub-browser-es5.js Normal file
View file

@ -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;
};
}

View file

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

View file

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

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Basic PubSub Example</title>
<script src='../../event-pubsub-browser-es5.js'></script>
<script src='ES5-extending.js'></script>
</head>
<body>
Check the console
</body>
</html>

View file

@ -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();

View file

@ -3,13 +3,12 @@
<head> <head>
<title>Basic PubSub Example</title> <title>Basic PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script> <script src='../../event-pubsub-browser.js'></script>
<!-- OR for supporting older browsers
<script src='../../event-pubsub-browser-es5.js'></script>
-->
<script src='basic.js'></script>
</head> </head>
<body> <body>
<ul id='events'> Check your console
<li>
<h1>PubSub Basic Events Example</h1>
</li>
</ul>
<script src='basic.js'></script>
</body> </body>
</html> </html>

View file

@ -1,24 +1,10 @@
var events = new window.pubsub(); var events = new window.EventPubSub;
/************************************\
*
* The events var was instantiated
* as it's own scope
*
* **********************************/
events.on( events.on(
'hello', 'hello',
function(data){ function(data){
eventLog.log('hello event recieved ', data); console.log('hello event recieved ', data);
} events.emit(
);
events.on(
'hello',
function(data){
eventLog.log('Second handler listening to hello event got',data);
events.trigger(
'world', 'world',
{ {
type:'myObject', type:'myObject',
@ -30,46 +16,19 @@ events.on(
} }
); );
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( events.on(
'*', '*',
function(type){ 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);
} }
); );
/*******************************\ events.emit(
*
* 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', 'hello',
'world' 'world'
); );
events.emit(
'hello',
'again','and again'
);

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Basic PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script>
<script src='extending.js'></script>
</head>
<body>
Check the console
</body>
</html>

View file

@ -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();

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Basic PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script>
<!-- OR for supporting older browsers
<script src='../../event-pubsub-browser-es5.js'></script>
-->
<script src='multiple.js'></script>
</head>
<body>
Check your console
</body>
</html>

View file

@ -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'
);

View file

@ -1,15 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Basic PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script>
</head>
<body>
<ul id='events'>
<li>
<h1>PubSub Multiple Event Scopes Example</h1>
</li>
</ul>
<script src='multipleScopes.js'></script>
</body>
</html>

View file

@ -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'
);

View file

@ -1,15 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Basic PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script>
</head>
<body>
<ul id='events'>
<li>
<h1>PubSub Events inside of and using an Object as the scope Example</h1>
</li>
</ul>
<script src='objectScope.js'></script>
</body>
</html>

View file

@ -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'
)

View file

@ -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'
);

View file

@ -1,11 +1,6 @@
var events = new require('../../event-pubsub.js')(); const Events = new require('../../event-pubsub.js');
/************************************\ const events=new Events;
*
* The events var was instantiated
* as it's own scope
*
* **********************************/
events.on( events.on(
'hello', 'hello',
@ -18,7 +13,7 @@ events.on(
'hello', 'hello',
function(data){ function(data){
console.log('Second handler listening to hello event got',data); console.log('Second handler listening to hello event got',data);
events.trigger( events.emit(
'world', 'world',
{ {
type:'myObject', type:'myObject',
@ -54,9 +49,9 @@ events.on(
); );
/************************************\ /************************************\
* trigger events for testing * emit events for testing
* **********************************/ * **********************************/
events.trigger( events.emit(
'hello', 'hello',
'world' 'world'
); );

View file

@ -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();

View file

@ -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();

View file

@ -1,14 +1,14 @@
var pubsub = require('../../event-pubsub.js'); const Events = require('../../event-pubsub.js');
/************************************\ /************************************\
* instantiating myEvents scope * instantiating myEvents scope
* **********************************/ * **********************************/
var myEvents=new pubsub(); const myEvents=new EventPubSub();
/************************************\ /************************************\
* instantiating myEvents2 scope * instantiating myEvents2 scope
* **********************************/ * **********************************/
var myEvents2=new pubsub(); const myEvents2=new EventPubSub();
/************************************\ /************************************\
@ -25,7 +25,7 @@ myEvents.on(
'hello', 'hello',
function(data){ function(data){
console.log('Second handler listening to myEvents hello event got',data); console.log('Second handler listening to myEvents hello event got',data);
myEvents.trigger( myEvents.emit(
'world', 'world',
{ {
type:'myObject', type:'myObject',
@ -77,14 +77,14 @@ myEvents2.on(
/************************************\ /************************************\
* trigger events for testing * emit events for testing
* **********************************/ * **********************************/
myEvents.trigger( myEvents.emit(
'hello', 'hello',
'world' 'world'
); );
myEvents2.trigger( myEvents2.emit(
'world', 'world',
'is round' 'is round'
); );

View file

@ -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'
)

View file

@ -1,7 +1,7 @@
{ {
"name": "event-pubsub", "name": "event-pubsub",
"version": "2.2.0", "version": "3.0.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!", "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", "main": "event-pubsub.js",
"directories": { "directories": {
"example": "examples" "example": "examples"