From 74e54f7b8f36e62f3550007f79a4bc7ed39dced5 Mon Sep 17 00:00:00 2001 From: Pavlo Aksonov Date: Wed, 20 Jul 2016 17:47:56 +0200 Subject: [PATCH] Add pushOrPop action - it will pop if scene exists in the stack, push otherwise --- src/ActionConst.js | 1 + src/Actions.js | 2 ++ src/Reducer.js | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/ActionConst.js b/src/ActionConst.js index de7c4ad8d..5f145359d 100644 --- a/src/ActionConst.js +++ b/src/ActionConst.js @@ -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'; diff --git a/src/Actions.js b/src/Actions.js index 81654cca8..7003b4015 100644 --- a/src/Actions.js +++ b/src/Actions.js @@ -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, @@ -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) { diff --git a/src/Reducer.js b/src/Reducer.js index 0a22e2135..971cf8dba 100644 --- a/src/Reducer.js +++ b/src/Reducer.js @@ -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])) { @@ -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: