From 444764ea149f8639b9eb573bc22d08cd458b8e76 Mon Sep 17 00:00:00 2001 From: aksonov Date: Mon, 24 Sep 2018 15:54:15 +0200 Subject: [PATCH] remove Reducer cyclic dependency --- examples/react-native/Example.js | 4 +- src/Reducer.js | 67 +++++++++++++++----------------- src/navigationStore.js | 3 +- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/examples/react-native/Example.js b/examples/react-native/Example.js index 0070b95de..a90ba79a0 100644 --- a/examples/react-native/Example.js +++ b/examples/react-native/Example.js @@ -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); diff --git a/src/Reducer.js b/src/Reducer.js index c1459b4ca..184a960e9 100644 --- a/src/Reducer.js +++ b/src/Reducer.js @@ -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; + }; } diff --git a/src/navigationStore.js b/src/navigationStore.js index 5e519eb74..98839a472 100644 --- a/src/navigationStore.js +++ b/src/navigationStore.js @@ -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'; @@ -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)); };