-
Notifications
You must be signed in to change notification settings - Fork 53
/
NavigationStackReducer.js
110 lines (94 loc) · 3.28 KB
/
NavigationStackReducer.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule NavigationStackReducer
* @flow
*/
'use strict';
var NavigationStateUtils = require('./NavigationStateUtils');
import type {
NavigationState,
NavigationParentState,
NavigationReducer,
} from 'NavigationTypeDefinition';
import type {
BackAction,
} from 'NavigationRootContainer';
export type NavigationStackReducerAction = BackAction | {
type: string,
};
export type ReducerForStateHandler = (state: NavigationState) => NavigationReducer;
export type PushedReducerForActionHandler = (action: any, lastState: NavigationParentState) => ?NavigationReducer;
export type StackReducerConfig = {
/*
* The initialState is that the reducer will use when there is no previous state.
* Must be a NavigationParentState:
*
* {
* children: [
* {key: 'subState0'},
* {key: 'subState1'},
* ],
* index: 0,
* key: 'navStackKey'
* }
*/
initialState: NavigationParentState;
/*
* Returns the sub-reducer for a particular state to handle. This will be called
* when we need to handle an action on a sub-state. If no reducer is returned,
* no action will be taken
*/
getReducerForState?: ReducerForStateHandler;
/*
* Returns a sub-reducer that will be used when pushing a new route. If a reducer
* is returned, it be called to get the new state that will be pushed
*/
getPushedReducerForAction: PushedReducerForActionHandler;
};
const defaultGetReducerForState = (initialState) => (state) => state || initialState;
function NavigationStackReducer({initialState, getReducerForState, getPushedReducerForAction}: StackReducerConfig): NavigationReducer {
const getReducerForStateWithDefault = getReducerForState || defaultGetReducerForState;
return function (lastState: ?NavigationState, action: any): NavigationState {
if (!lastState) {
return initialState;
}
const lastParentState = NavigationStateUtils.getParent(lastState);
if (!lastParentState) {
return lastState;
}
const activeSubState = lastParentState.children[lastParentState.index];
const activeSubReducer = getReducerForStateWithDefault(activeSubState);
const nextActiveState = activeSubReducer(activeSubState, action);
if (nextActiveState !== activeSubState) {
const nextChildren = [...lastParentState.children];
nextChildren[lastParentState.index] = nextActiveState;
return {
...lastParentState,
children: nextChildren,
};
}
const subReducerToPush = getPushedReducerForAction(action, lastParentState);
if (subReducerToPush) {
return NavigationStateUtils.push(
lastParentState,
subReducerToPush(null, action)
);
}
switch (action.type) {
case 'back':
case 'BackAction':
if (lastParentState.index === 0 || lastParentState.children.length === 1) {
return lastParentState;
}
return NavigationStateUtils.pop(lastParentState);
}
return lastParentState;
};
}
module.exports = NavigationStackReducer;