mirror of
https://github.com/vale981/event-pubsub
synced 2025-03-05 09:31:42 -05:00
automatically include ES5/ES6 based on node version
This commit is contained in:
parent
ff1cd18420
commit
79186447a8
4 changed files with 90 additions and 91 deletions
19
README.md
19
README.md
|
@ -60,24 +60,11 @@ The ` * ` type is a special event type that will be triggered by ***any publish
|
||||||
* node ` const events = new Events `
|
* node ` const events = new Events `
|
||||||
* browser `const events = new window.EventPubSub;`
|
* browser `const events = new window.EventPubSub;`
|
||||||
|
|
||||||
### Automatically choosing ES6 || ES5
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
|
|
||||||
//use es6 by default
|
|
||||||
let Events = require('event-pubsub');
|
|
||||||
//if not using atleast node v5.0.0
|
|
||||||
if(process.version[1]<5){
|
|
||||||
//downgrade to ES5
|
|
||||||
Events = require('event-pubsub/es5');
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Node
|
#### Node
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
|
// ES5/ES6 now automatically chosen based on node version
|
||||||
const Events = new require('event-pubsub');
|
const Events = new require('event-pubsub');
|
||||||
const events=new Events;
|
const events=new Events;
|
||||||
|
|
||||||
|
@ -206,7 +193,10 @@ events.on(
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
|
// ES5/ES6 now automatically chosen based on node version
|
||||||
const Events = require('event-pubsub');
|
const Events = require('event-pubsub');
|
||||||
|
// manually include es6
|
||||||
|
// const Events = require('event-pubsub/es6');
|
||||||
|
|
||||||
class Book extends Events{
|
class Book extends Events{
|
||||||
constructor(){
|
constructor(){
|
||||||
|
@ -253,6 +243,7 @@ book.add(
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
|
// manually include es5
|
||||||
const Events = require('event-pubsub/es5.js');
|
const Events = require('event-pubsub/es5.js');
|
||||||
|
|
||||||
function Book(){
|
function Book(){
|
||||||
|
|
81
es6.js
Normal file
81
es6.js
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
'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){
|
||||||
|
const err=new ReferenceError('handler not defined.');
|
||||||
|
throw(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!this._events_[type]){
|
||||||
|
this._events_[type]=[];
|
||||||
|
}
|
||||||
|
|
||||||
|
this._events_[type].push(handler);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(type,...args){
|
||||||
|
if(!this._events_[type]){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const handlers=this._events_[type];
|
||||||
|
|
||||||
|
for(let handler of handlers){
|
||||||
|
handler.apply(this, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!this._events_['*']){
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
const catchAll=this._events_['*'];
|
||||||
|
|
||||||
|
for(let handler of catchAll){
|
||||||
|
handler.apply(this, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports=EventPubSub;
|
|
@ -1,81 +1,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
class EventPubSub {
|
let EventPubSub = require('./es6');
|
||||||
constructor(scope){
|
if(process.version[1]<5){
|
||||||
this._events_={};
|
EventPubSub = require('./es5');
|
||||||
this.publish=this.trigger=this.emit;
|
|
||||||
this.subscribe=this.on;
|
|
||||||
this.unSubscribe=this.off;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit(type,...args){
|
|
||||||
if(!this._events_[type]){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const handlers=this._events_[type];
|
|
||||||
|
|
||||||
for(let handler of handlers){
|
|
||||||
handler.apply(this, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!this._events_['*']){
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
const catchAll=this._events_['*'];
|
|
||||||
|
|
||||||
for(let handler of catchAll){
|
|
||||||
handler.apply(this, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports=EventPubSub;
|
module.exports=EventPubSub;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "event-pubsub",
|
"name": "event-pubsub",
|
||||||
"version": "4.0.1",
|
"version": "4.1.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!",
|
"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": {
|
||||||
|
|
Loading…
Add table
Reference in a new issue