Skip to content

Commit

Permalink
Merge pull request #429 from joenoon/jn-refresh3
Browse files Browse the repository at this point in the history
make refresh by key modify the child data and not scene data.
  • Loading branch information
aksonov committed Mar 29, 2016
2 parents 3b03ce9 + 265654c commit f4236b3
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ function inject(state, action, props, scenes) {
case POP_ACTION:
return {...state, index:state.index-1, children:state.children.slice(0, -1) };
case REFRESH_ACTION:
// use key and parent from state to avoid loss during refresh
let {key, parent} = state;
return {...state, ...props, key, parent};
return {...state, ...props};
case PUSH_ACTION:
if (state.children[state.index].sceneKey == action.key && !props.clone){
return state;
Expand Down Expand Up @@ -65,24 +63,16 @@ function inject(state, action, props, scenes) {
}


function findElement(state, key) {
if (state.sceneKey != key){
if (state.children){
let result = undefined;
state.children.forEach(el=>{
let res = findElement(el, key);
if (res){
result = res;
return res;
}
});
return result;
} else {
return false;
}
} else {
function findElement(state, key, type) {
if (type === REFRESH_ACTION ? state.key === key : state.sceneKey === key) {
return state;
}
if (state.children) {
for (let child of state.children) {
let current = findElement(child, key, type);
if (current) return current;
}
}
}

function getCurrent(state){
Expand All @@ -101,8 +91,6 @@ function update(state,action){
return inject(state, action, props, state.scenes);
}

var _uniqPush = 0;

function reducer({initialState, scenes}){
assert(initialState, "initialState should not be null");
assert(initialState.key, "initialState.key should not be null");
Expand All @@ -114,14 +102,19 @@ function reducer({initialState, scenes}){
assert(state.scenes, "state.scenes is missed");

if (action.key){
let scene = state.scenes[action.key];
assert(scene, "missed route data for key="+action.key);

// clone scene
if (scene.clone) {
action.parent = getCurrent(state).parent;
if (action.type === REFRESH_ACTION) {
let key = action.key;
let child = findElement(state, key, action.type);
assert(child, "missed child data for key="+key);
action = {...child,...action};
} else {
let scene = state.scenes[action.key];
assert(scene, "missed route data for key="+action.key);
// clone scene
if (scene.clone) {
action.parent = getCurrent(state).parent;
}
}

} else {
// set current route for pop action or refresh action
if (action.type === POP_ACTION || action.type === POP_ACTION2 || action.type === REFRESH_ACTION){
Expand All @@ -132,9 +125,9 @@ function reducer({initialState, scenes}){
// recursive pop parent
if (action.type === POP_ACTION || action.type === POP_ACTION2) {
let parent = action.parent || state.scenes[action.key].parent;
let el = findElement(state, parent);
let el = findElement(state, parent, action.type);
while (el.parent && (el.children.length <= 1 || el.tabs)) {
el = findElement(state, el.parent);
el = findElement(state, el.parent, action.type);
assert(el, "Cannot find element for parent=" + el.parent + " within current state");
}
action.parent = el.sceneKey;
Expand Down

0 comments on commit f4236b3

Please sign in to comment.