-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
328 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,11 @@ npm-debug.log | |
yarn-dubug.log | ||
|
||
#vscode settings | ||
.vscode | ||
#.vscode | ||
|
||
# IDE dependencies | ||
.idea | ||
.idea/* | ||
|
||
#build | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"window.zoomLevel": 0, | ||
"editor.wordWrap": "on", | ||
"workbench.iconTheme": "vscode-icons", | ||
"files.associations": { | ||
"*.jsx": "javascriptreact", | ||
"*.scss": "scss", | ||
"*.js": "javascript", | ||
"*.ejs": "html" | ||
}, | ||
"vsicons.projectDetection.disableDetect": true, | ||
"vsicons.associations.files": [ | ||
{ | ||
"icon": "webpack", | ||
"extensions": ["webpack.config.parts.js"], | ||
"filename": true, | ||
"format": "svg" | ||
} | ||
], | ||
"editor.renderIndentGuides": true, | ||
"editor.tabSize": 2, | ||
"auto-close-tag.enableAutoCloseSelfClosingTag": true, | ||
"auto-close-tag.activationOnLanguage": ["*", "javascriptreact"], | ||
"emojisense.languages": { | ||
"markdown": true, | ||
"plaintext": true, | ||
"git-commit": false, | ||
"json": true, | ||
"javascript": true, | ||
"javascriptreact": true, | ||
"react": true, | ||
"html": true | ||
}, | ||
"editor.wordWrapColumn": 100, | ||
"prettier.printWidth": 100, | ||
"editor.formatOnSave": true, | ||
"workbench.colorTheme": "Material Theme", | ||
"git.enableSmartCommit": true, | ||
"eslint.autoFixOnSave": true, | ||
"prettier.eslintIntegration": true, | ||
"vsicons.presets.jsOfficial": true, | ||
"workbench.editor.enablePreview": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,45 @@ | ||
# walky-talky | ||
# walky-talky | ||
|
||
<p align="center"> | ||
<img src="https://user-images.githubusercontent.com/11384858/34708235-b9f905d6-f537-11e7-8068-433761706f91.png" /> | ||
<br> | ||
</p> | ||
|
||
EventListeners can easily go out of hand if not used with utmost care, walky-talky is a tiny JS library to create a message channel using Publish/Subscribe pattern. | ||
|
||
## Installation | ||
|
||
``` | ||
yarn add walky-talky | ||
``` | ||
|
||
or | ||
|
||
``` | ||
npm install walky-talky --save | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
// module.js | ||
import WalkyTalky from "walky-talky"; | ||
actionHandler = dataObject => { | ||
// some logic | ||
}; | ||
// subscribe to a custom action type | ||
WalkyTalky.subscribe("ACTION_TYPE", actionHandler); | ||
|
||
// app.js | ||
import WalkyTalky from "walky-talky"; | ||
// publish the action type with the data object | ||
WalkyTalky.publish("ACTION_TYPE", dataObject); | ||
``` | ||
|
||
## Like it? | ||
|
||
:star: this repo | ||
|
||
## License | ||
|
||
MIT © [Kamlesh Chandnani](https://github.com/kamleshchandnani) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* A tiny JS library to create a message channel using Publish/Subscribe pattern | ||
* The events are stored as an object where keys will be the type of actions and values will be array of listeners | ||
* There can be multiple listeners for a single action type | ||
*/ | ||
class WalkyTalky { | ||
constructor() { | ||
this.events = {}; | ||
} | ||
|
||
/** | ||
* Function to subscribe to an event/action | ||
* It checks for the actionType if it already exists, then adds the listener to the list of that particular actionType | ||
* If the actionType doesn't already exists then it creates the same and assigns the listeners to it's list. | ||
* @param {string} actionType | ||
* @param {object} listener | ||
* @memberof WalkyTalky | ||
*/ | ||
subscribe(actionType, listener) { | ||
// create the actionType if not yet created | ||
if (!this.events[actionType]) this.events[actionType] = []; | ||
// add the listener | ||
this.events[actionType].push(listener); | ||
} | ||
|
||
/** | ||
* Function to publish events/action | ||
* The subscribed event/action will get this message | ||
* This function iterates over the list of listeners and calls each of them with params for a particular actionType | ||
* @param {string} actionType | ||
* @param {object} data | ||
* @memberof WalkyTalky | ||
*/ | ||
publish(actionType, data) { | ||
// return if the actionType doesn't exist, or there are no listeners | ||
if (!this.events[actionType] || this.events[actionType].length < 1) return; | ||
|
||
// send the event to all listeners | ||
this.events[actionType].forEach((listener) => { | ||
listener(data || {}); | ||
}); | ||
} | ||
} | ||
|
||
export default new WalkyTalky(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,15 @@ | |
"author": "Kamlesh Chandnani <[email protected]>", | ||
"license": "ISC", | ||
"homepage": "https://github.com/kamleshchandnani/walky-talky#readme", | ||
"description": "A small JS library to address the problem of assigning multiple listeners for the same event/action type using Publish/Subscribe pattern", | ||
"description": "A tiny JS library to create a message channel using Publish/Subscribe pattern", | ||
"main": "dist/index.js", | ||
"module": "dist/index.es.js", | ||
"files": ["dist"], | ||
"scripts": { | ||
"clean": "rimraf dist", | ||
"build": "run-s clean && run-p build:es build:cjs build:lib:es", | ||
"build:es": "NODE_ENV=es rollup -c", | ||
"build:cjs": "NODE_ENV=cjs rollup -c", | ||
"build": "run-s clean && mkdir dist && run-p build:es build:cjs", | ||
"build:es": "NODE_ENV=es babel index.js -o dist/index.es.js", | ||
"build:cjs": "NODE_ENV=cjs babel index.js -o dist/index.js", | ||
"precommit": "lint-staged" | ||
}, | ||
"repository": { | ||
|
@@ -28,17 +28,12 @@ | |
"babel-eslint": "^8.0.2", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-kamlesh": "git+https://github.com/kamleshchandnani/babel-preset-kamlesh.git", | ||
"babel-preset-minify": "^0.2.0", | ||
"eslint": "^4.11.0", | ||
"eslint-config-kamlesh": "git+https://github.com/kamleshchandnani/eslint-config-kamlesh.git", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^5.0.0", | ||
"npm-run-all": "^4.1.2", | ||
"rollup": "^0.51.7", | ||
"rollup-plugin-babel": "^3.0.2", | ||
"rollup-plugin-commonjs": "^8.2.6", | ||
"rollup-plugin-filesize": "^1.4.2", | ||
"rollup-plugin-node-resolve": "^3.0.0", | ||
"rollup-plugin-uglify": "^2.0.1", | ||
"uglify-es": "^3.2.2" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.