Skip to content

Commit

Permalink
remove Reducer cyclic dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
aksonov committed Sep 24, 2018
1 parent f51f469 commit 444764e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
4 changes: 2 additions & 2 deletions examples/react-native/Example.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const styles = StyleSheet.create({
},
});

const reducerCreate = params => {
const defaultReducer = new Reducer(params);
const defaultReducer = Reducer(Actions);
const reducerCreate = () => {
return (state, action) => {
console.log('reducer: ACTION:', action);
return defaultReducer(state, action);
Expand Down
67 changes: 32 additions & 35 deletions src/Reducer.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import isEqual from 'lodash.isequal';
import { NavigationActions, StackActions } from 'react-navigation';
import navigationStore from './navigationStore';
import * as ActionConst from './ActionConst';
import { getActiveState, popPrevious } from './State';

export function reducer(state, action) {
const { type, routeName } = action;
if (type === ActionConst.POP_TO) {
let nextScene = '';
let newState = state;
let currentState = state;
while (newState && nextScene !== routeName) {
newState = navigationStore.getStateForAction(StackActions.pop(), currentState);
if (newState) {
nextScene = getActiveState(newState).routeName;
if (isEqual(currentState, newState)) {
console.warn(`popTo called with an unknown routeName: ${routeName}`);
break;
}
if (nextScene !== routeName) {
currentState = newState;
export default function createReducer(navigationStore) {
return (state, action) => {
const { type, routeName } = action;
if (type === ActionConst.POP_TO) {
let nextScene = '';
let newState = state;
let currentState = state;
while (newState && nextScene !== routeName) {
newState = navigationStore.getStateForAction(StackActions.pop(), currentState);
if (newState) {
nextScene = getActiveState(newState).routeName;
if (isEqual(currentState, newState)) {
console.warn(`popTo called with an unknown routeName: ${routeName}`);
break;
}
if (nextScene !== routeName) {
currentState = newState;
}
}
}
return nextScene === routeName ? newState : state;
}
return nextScene === routeName ? newState : state;
}
if (type === ActionConst.REPLACE) {
const newState = navigationStore.getStateForAction(
NavigationActions.navigate({
routeName,
params: action.params,
}),
state,
);
const res = popPrevious(newState, routeName);
return res;
}
return navigationStore.getStateForAction(action, state) || state;
}

export default function createReducer() {
return reducer;
if (type === ActionConst.REPLACE) {
const newState = navigationStore.getStateForAction(
NavigationActions.navigate({
routeName,
params: action.params,
}),
state,
);
const res = popPrevious(newState, routeName);
return res;
}
return navigationStore.getStateForAction(action, state) || state;
};
}
3 changes: 2 additions & 1 deletion src/navigationStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TabBarBottom as DEPRECATED_TabBarBottom,
} from 'react-navigation-deprecated-tab-navigator';
import PropTypes from 'prop-types';
import { reducer } from './Reducer';
import createReducer from './Reducer';
import * as ActionConst from './ActionConst';
import { OnEnter, OnExit, assert } from './Util';
import { LeftButton, RightButton, BackButton } from './NavBar';
Expand Down Expand Up @@ -520,6 +520,7 @@ class NavigationStore {

setCustomReducer = (Navigator) => {
this.getStateForAction = Navigator.router.getStateForAction;
const reducer = createReducer(this);
Navigator.router.getStateForAction = (cmd, state) => (this.reducer ? this.reducer(state, cmd) : reducer(state, cmd));
};

Expand Down

0 comments on commit 444764e

Please sign in to comment.