Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Commit

Permalink
feat: remove exported actionMetaInterceptor
Browse files Browse the repository at this point in the history
will be implementation details only + readme
  • Loading branch information
jony89 committed Jan 24, 2018
1 parent d1e8439 commit 14ae142
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ Now we can create two instances :

```js
import { connect } from 'react-redux';
import { actionCreatorsInterceptor, actionMetaInterceptor } from 'reducer-action-interceptor';
import { actionCreatorsInterceptor } from 'reducer-action-interceptor';
import MyContainer, { actionCreators } from '../MyContainer';

/**
* First Container
**/
const mapStateToPropsFirstComp = state => ({ ...state.firstContainerReducer });
// actionMetaInterceptor(FIRST_CONTAINER_TYPE) is optional, we can intercept however we like
export const MyFirstContainerConnected = connect(
mapStateToPropsFirstComp,
actionCreatorsInterceptor(actionCreators, actionMetaInterceptor('FIRST_CONTAINER_TYPE')),
actionCreatorsInterceptor(actionCreators, 'INSTANCE1'),
)(MyContainer);

/**
Expand All @@ -39,20 +38,20 @@ export const MyFirstContainerConnected = connect(
const mapStateToPropsSecondComp = state => ({ ...state.secondContainerReducer });
export const MySecondContainerConnected = connect(
mapStateToPropsSecondComp,
actionCreatorsInterceptor(actionCreators, actionMetaInterceptor('SECOND_CONTAINER_TYPE')),
actionCreatorsInterceptor(actionCreators, 'INSTANCE2'),
)(MyContainer);
```

Obviously, we need to take care of the reducers as well :
Obviously, we need to take care of the reducers as well. using the HOR - `reducerInterceptor` :

```js
import { combineReducers } from 'redux';
import { reducerInterceptor } from 'reducer-action-interceptor';
import someGenericReducer from './someGenericReducer.reducer';

export default combineReducers({
firstContainerReducer: reducerInterceptor(someGenericReducer, 'FIRST_CONTAINER_TYPE'),
secondContainerReducer: reducerInterceptor(someGenericReducer, 'SECOND_CONTAINER_TYPE'),
firstContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE1'),
secondContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE2'),
});
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reducer-action-interceptor",
"version": "0.1.1",
"version": "0.2.0",
"description": "reducer-action-interceptor allows you on one hand to intercept action creators in order to patch the action before bubblig up to the reducers, and on the other hand to intercept the reducers in order to take advantage of that interception",
"main": "index.js",
"jsnext:main": "es/index.js",
Expand Down
7 changes: 3 additions & 4 deletions spec/actionCreatorsInterceptor.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createAction } from 'redux-actions';
import actionCreatorsInterceptor from '../src/actionCreatorsInterceptor';
import actionMetaInterceptor from '../src/actionMetaInterceptor';

const MY_COOL_ACTION_1 = 'MY_COOL_ACTION_1';
const MY_COOL_ACTION_2 = 'MY_COOL_ACTION_2';
Expand All @@ -17,15 +16,15 @@ describe('actionCreatorsInterceptor', () => {
it('should create new reference for intercepted actions', () => {
const interceptedActionCreators = actionCreatorsInterceptor(
actionCreators,
actionMetaInterceptor(MY_UNIQUE_VALUE)
MY_UNIQUE_VALUE
);
expect(interceptedActionCreators).not.toBe(actionCreators);
});

it('should contain the same exact action keys', () => {
const interceptedActionCreators = actionCreatorsInterceptor(
actionCreators,
actionMetaInterceptor(MY_UNIQUE_VALUE)
MY_UNIQUE_VALUE
);
expect(interceptedActionCreators.myFirstCoolActionCreator).toBeDefined();
expect(interceptedActionCreators.mySecondCoolActionCreator).toBeDefined();
Expand All @@ -35,7 +34,7 @@ describe('actionCreatorsInterceptor', () => {
it('should intercept action post dispatch', async () => {
const interceptedActionCreators = actionCreatorsInterceptor(
actionCreators,
actionMetaInterceptor(MY_UNIQUE_VALUE)
MY_UNIQUE_VALUE
);
const interceptedAction = interceptedActionCreators.myFirstCoolActionCreator;
const store = global.mock.createEmptyStore();
Expand Down
8 changes: 6 additions & 2 deletions src/actionCreatorsInterceptor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export default (actions: mixed[], interceptor: () => mixed) => {
import actionMetaInterceptor from './actionMetaInterceptor';

export default (actions: {}, instanceName: string) => {
if (!actions) return actions;

const actionInterceptor = actionMetaInterceptor(instanceName)

Object.keys(actions).forEach(key => {
const actionCreator = actions[key];

Expand All @@ -9,7 +13,7 @@ export default (actions: mixed[], interceptor: () => mixed) => {
if (typeof action === 'function') {
return action(dispatchInterceptor, getState);
}
return dispatch(interceptor(action));
return dispatch(actionInterceptor(action));
};

const action = actionCreator(...args);
Expand Down
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import reducerInterceptor from './reducerInterceptor';
import actionMetaInterceptor from './actionMetaInterceptor';
import actionCreatorsInterceptor from './actionCreatorsInterceptor';

export default {
reducerInterceptor,
actionMetaInterceptor,
actionCreatorsInterceptor
}

0 comments on commit 14ae142

Please sign in to comment.