Comparing version 0.3.0 to 0.4.0
@@ -23,3 +23,3 @@ 'use strict'; | ||
* // create channel | ||
* const onMyEvent = new Channel.EventEmitter(); | ||
* const onMyEvent = new Channel(); | ||
* // listen | ||
@@ -87,4 +87,3 @@ * onMyEvent.addListener(data => console.log(data)); | ||
if (index >= 0) { | ||
this._listeners.splice(index, 1); | ||
this._dispatchInnerRemoveEvents.apply(this, arguments); | ||
this._spliceListener(index); | ||
} | ||
@@ -94,2 +93,14 @@ } | ||
/** | ||
* Remove all listeners from channel. | ||
*/ | ||
}, { | ||
key: 'removeAllListeners', | ||
value: function removeAllListeners() { | ||
while (this.hasListeners()) { | ||
this._spliceListener(0); | ||
} | ||
} | ||
/** | ||
* Is listener exist | ||
@@ -325,2 +336,19 @@ * @param {Function} callback | ||
} | ||
/** | ||
* Splice listener under index | ||
* @param {Number} index | ||
*/ | ||
}, { | ||
key: '_spliceListener', | ||
value: function _spliceListener(index) { | ||
var listener = this._listeners[index]; | ||
this._listeners.splice(index, 1); | ||
var args = [listener.callback]; | ||
if (listener.context) { | ||
args.push(listener.context); | ||
} | ||
this._dispatchInnerRemoveEvents.apply(this, args); | ||
} | ||
}]); | ||
@@ -327,0 +355,0 @@ |
{ | ||
"name": "chnl", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Implementation of event channels compatible with Chrome extensions events API", | ||
@@ -21,4 +21,5 @@ "main": "./es5/index.js", | ||
"docs-serve": "CMD=serve npm run docs-base -- --watch", | ||
"docs-commit": "npm run docs && git add docs && git commit -m\"docs\"", | ||
"babel": "rm -rf ./es5 && babel ./src --out-dir ./es5", | ||
"release": "npm run code && npm test && npm version $VER && npm publish && git push --follow-tags --no-verify", | ||
"release": "npm run code && npm test && npm version $VER && npm publish && npm run docs-commit && git push --follow-tags --no-verify", | ||
"release-patch": "VER=patch npm run release", | ||
@@ -43,8 +44,8 @@ "release-minor": "VER=minor npm run release", | ||
"documentation": "^4.0.0-rc.1", | ||
"eslint": "^4.1.1", | ||
"eslint": "^4.2.0", | ||
"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" | ||
"lint-staged": "^4.0.1", | ||
"sinon": "^2.3.7" | ||
}, | ||
@@ -51,0 +52,0 @@ "config": { |
@@ -8,6 +8,6 @@ # chnl | ||
Implementation of event channels (aka pub/sub, dispatcher, emitter) inspired and | ||
Implementation of event channels (pub/sub, dispatcher, emitter) inspired and | ||
compatible with [Chrome extensions events API](https://developer.chrome.com/extensions/events#type-Event). | ||
## Installation | ||
## Install | ||
``` | ||
@@ -17,3 +17,3 @@ npm i chnl --save | ||
## Documentation | ||
## Docs | ||
https://vitalets.github.io/chnl | ||
@@ -27,9 +27,8 @@ | ||
// create channel | ||
exports.onChanged = new Channel(); | ||
export const myChannel = new Channel(); | ||
// dispatch event (and don't care if there module B and what it will do with event) | ||
exports.onChanged.dispatch(data); | ||
// dispatch event asynchronously via setTimeout(..., 0); | ||
exports.onChanged.dispatchAsync(data); | ||
// dispatch event with data | ||
setTimeout(() => { | ||
myChannel.dispatch({foo: 'bar'}); | ||
}, 1000); | ||
``` | ||
@@ -39,19 +38,6 @@ | ||
```js | ||
import moduleA from './moduleA'; | ||
import {myChannel} from './moduleA'; | ||
// subscribe on channel | ||
moduleA.onChanged.addListener(data => { | ||
console.log('moduleA.onChanged', data); | ||
}); | ||
// subscribe once | ||
moduleA.onChanged.addOnceListener(data => { | ||
console.log('moduleA.onChanged once', data); | ||
}); | ||
// mute channel (optionally you can accumulate events to dispatch them after unmute) | ||
moduleA.onChanged.mute({accumulate: true}); | ||
// unmute channel | ||
moduleA.onChanged.unmute(); | ||
// subscribe on channel and log event data | ||
myChannel.addListener(data => console.log('myChannel event come with data', data)); | ||
``` | ||
@@ -58,0 +44,0 @@ |
@@ -18,3 +18,3 @@ const innerEvents = [ | ||
* // create channel | ||
* const onMyEvent = new Channel.EventEmitter(); | ||
* const onMyEvent = new Channel(); | ||
* // listen | ||
@@ -65,4 +65,3 @@ * onMyEvent.addListener(data => console.log(data)); | ||
if (index >= 0) { | ||
this._listeners.splice(index, 1); | ||
this._dispatchInnerRemoveEvents.apply(this, arguments); | ||
this._spliceListener(index); | ||
} | ||
@@ -72,2 +71,11 @@ } | ||
/** | ||
* Remove all listeners from channel. | ||
*/ | ||
removeAllListeners() { | ||
while (this.hasListeners()) { | ||
this._spliceListener(0); | ||
} | ||
} | ||
/** | ||
* Is listener exist | ||
@@ -241,2 +249,16 @@ * @param {Function} callback | ||
} | ||
/** | ||
* Splice listener under index | ||
* @param {Number} index | ||
*/ | ||
_spliceListener(index) { | ||
const listener = this._listeners[index]; | ||
this._listeners.splice(index, 1); | ||
const args = [listener.callback]; | ||
if (listener.context) { | ||
args.push(listener.context); | ||
} | ||
this._dispatchInnerRemoveEvents.apply(this, args); | ||
} | ||
} |
44207
1311
43