diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8f50f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +npm-debug.log diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..d838da9 --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +examples/ diff --git a/es5.js b/es5.js index bd74e05..ef6e7fc 100644 --- a/es5.js +++ b/es5.js @@ -9,8 +9,7 @@ function EventPubSub() { function on(type,handler){ if(!handler){ - const err=new ReferenceError('handler not defined.'); - throw(err); + throw new ReferenceError('handler not defined.'); } if(!this._events_[type]){ @@ -27,8 +26,7 @@ function EventPubSub() { } 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; + throw new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler'); } if(handler=='*'){ diff --git a/es6.js b/es6.js index 00ced6f..02578f8 100644 --- a/es6.js +++ b/es6.js @@ -1,85 +1,83 @@ 'use strict'; class EventPubSub { - constructor(scope){ - this._events_={}; - this.publish=this.trigger=this.emit; - this.subscribe=this.on; - this.unSubscribe=this.off; + 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); + on( type, handler ) { + if ( !handler ) { + throw new ReferenceError( 'handler not defined.' ); } - if(!this._events_[type]){ - this._events_[type]=[]; + if ( !this._events_[ type ] ) { + this._events_[ type ] = []; } - this._events_[type].push(handler); + this._events_[ type ].push( handler ); return this; } - off(type,handler){ - if(!this._events_[type]){ + 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 ) { + throw new ReferenceError( 'handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler' ); } - if(handler=='*'){ - delete this._events_[type]; + if ( handler == '*' ) { + delete this._events_[ type ]; return this; } - const handlers=this._events_[type]; + const handlers = this._events_[ type ]; - while(handlers.includes(handler)){ + while ( handlers.includes( handler ) ) { handlers.splice( - handlers.indexOf(handler), + handlers.indexOf( handler ), 1 ); } - if(handlers.length<1){ - delete this._events_[type]; + if ( handlers.length < 1 ) { + delete this._events_[ type ]; } return this; } - emit(type,...args){ - if(!this._events_[type]){ - return this.emit$(type,...args); + emit( type, ...args ) { + if ( !this._events_[ type ] ) { + return this.emit$( type, ...args ); } - const handlers=this._events_[type]; + const handlers = this._events_[ type ]; - for(let handler of handlers){ - handler.apply(this, args); + for ( let handler of handlers ) { + handler.apply( this, args ); } - return this.emit$(type,...args); + return this.emit$( type, ...args ); } - emit$(type,...args){ - if(!this._events_['*']){ + emit$( type, ...args ) { + if ( !this._events_[ '*' ] ) { return this; } - const catchAll=this._events_['*']; + const catchAll = this._events_[ '*' ]; - for(let handler of catchAll){ - handler.call(this, type, ...args); + for ( let handler of catchAll ) { + handler.call( this, type, ...args ); } return this; } } -module.exports=EventPubSub; +module.exports = EventPubSub; diff --git a/npm-debug.log b/npm-debug.log deleted file mode 100644 index 3518ddb..0000000 --- a/npm-debug.log +++ /dev/null @@ -1,109 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'publish' ] -2 info using npm@3.10.8 -3 info using node@v6.7.0 -4 verbose publish [ '.' ] -5 silly cache add args [ '.', null ] -6 verbose cache add spec . -7 silly cache add parsed spec Result { -7 silly cache add raw: '.', -7 silly cache add scope: null, -7 silly cache add escapedName: null, -7 silly cache add name: null, -7 silly cache add rawSpec: '.', -7 silly cache add spec: '/home/ssmp/git/event-pubsub', -7 silly cache add type: 'directory' } -8 verbose addLocalDirectory /home/ssmp/.npm/event-pubsub/4.2.2/package.tgz not in flight; packing -9 verbose correctMkdir /home/ssmp/.npm correctMkdir not in flight; initializing -10 info lifecycle event-pubsub@4.2.2~prepublish: event-pubsub@4.2.2 -11 silly lifecycle event-pubsub@4.2.2~prepublish: no script for prepublish, continuing -12 verbose tar pack [ '/home/ssmp/.npm/event-pubsub/4.2.2/package.tgz', -12 verbose tar pack '/home/ssmp/git/event-pubsub' ] -13 verbose tarball /home/ssmp/.npm/event-pubsub/4.2.2/package.tgz -14 verbose folder /home/ssmp/git/event-pubsub -15 verbose addLocalTarball adding from inside cache /home/ssmp/.npm/event-pubsub/4.2.2/package.tgz -16 verbose correctMkdir /home/ssmp/.npm correctMkdir not in flight; initializing -17 silly cache afterAdd event-pubsub@4.2.2 -18 verbose afterAdd /home/ssmp/.npm/event-pubsub/4.2.2/package/package.json not in flight; writing -19 verbose correctMkdir /home/ssmp/.npm correctMkdir not in flight; initializing -20 verbose afterAdd /home/ssmp/.npm/event-pubsub/4.2.2/package/package.json written -21 silly publish { name: 'event-pubsub', -21 silly publish version: '4.2.2', -21 silly publish 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!', -21 silly publish main: 'event-pubsub.js', -21 silly publish directories: { example: 'examples' }, -21 silly publish engines: { node: '>=4.0.0' }, -21 silly publish scripts: { test: 'echo "Error: no test specified" && exit 1' }, -21 silly publish repository: -21 silly publish { type: 'git', -21 silly publish url: 'git+https://github.com/RIAEvangelist/event-pubsub.git' }, -21 silly publish keywords: [ 'event', 'events', 'pubsub', 'node', 'browser' ], -21 silly publish author: { name: 'Brandon Nozaki Miller' }, -21 silly publish license: 'Unlicense', -21 silly publish bugs: { url: 'https://github.com/RIAEvangelist/event-pubsub/issues' }, -21 silly publish homepage: 'https://github.com/RIAEvangelist/event-pubsub', -21 silly publish readme: '# Event PubSub\n\nnpm info : \n![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)\n\nGitHub info : \n![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)\n\n***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.\n\nFor older versions of node and io.js the latest versions of `event-pubsub` may work with the --harmony flag. Officially though, we support node v4 and newer with es5 and es6\n\nEasy for any developer level. No frills, just high speed events following the publisher subscriber pattern!\n\n[Pretty GitHub.io site](http://riaevangelist.github.io/event-pubsub/) \n\n[See NPM stats for event-pubsub](http://npm-stat.com/charts.html?package=event-pubsub&author=&from=&to=)\n\n**EXAMPLE FILES** \n\n1. [Node Event PubSub Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/node) \n2. [Browser Event PubSub Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/browser)\n\n**Node Install** \n`npm i --save event-pubsub` \nBy default the correct version (ES5/ES6) will be included. You can force the es5/6 version by requiring `event-pubsub/es5` or `event-pubsub/es6`.\n\n**Browser Install** \n*see browser examples above or below* \n\n```html\n\n\n\n\n```\n\n# Methods\n\n|Method|Arguments|Description|\n|------|---------|-----------|\n|subscribe|type (string), handler(function)|will bind the `handler` function to the the `type` event. Just like `addEventListener` in the browser|\n|on|same as above|same as above|\n|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.|\n|off|same as above|same as above|\n|publish|type (string), ...data arguments|will call all `handler` functions bound to the event `type` and pass all `...data arguments` to those handlers|\n|emit|same as above|same as above|\n|trigger|same as above|same as above|\n\nWhile `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`.\n\n# The ` * ` type\n\nThe ` * ` 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.\n\n\n## Basic Examples\n\n***NOTE - the only difference between node and browser code is how the `events` variable is created*** \n* node ` const events = new Events `\n* browser `const events = new window.EventPubSub;`\n\n#### Node\n\n```javascript\n\n// ES5/ES6 now automatically chosen based on node version\nconst Events = new require(\'event-pubsub\');\nconst events=new Events;\n\nevents.on(\n \'hello\',\n function(data){\n console.log(\'hello event recieved \', data);\n events.emit(\n \'world\',\n {\n type:\'myObject\',\n data:{\n x:\'YAY, Objects!\'\n }\n }\n )\n }\n);\n\nevents.on(\n \'world\',\n function(data){\n console.log(\'World event got\',data);\n events.off(\'*\',\'*\');\n console.log(\'Removed all events\');\n }\n);\n\nevents.emit(\n \'hello\',\n \'world\'\n);\n\n\n\n```\n\n#### Basic Chaining\n\n```javascript\n\nevents.on(\n \'hello\',\n someFunction\n).on(\n \'goodbye\',\n anotherFunction\n).emit(\n \'hello\',\n \'world\'\n);\n\nevents.emit(\n \'goodbye\',\n \'complexity\'\n).off(\n \'hello\',\n \'*\'\n);\n\n```\n\n#### Browser\n##### HTML\n\n```html\n\n\n \n \n
\n