Skip to content

Commit

Permalink
add reset api
Browse files Browse the repository at this point in the history
  • Loading branch information
doanndd committed Sep 8, 2016
1 parent 177eb32 commit 87634b7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ dispatch(remoteDelete(1));
1. **add(type, actionCreator)** Add new action creator to hub
2. **remove(type)** Remove action creator from hub
3. **replace(type, actionCreator)** Replace action creator from hub with new action creator
4. **reset()** Reset actions data
5 changes: 5 additions & 0 deletions __tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ describe('ActionsHub', () => {
expect(Actions.ADD('new')).toBe('new');
expect(Actions.ADD({id: 1})).toEqual({id: 1});
});

it('reset(): reset actions data', () => {
Actions.reset();
expect(() => Actions.ADD()).toThrow();
});
});
34 changes: 33 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

var _actions = {};

// Private method : define action property
/**
* defineActionProp - define action property
*
* @param {string} type action's type
* @returns {undefined}
*/
function defineActionProp(type) {
ActionsHub[type] = function() {
var creators = _actions[type];
Expand All @@ -24,6 +29,13 @@ function defineActionProp(type) {

// Actions Hub
const ActionsHub = {
/**
* add - add an action
*
* @param {string} type action's type
* @param {mixed} actionCreator action creator function
* @returns {function} registered action creator function
*/
add(type, actionCreator) {
if (typeof actionCreator !== 'function') {
// Make a new actionCreator
Expand All @@ -48,14 +60,34 @@ const ActionsHub = {
return ActionsHub[type];
},

/**
* remove - remove action type
*
* @param {string} type action's type
* @returns {undefined}
*/
remove(type) {
_actions[type] = [];
},

/**
* replace - replace old action creator with new one
*
* @param {string} type action's type
* @param {mixed} actionCreator action creator function
* @returns {function} registered action creator function
*/
replace(type, actionCreator) {
ActionsHub.remove(type);
return ActionsHub.add(type, actionCreator);
},

/**
* reset - reset actions data
*/
reset() {
_actions = {};
},
};

module.exports = ActionsHub;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-actions-hub",
"version": "0.1.1",
"version": "0.1.2",
"description": "Share Redux Actions between modules",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 87634b7

Please sign in to comment.