Skip to content

Commit

Permalink
- Added core logic
Browse files Browse the repository at this point in the history
- Added build scripts
- Removed rollup
  • Loading branch information
kamleshchandnani committed Jan 9, 2018
1 parent 9aa9fcc commit 3d943df
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 236 deletions.
17 changes: 3 additions & 14 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,12 @@
"modules": false,
"useBuiltIns": false
}
]
],
"minify"
]
},
"cjs": {
"presets": [
[
"kamlesh",
{
"targets": {
"browsers": ["last 2 Chrome versions", "safari >=11"]
},
"loose": false,
"modules": false,
"useBuiltIns": false
}
]
]
"presets": ["kamlesh", "minify"]
}
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ npm-debug.log
yarn-dubug.log

#vscode settings
.vscode
#.vscode

# IDE dependencies
.idea
.idea/*

#build
dist
43 changes: 43 additions & 0 deletions .vscode/settings.json
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
}
46 changes: 45 additions & 1 deletion README.md
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)
45 changes: 45 additions & 0 deletions index.js
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();
15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
32 changes: 0 additions & 32 deletions rollup.config.js

This file was deleted.

Loading

0 comments on commit 3d943df

Please sign in to comment.