-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathReducer.js
43 lines (42 loc) · 1.51 KB
/
Reducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import _ from 'lodash';
import { NavigationActions, StackActions } from 'react-navigation';
import * as ActionConst from './ActionConst';
import { getActiveState, popPrevious } from './State';
export default function createReducer(store) {
return (state, action) => {
const NavigationStore = require('./Store').default;
const navigationStore = store || new NavigationStore();
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 (nextScene !== routeName && _.isEqual(currentState, newState)) {
console.warn(`popTo called with an unknown routeName: ${routeName}, current scene: ${nextScene}`);
break;
}
if (nextScene !== routeName) {
currentState = newState;
}
}
}
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;
};
}