Comparing version 0.2.5 to 0.3.0
@@ -11,14 +11,22 @@ 'use strict'; | ||
var innerEvents = ['onListenerAdded', 'onListenerRemoved', 'onFirstListenerAdded', 'onLastListenerRemoved']; | ||
/** | ||
* Channel | ||
* Channel of particular events. Allows attach/detach listeners and dispatch event data. | ||
* | ||
* @param {String} [name] | ||
* @param {Boolean} [noInnerEvents] | ||
* | ||
* @example | ||
* import Channel from 'chnl'; | ||
* | ||
* // create channel | ||
* const onMyEvent = new Channel.EventEmitter(); | ||
* // listen | ||
* onMyEvent.addListener(data => console.log(data)); | ||
* // dispatch data | ||
* onMyEvent.dispatch(data); | ||
*/ | ||
var innerEvents = ['onListenerAdded', 'onListenerRemoved', 'onFirstListenerAdded', 'onLastListenerRemoved']; | ||
var Channel = function () { | ||
/** | ||
* Constructor | ||
* @param {String} [name] | ||
* @param {Boolean} [noInnerEvents] | ||
*/ | ||
function Channel(name, noInnerEvents) { | ||
@@ -146,3 +154,3 @@ var _this = this; | ||
value: function mute() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
@@ -184,3 +192,3 @@ this._mute = true; | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? { args: [], async: false } : arguments[0]; | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { args: [], async: false }; | ||
@@ -187,0 +195,0 @@ if (!this._mute) { |
@@ -7,5 +7,3 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** | ||
* Simple event emitter based on channel | ||
*/ | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -20,6 +18,17 @@ var _channel = require('./channel'); | ||
/** | ||
* Event emitter similar to Node.js [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter). | ||
* The main difference from single channel is that each method takes additional `event` argument. | ||
* | ||
* @example | ||
* import Channel from 'chnl'; | ||
* | ||
* // create emitter | ||
* const emitter = new Channel.EventEmitter(); | ||
* // listen 'myEvent' | ||
* emitter.on('myEvent', data => console.log(data)); | ||
* // emit 'myEvent' | ||
* emitter.emit('myEvent', 'hello world!'); | ||
*/ | ||
var EventEmitter = function () { | ||
/** | ||
* Constructor | ||
*/ | ||
function EventEmitter() { | ||
@@ -26,0 +35,0 @@ _classCallCheck(this, EventEmitter); |
@@ -26,22 +26,27 @@ 'use strict'; | ||
/** | ||
* @private | ||
*/ | ||
/** | ||
* Chnl entry point | ||
*/ | ||
_channel2.default.EventEmitter = _eventEmitter2.default; | ||
_channel2.default.Subscription = _subscription2.default; | ||
_channel2.default.ReactSubscription = _reactSubscription2.default; | ||
var chnl = _channel2.default; | ||
chnl.EventEmitter = _eventEmitter2.default; | ||
chnl.Subscription = _subscription2.default; | ||
chnl.ReactSubscription = _reactSubscription2.default; | ||
exports.default = _channel2.default; | ||
exports.default = chnl; | ||
/* | ||
Can not export additional classes like: | ||
Can not export additional classes like: | ||
export { | ||
EventEmitter, | ||
Subscription, | ||
}; | ||
export { | ||
EventEmitter, | ||
Subscription, | ||
}; | ||
because in that case babel's output is not compatible with pure commonjs | ||
See: http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default | ||
*/ | ||
because in that case babel's output is not compatible with pure commonjs | ||
See: http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default | ||
*/ | ||
module.exports = exports['default']; |
@@ -19,43 +19,40 @@ 'use strict'; | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** | ||
* ReactSubscription is an utility class that extends Subscription class and allows to subscribe/unsubscribe | ||
* listeners in ReactComponent callbacks - componentDidMount/componentWillUnmount | ||
* Example: | ||
* Before: | ||
class Button extends React.Component { | ||
constructor() { | ||
super(); | ||
this.subscription = new Channel.Subscription([ | ||
{channel: onNewData, listener: this.handleNewData.bind(this)} | ||
]); | ||
} | ||
componentDidMount() { | ||
this.subscription.on(); | ||
} | ||
componentWillUnmount() { | ||
this.subscription.off(); | ||
} | ||
} | ||
After: | ||
class Button extends React.Component { | ||
constructor() { | ||
super(); | ||
new Channel.ReactSubscription(this, [ | ||
{channel: onNewData, listener: this.handleNewData.bind(this)} | ||
]); | ||
} | ||
} | ||
*/ | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
/** | ||
* Utility class that extends Subscription for using in ReactComponent - automatically attach/detach listeners | ||
* in `componentDidMount` / `componentWillUnmount`. | ||
* | ||
* @param {ReactComponent} component | ||
* @param {Array<{channel, event, listener}>} items | ||
* | ||
* @example | ||
* class Button extends React.Component { | ||
* constructor() { | ||
* super(); | ||
* new Channel.ReactSubscription(this, [ | ||
* {channel: onNewData, listener: this.handleNewData.bind(this)} | ||
* ]); | ||
* } | ||
* } | ||
* | ||
* // actually equals to (but with more boilerplate code): | ||
* class Button extends React.Component { | ||
* constructor() { | ||
* super(); | ||
* this.subscription = new Channel.Subscription([ | ||
* {channel: onNewData, listener: this.handleNewData.bind(this)} | ||
* ]); | ||
* } | ||
* componentDidMount() { | ||
* this.subscription.on(); | ||
* } | ||
* componentWillUnmount() { | ||
* this.subscription.off(); | ||
* } | ||
* } | ||
*/ | ||
var ReactSubscription = function (_Subscription) { | ||
_inherits(ReactSubscription, _Subscription); | ||
/** | ||
* Constructor | ||
* | ||
* @param {ReactComponent} component | ||
* @param {Array<{channel, event, listener}>} items | ||
*/ | ||
function ReactSubscription(component, items) { | ||
@@ -62,0 +59,0 @@ _classCallCheck(this, ReactSubscription); |
@@ -7,3 +7,3 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -16,4 +16,4 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
* Subscription item | ||
* @private | ||
*/ | ||
var SubscriptionItem = function () { | ||
@@ -71,6 +71,6 @@ /** | ||
value: function _applyMethod(method) { | ||
var _params = this._params; | ||
var channel = _params.channel; | ||
var event = _params.event; | ||
var listener = _params.listener; | ||
var _params = this._params, | ||
channel = _params.channel, | ||
event = _params.event, | ||
listener = _params.listener; | ||
@@ -83,6 +83,6 @@ var args = event ? [event, listener] : [listener]; | ||
value: function _assertParams() { | ||
var _params2 = this._params; | ||
var channel = _params2.channel; | ||
var event = _params2.event; | ||
var listener = _params2.listener; | ||
var _params2 = this._params, | ||
channel = _params2.channel, | ||
event = _params2.event, | ||
listener = _params2.listener; | ||
@@ -89,0 +89,0 @@ if (!channel || (typeof channel === 'undefined' ? 'undefined' : _typeof(channel)) !== 'object') { |
@@ -7,23 +7,3 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** | ||
* Subscription is utility class allowing dynamically attach/detach batch of listeners to event channels. | ||
* | ||
* Example: | ||
this._subscription = new Channel.Subscription([ | ||
{ | ||
channel: chrome.tabs.onUpdated, | ||
listener: this._onTabUpdated.bind(this) | ||
}, | ||
{ | ||
channel: document.getElementById('button'), | ||
event: 'click', | ||
listener: this._onButtonClick.bind(this) | ||
} | ||
]); | ||
this._subscription.on(); // now listeners are attached | ||
this._subscription.off(); // now listeners are detached | ||
* | ||
*/ | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -38,8 +18,22 @@ var _subscriptionItem = require('./subscription-item'); | ||
/** | ||
* Utility class allowing dynamically attach/detach batch of listeners to event channels. | ||
* | ||
* @param {Array<{channel, event, listener}>} items | ||
* | ||
* @example | ||
* import Channel from 'chnl'; | ||
* const subscription = new Channel.Subscription([ | ||
* { | ||
* channel: chrome.tabs.onUpdated, | ||
* listener: this._onTabUpdated.bind(this) | ||
* } | ||
* ]); | ||
* | ||
* // attach listeners | ||
* subscription.on(); | ||
* // detach listeners | ||
* subscription.off(); | ||
*/ | ||
var Subscription = function () { | ||
/** | ||
* Constructor | ||
* | ||
* @param {Array<{channel, event, listener}>} items | ||
*/ | ||
function Subscription(items) { | ||
@@ -46,0 +40,0 @@ _classCallCheck(this, Subscription); |
{ | ||
"name": "chnl", | ||
"version": "0.2.5", | ||
"description": "Chrome compatible javascript channels", | ||
"version": "0.3.0", | ||
"description": "Implementation of event channels compatible with Chrome extensions events API", | ||
"main": "./es5/index.js", | ||
@@ -12,16 +12,26 @@ "author": { | ||
"eslint": "eslint src test", | ||
"eslint-staged": "lint-staged", | ||
"check-deps": "check-dependencies", | ||
"code": "npm run check-deps && npm run eslint", | ||
"test": "ava", | ||
"ci": "npm run code && npm test", | ||
"coveralls": "coveralls < coverage/lcov.info", | ||
"docs-base": "node_modules/documentation/bin/documentation.js $CMD src/** -f html -o docs -g --document-exported --infer-private=\"^_\" -c documentation.yml", | ||
"docs": "CMD=build npm run docs-base", | ||
"docs-serve": "CMD=serve npm run docs-base -- --watch", | ||
"babel": "rm -rf ./es5 && babel ./src --out-dir ./es5", | ||
"publish-to-npm": "npm run babel && npm publish && git push --follow-tags", | ||
"pre-release": "npm run code && npm test", | ||
"release-patch": "npm run pre-release && npm version patch && npm run publish-to-npm", | ||
"release-minor": "npm run pre-release && npm version minor && npm run publish-to-npm" | ||
"release": "npm run code && npm test && npm version $VER && npm publish && git push --follow-tags --no-verify", | ||
"release-patch": "VER=patch npm run release", | ||
"release-minor": "VER=minor npm run release", | ||
"precommit": "npm run eslint-staged && npm test", | ||
"prepush": "npm run code && npm test", | ||
"prepublish": "npm run babel && npm run test" | ||
}, | ||
"lint-staged": { | ||
"{src,test}/**/*.js": "eslint" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.16.0", | ||
"ava": "^0.20.0", | ||
"babel-cli": "^6.14.0", | ||
"babel-eslint": "^6.1.2", | ||
"babel-eslint": "^7.2.3", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
@@ -32,7 +42,9 @@ "babel-preset-es2015": "^6.14.0", | ||
"coveralls": "^2.11.12", | ||
"eslint": "^3.5.0", | ||
"eslint-plugin-ava": "^3.0.0", | ||
"eslint-plugin-babel": "^3.3.0", | ||
"ghooks": "^1.3.2", | ||
"sinon": "^1.17.5" | ||
"documentation": "^4.0.0-rc.1", | ||
"eslint": "^4.1.1", | ||
"eslint-plugin-ava": "^4.2.1", | ||
"eslint-plugin-babel": "^4.1.1", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^4.0.0", | ||
"sinon": "^2.3.6" | ||
}, | ||
@@ -39,0 +51,0 @@ "config": { |
@@ -1,5 +0,1 @@ | ||
/** | ||
* Channel | ||
*/ | ||
const innerEvents = [ | ||
@@ -12,8 +8,19 @@ 'onListenerAdded', | ||
/** | ||
* Channel of particular events. Allows attach/detach listeners and dispatch event data. | ||
* | ||
* @param {String} [name] | ||
* @param {Boolean} [noInnerEvents] | ||
* | ||
* @example | ||
* import Channel from 'chnl'; | ||
* | ||
* // create channel | ||
* const onMyEvent = new Channel.EventEmitter(); | ||
* // listen | ||
* onMyEvent.addListener(data => console.log(data)); | ||
* // dispatch data | ||
* onMyEvent.dispatch(data); | ||
*/ | ||
export default class Channel { | ||
/** | ||
* Constructor | ||
* @param {String} [name] | ||
* @param {Boolean} [noInnerEvents] | ||
*/ | ||
constructor(name, noInnerEvents) { | ||
@@ -20,0 +27,0 @@ this._listeners = []; |
@@ -0,11 +1,18 @@ | ||
import Channel from './channel'; | ||
/** | ||
* Simple event emitter based on channel | ||
* Event emitter similar to Node.js [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter). | ||
* The main difference from single channel is that each method takes additional `event` argument. | ||
* | ||
* @example | ||
* import Channel from 'chnl'; | ||
* | ||
* // create emitter | ||
* const emitter = new Channel.EventEmitter(); | ||
* // listen 'myEvent' | ||
* emitter.on('myEvent', data => console.log(data)); | ||
* // emit 'myEvent' | ||
* emitter.emit('myEvent', 'hello world!'); | ||
*/ | ||
import Channel from './channel'; | ||
export default class EventEmitter { | ||
/** | ||
* Constructor | ||
*/ | ||
constructor() { | ||
@@ -12,0 +19,0 @@ this._channels = new Map(); |
@@ -10,17 +10,22 @@ /** | ||
Channel.EventEmitter = EventEmitter; | ||
Channel.Subscription = Subscription; | ||
Channel.ReactSubscription = ReactSubscription; | ||
/** | ||
* @private | ||
*/ | ||
const chnl = Channel; | ||
chnl.EventEmitter = EventEmitter; | ||
chnl.Subscription = Subscription; | ||
chnl.ReactSubscription = ReactSubscription; | ||
export default Channel; | ||
export default chnl; | ||
/* | ||
Can not export additional classes like: | ||
Can not export additional classes like: | ||
export { | ||
EventEmitter, | ||
Subscription, | ||
}; | ||
export { | ||
EventEmitter, | ||
Subscription, | ||
}; | ||
because in that case babel's output is not compatible with pure commonjs | ||
See: http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default | ||
*/ | ||
because in that case babel's output is not compatible with pure commonjs | ||
See: http://stackoverflow.com/questions/33505992/babel-6-changes-how-it-exports-default | ||
*/ |
@@ -0,42 +1,37 @@ | ||
import Subscription from './subscription'; | ||
/** | ||
* ReactSubscription is an utility class that extends Subscription class and allows to subscribe/unsubscribe | ||
* listeners in ReactComponent callbacks - componentDidMount/componentWillUnmount | ||
* Example: | ||
* Before: | ||
class Button extends React.Component { | ||
constructor() { | ||
super(); | ||
this.subscription = new Channel.Subscription([ | ||
{channel: onNewData, listener: this.handleNewData.bind(this)} | ||
]); | ||
} | ||
componentDidMount() { | ||
this.subscription.on(); | ||
} | ||
componentWillUnmount() { | ||
this.subscription.off(); | ||
} | ||
} | ||
After: | ||
class Button extends React.Component { | ||
constructor() { | ||
super(); | ||
new Channel.ReactSubscription(this, [ | ||
{channel: onNewData, listener: this.handleNewData.bind(this)} | ||
]); | ||
} | ||
} | ||
* Utility class that extends Subscription for using in ReactComponent - automatically attach/detach listeners | ||
* in `componentDidMount` / `componentWillUnmount`. | ||
* | ||
* @param {ReactComponent} component | ||
* @param {Array<{channel, event, listener}>} items | ||
* | ||
* @example | ||
* class Button extends React.Component { | ||
* constructor() { | ||
* super(); | ||
* new Channel.ReactSubscription(this, [ | ||
* {channel: onNewData, listener: this.handleNewData.bind(this)} | ||
* ]); | ||
* } | ||
* } | ||
* | ||
* // actually equals to (but with more boilerplate code): | ||
* class Button extends React.Component { | ||
* constructor() { | ||
* super(); | ||
* this.subscription = new Channel.Subscription([ | ||
* {channel: onNewData, listener: this.handleNewData.bind(this)} | ||
* ]); | ||
* } | ||
* componentDidMount() { | ||
* this.subscription.on(); | ||
* } | ||
* componentWillUnmount() { | ||
* this.subscription.off(); | ||
* } | ||
* } | ||
*/ | ||
import Subscription from './subscription'; | ||
export default class ReactSubscription extends Subscription { | ||
/** | ||
* Constructor | ||
* | ||
* @param {ReactComponent} component | ||
* @param {Array<{channel, event, listener}>} items | ||
*/ | ||
constructor(component, items) { | ||
@@ -43,0 +38,0 @@ super(items); |
/** | ||
* Subscription item | ||
* @private | ||
*/ | ||
export default class SubscriptionItem { | ||
@@ -6,0 +6,0 @@ /** |
@@ -0,31 +1,23 @@ | ||
import SubscriptionItem from './subscription-item'; | ||
/** | ||
* Subscription is utility class allowing dynamically attach/detach batch of listeners to event channels. | ||
* Utility class allowing dynamically attach/detach batch of listeners to event channels. | ||
* | ||
* Example: | ||
this._subscription = new Channel.Subscription([ | ||
{ | ||
channel: chrome.tabs.onUpdated, | ||
listener: this._onTabUpdated.bind(this) | ||
}, | ||
{ | ||
channel: document.getElementById('button'), | ||
event: 'click', | ||
listener: this._onButtonClick.bind(this) | ||
} | ||
]); | ||
this._subscription.on(); // now listeners are attached | ||
this._subscription.off(); // now listeners are detached | ||
* @param {Array<{channel, event, listener}>} items | ||
* | ||
* @example | ||
* import Channel from 'chnl'; | ||
* const subscription = new Channel.Subscription([ | ||
* { | ||
* channel: chrome.tabs.onUpdated, | ||
* listener: this._onTabUpdated.bind(this) | ||
* } | ||
* ]); | ||
* | ||
* // attach listeners | ||
* subscription.on(); | ||
* // detach listeners | ||
* subscription.off(); | ||
*/ | ||
import SubscriptionItem from './subscription-item'; | ||
export default class Subscription { | ||
/** | ||
* Constructor | ||
* | ||
* @param {Array<{channel, event, listener}>} items | ||
*/ | ||
constructor(items) { | ||
@@ -32,0 +24,0 @@ this._items = items.map(params => new SubscriptionItem(params)); |
15
1267
43528
15
57