Skip to content

Commit

Permalink
Add pushOrPop action - it will pop if scene exists in the stack, push…
Browse files Browse the repository at this point in the history
… otherwise (#947)
  • Loading branch information
aksonov authored Jul 20, 2016
1 parent e811a14 commit 52ac640
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ActionConst.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const JUMP = 'REACT_NATIVE_ROUTER_FLUX_JUMP';
export const PUSH = 'REACT_NATIVE_ROUTER_FLUX_PUSH';
export const PUSH_OR_POP = 'REACT_NATIVE_ROUTER_FLUX_PUSH_OR_POP';
export const REPLACE = 'REACT_NATIVE_ROUTER_FLUX_REPLACE';
export const BACK = 'REACT_NATIVE_ROUTER_FLUX_BACK';
export const BACK_ACTION = 'REACT_NATIVE_ROUTER_FLUX_BACK_ACTION';
Expand Down
2 changes: 2 additions & 0 deletions src/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const ActionMap = {
refresh: ActionConst.REFRESH,
reset: ActionConst.RESET,
focus: ActionConst.FOCUS,
pushOrPop: ActionConst.PUSH_OR_POP,
[ActionConst.JUMP]: ActionConst.JUMP,
[ActionConst.PUSH]: ActionConst.PUSH,
[ActionConst.REPLACE]: ActionConst.REPLACE,
Expand All @@ -29,6 +30,7 @@ export const ActionMap = {
[ActionConst.REFRESH]: ActionConst.REFRESH,
[ActionConst.RESET]: ActionConst.RESET,
[ActionConst.FOCUS]: ActionConst.FOCUS,
[ActionConst.PUSH_OR_POP]: ActionConst.PUSH_OR_POP,
};

function filterParam(data) {
Expand Down
17 changes: 17 additions & 0 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ function inject(state, action, props, scenes) {
key: state.key,
from: null,
};
case ActionConst.PUSH_OR_POP:
ind = state.children.findIndex(el => el.sceneKey === action.key);
if (ind !== -1) {
return {
...state,
index: ind,
from: state.children[state.index],
children: state.children.slice(0, ind + 1),
};
}
return {
...state,
index: state.index + 1,
from: null,
children: [...state.children, getInitialState(props, scenes, state.index + 1, action)],
};
case ActionConst.PUSH:
if (state.children[state.index].sceneKey === action.key && !props.clone
&& checkPropertiesEqual(action, state.children[state.index])) {
Expand Down Expand Up @@ -275,6 +291,7 @@ function reducer({ initialState, scenes }) {
case ActionConst.POP_TO:
case ActionConst.REFRESH:
case ActionConst.PUSH:
case ActionConst.PUSH_OR_POP:
case ActionConst.JUMP:
case ActionConst.REPLACE:
case ActionConst.RESET:
Expand Down

0 comments on commit 52ac640

Please sign in to comment.