Authored by 于良

componentWillUnmount未调用 导致clean操作失败 fix review by 盖剑秋

1 module.exports = { 1 module.exports = {
2 dev: { 2 dev: {
3 - baseUrl: 'http://192.168.102.206:8095/social', 3 + baseUrl: 'http://192.168.102.31:8095/social',
4 HTTPTimeout: 30000, //毫秒 4 HTTPTimeout: 30000, //毫秒
5 5
6 }, 6 },
7 prd: { 7 prd: {
8 - baseUrl: 'http://192.168.102.206:8095/social', 8 + baseUrl: 'http://192.168.102.31:8095/social',
9 HTTPTimeout: 30000, //毫秒 9 HTTPTimeout: 30000, //毫秒
10 10
11 }, 11 },
@@ -296,6 +296,7 @@ export default function community(platform) { @@ -296,6 +296,7 @@ export default function community(platform) {
296 key="SubjectPost" 296 key="SubjectPost"
297 title="主题帖" 297 title="主题帖"
298 hideNavBar={false} 298 hideNavBar={false}
  299 + clone={true}
299 component={SubjectPostContainer} 300 component={SubjectPostContainer}
300 initial={false} 301 initial={false}
301 rightButtonImage={require('./images/posting/share.png')} 302 rightButtonImage={require('./images/posting/share.png')}
@@ -317,9 +318,9 @@ export default function community(platform) { @@ -317,9 +318,9 @@ export default function community(platform) {
317 <Scene 318 <Scene
318 key='LikeList' 319 key='LikeList'
319 hideNavBar={false} 320 hideNavBar={false}
  321 + clone={true}
320 component={LikeListContainer} 322 component={LikeListContainer}
321 initial={false} 323 initial={false}
322 - clone={true}  
323 getSceneStyle={(props) => { 324 getSceneStyle={(props) => {
324 return this.navPushStyle(props); 325 return this.navPushStyle(props);
325 }} 326 }}
@@ -36,27 +36,39 @@ const { @@ -36,27 +36,39 @@ const {
36 } = require('../../constants/actionTypes').default; 36 } = require('../../constants/actionTypes').default;
37 37
38 export function goToSection(section) { 38 export function goToSection(section) {
  39 + return (dispatch, getState) => {
  40 + let {route} = getState();
  41 + let tryToClean = route.lastPopScene == 'Section';
39 Actions.Section(); 42 Actions.Section();
40 - return { 43 + dispatch({
41 type: GO_TO_SECTION, 44 type: GO_TO_SECTION,
42 - payload: section,  
43 - }; 45 + payload: {...section, tryToClean},
  46 + });
  47 + }
44 } 48 }
45 49
46 export function goToPost(id) { 50 export function goToPost(id) {
  51 + return (dispatch, getState) => {
  52 + let {route} = getState();
  53 + let tryToClean = route.lastPopScene == 'SubjectPost';
47 Actions.SubjectPost({postId:id}); 54 Actions.SubjectPost({postId:id});
48 - return { 55 + dispatch({
49 type: GO_TO_POST, 56 type: GO_TO_POST,
50 - payload: id,  
51 - }; 57 + payload: {id, tryToClean},
  58 + });
  59 + }
52 } 60 }
53 61
54 export function goToLikeList(postId, likeCount) { 62 export function goToLikeList(postId, likeCount) {
  63 + return (dispatch, getState) => {
  64 + let {route} = getState();
  65 + let tryToClean = route.lastPopScene == 'LikeList';
55 Actions.LikeList({title: `共${likeCount}个赞`}); 66 Actions.LikeList({title: `共${likeCount}个赞`});
56 - return { 67 + dispatch({
57 type: GO_TO_LIKE_LIST, 68 type: GO_TO_LIKE_LIST,
58 - payload: postId,  
59 - }; 69 + payload: {postId, tryToClean},
  70 + });
  71 + }
60 } 72 }
61 73
62 export function goToUserOrMe(uid) { 74 export function goToUserOrMe(uid) {
@@ -68,11 +80,7 @@ export function goToUserOrMe(uid) { @@ -68,11 +80,7 @@ export function goToUserOrMe(uid) {
68 type: GO_TO_USER, 80 type: GO_TO_USER,
69 }); 81 });
70 } else { 82 } else {
71 - Actions.UserThatNotMe();  
72 - dispatch({  
73 - type: GO_TO_USER_THAT_NOT_ME,  
74 - payload: uid,  
75 - }); 83 + dispatch(goToUserThatNotMe(uid));
76 } 84 }
77 }; 85 };
78 } 86 }
@@ -97,14 +105,15 @@ export function goToUser() { @@ -97,14 +105,15 @@ export function goToUser() {
97 105
98 export function goToUserThatNotMe(uid) { 106 export function goToUserThatNotMe(uid) {
99 return (dispatch, getState) => { 107 return (dispatch, getState) => {
100 - let {user} = getState(); 108 + let {user, route} = getState();
101 if (user.profile.uid == uid) { 109 if (user.profile.uid == uid) {
102 110
103 } else { 111 } else {
  112 + let tryToClean = route.lastPopScene == 'UserThatNotMe';
104 Actions.UserThatNotMe(); 113 Actions.UserThatNotMe();
105 dispatch({ 114 dispatch({
106 type: GO_TO_USER_THAT_NOT_ME, 115 type: GO_TO_USER_THAT_NOT_ME,
107 - payload: uid, 116 + payload: {uid, tryToClean},
108 }); 117 });
109 } 118 }
110 }; 119 };
@@ -17,6 +17,7 @@ let InitialState = Record({ @@ -17,6 +17,7 @@ let InitialState = Record({
17 sid: -1, 17 sid: -1,
18 items: List(), 18 items: List(),
19 item: item, 19 item: item,
  20 + cleaned: false,
20 }); 21 });
21 22
22 export default InitialState; 23 export default InitialState;
@@ -24,15 +24,26 @@ export default function likeList(state = initialState, action) { @@ -24,15 +24,26 @@ export default function likeList(state = initialState, action) {
24 24
25 switch (action.type) { 25 switch (action.type) {
26 case GO_TO_LIKE_LIST: { 26 case GO_TO_LIKE_LIST: {
27 - let sid = state.sid + 1; 27 +
  28 + let currentSid = state.sid;
  29 + let newItems = state.items;
  30 + let newSid = currentSid;
  31 + // componentWillUnmount未调用,导致clean操作未进行
  32 + if (action.payload.tryToClean && currentSid > -1 && !state.cleaned) {
  33 + newItems = newItems.pop();
  34 + newSid = newSid - 1;
  35 + }
  36 +
  37 + let sid = newSid + 1;
28 // 获取新的初始状态item 38 // 获取新的初始状态item
29 let item = initialState.item; 39 let item = initialState.item;
30 // 设置item的value 40 // 设置item的value
31 - item = item.set('postId', action.payload); 41 + item = item.set('postId', action.payload.postId);
32 // items新增item 42 // items新增item
33 - let newItems = state.items.push(item); 43 + newItems = newItems.push(item);
34 let nextState = state.set('sid', sid) 44 let nextState = state.set('sid', sid)
35 - .set('items', newItems); 45 + .set('items', newItems)
  46 + .set('cleaned', false);
36 return nextState; 47 return nextState;
37 } 48 }
38 49
@@ -69,7 +80,8 @@ export default function likeList(state = initialState, action) { @@ -69,7 +80,8 @@ export default function likeList(state = initialState, action) {
69 if (state.items.size > sid) { 80 if (state.items.size > sid) {
70 let items = state.items.pop(); 81 let items = state.items.pop();
71 let nextState = state.set('sid', sid - 1) 82 let nextState = state.set('sid', sid - 1)
72 - .set('items', items); 83 + .set('items', items)
  84 + .set('cleaned', true);
73 return nextState; 85 return nextState;
74 } 86 }
75 } 87 }
@@ -2,6 +2,7 @@ import { ActionConst } from 'react-native-router-flux'; @@ -2,6 +2,7 @@ import { ActionConst } from 'react-native-router-flux';
2 2
3 const initialState = { 3 const initialState = {
4 scene: {}, 4 scene: {},
  5 + lastPopScene: '',
5 }; 6 };
6 7
7 export default function reducer(state = initialState, action = {}) { 8 export default function reducer(state = initialState, action = {}) {
@@ -13,6 +14,12 @@ export default function reducer(state = initialState, action = {}) { @@ -13,6 +14,12 @@ export default function reducer(state = initialState, action = {}) {
13 scene: action.scene, 14 scene: action.scene,
14 }; 15 };
15 16
  17 + case ActionConst.BACK_ACTION:
  18 + return {
  19 + ...state,
  20 + lastPopScene: state.scene.sceneKey,
  21 + };
  22 +
16 // ...other actions 23 // ...other actions
17 24
18 default: 25 default:
@@ -54,6 +54,7 @@ let InitialState = Record({ @@ -54,6 +54,7 @@ let InitialState = Record({
54 sid: -1, 54 sid: -1,
55 items: List(), 55 items: List(),
56 item: item, 56 item: item,
  57 + cleaned: false,
57 }); 58 });
58 59
59 export default InitialState; 60 export default InitialState;
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 * InitialState 10 * InitialState
11 */ 11 */
12 import InitialState from './sectionInitialState'; 12 import InitialState from './sectionInitialState';
13 -import Immutable, {Map} from 'immutable'; 13 +import Immutable, {Map, List} from 'immutable';
14 14
15 const { 15 const {
16 SECTION_HEADER_REQUEST, 16 SECTION_HEADER_REQUEST,
@@ -46,16 +46,27 @@ export default function sectionReducer(state = initialState, action) { @@ -46,16 +46,27 @@ export default function sectionReducer(state = initialState, action) {
46 switch (action.type) { 46 switch (action.type) {
47 47
48 case GO_TO_SECTION: { 48 case GO_TO_SECTION: {
49 - let sid = state.sid + 1; 49 +
  50 + let currentSid = state.sid;
  51 + let newItems = state.items;
  52 + let newSid = currentSid;
  53 + // componentWillUnmount未调用,导致clean操作未进行
  54 + if (action.payload.tryToClean && currentSid > -1 && !state.cleaned) {
  55 + newItems = newItems.pop();
  56 + newSid = newSid - 1;
  57 + }
  58 +
  59 + let sid = newSid + 1;
50 // 获取新的初始状态item 60 // 获取新的初始状态item
51 let item = initialState.item; 61 let item = initialState.item;
52 // 设置item的value 62 // 设置item的value
53 item = item.set('id', action.payload.id) 63 item = item.set('id', action.payload.id)
54 .set('name', action.payload.name); 64 .set('name', action.payload.name);
55 // items新增item 65 // items新增item
56 - let newItems = state.items.push(item); 66 + let items = newItems.push(item);
57 let nextState = state.set('sid', sid) 67 let nextState = state.set('sid', sid)
58 - .set('items', newItems); 68 + .set('items', items)
  69 + .set('cleaned', false);
59 return nextState; 70 return nextState;
60 } 71 }
61 72
@@ -172,7 +183,8 @@ export default function sectionReducer(state = initialState, action) { @@ -172,7 +183,8 @@ export default function sectionReducer(state = initialState, action) {
172 if (state.items.size > sid) { 183 if (state.items.size > sid) {
173 let items = state.items.pop(); 184 let items = state.items.pop();
174 let nextState = state.set('sid', sid - 1) 185 let nextState = state.set('sid', sid - 1)
175 - .set('items', items); 186 + .set('items', items)
  187 + .set('cleaned', true);
176 return nextState; 188 return nextState;
177 } 189 }
178 } 190 }
@@ -68,6 +68,7 @@ let InitialState = Record({ @@ -68,6 +68,7 @@ let InitialState = Record({
68 sid: -1, 68 sid: -1,
69 items: List(), 69 items: List(),
70 item: item, 70 item: item,
  71 + cleaned: false,
71 }); 72 });
72 73
73 export default InitialState; 74 export default InitialState;
@@ -56,17 +56,27 @@ export default function postingReducer(state = initialState, action) { @@ -56,17 +56,27 @@ export default function postingReducer(state = initialState, action) {
56 56
57 switch (action.type) { 57 switch (action.type) {
58 case GO_TO_POST: { 58 case GO_TO_POST: {
59 - let sid = state.sid + 1; 59 +
  60 + let currentSid = state.sid;
  61 + let newItems = state.items;
  62 + let newSid = currentSid;
  63 + // componentWillUnmount未调用,导致clean操作未进行
  64 + if (action.payload.tryToClean && currentSid > -1 && !state.cleaned) {
  65 + newItems = newItems.pop();
  66 + newSid = newSid - 1;
  67 + }
  68 +
  69 + let sid = newSid + 1;
60 // 获取新的初始状态item 70 // 获取新的初始状态item
61 let item = initialState.item; 71 let item = initialState.item;
62 // 设置item的value 72 // 设置item的value
63 - item = item.set('id', action.payload); 73 + item = item.set('id', action.payload.id);
64 // items新增item 74 // items新增item
65 - let newItems = state.items.push(item); 75 + newItems = newItems.push(item);
66 let nextState = state.set('sid', sid) 76 let nextState = state.set('sid', sid)
67 - .set('items', newItems); 77 + .set('items', newItems)
  78 + .set('cleaned', false);
68 return nextState; 79 return nextState;
69 - // return state.set('id', action.payload);  
70 } 80 }
71 81
72 case SUBJECT_CONTENT_REQUEST: { 82 case SUBJECT_CONTENT_REQUEST: {
@@ -287,7 +297,8 @@ export default function postingReducer(state = initialState, action) { @@ -287,7 +297,8 @@ export default function postingReducer(state = initialState, action) {
287 if (state.items.size > sid) { 297 if (state.items.size > sid) {
288 let items = state.items.pop(); 298 let items = state.items.pop();
289 let nextState = state.set('sid', sid - 1) 299 let nextState = state.set('sid', sid - 1)
290 - .set('items', items); 300 + .set('items', items)
  301 + .set('cleaned', true);
291 return nextState; 302 return nextState;
292 } 303 }
293 } 304 }
@@ -32,6 +32,7 @@ let InitialState = Record({ @@ -32,6 +32,7 @@ let InitialState = Record({
32 sid: -1, 32 sid: -1,
33 items: List(), 33 items: List(),
34 item: item, 34 item: item,
  35 + cleaned: false,
35 }); 36 });
36 37
37 export default InitialState; 38 export default InitialState;
@@ -28,15 +28,26 @@ export default function userThatNotMe(state = initialState, action) { @@ -28,15 +28,26 @@ export default function userThatNotMe(state = initialState, action) {
28 28
29 switch (action.type) { 29 switch (action.type) {
30 case GO_TO_USER_THAT_NOT_ME: { 30 case GO_TO_USER_THAT_NOT_ME: {
31 - let sid = state.sid + 1; 31 +
  32 + let currentSid = state.sid;
  33 + let newItems = state.items;
  34 + let newSid = currentSid;
  35 + // componentWillUnmount未调用,导致clean操作未进行
  36 + if (action.payload.tryToClean && currentSid > -1 && !state.cleaned) {
  37 + newItems = newItems.pop();
  38 + newSid = newSid - 1;
  39 + }
  40 +
  41 + let sid = newSid + 1;
32 // 获取新的初始状态item 42 // 获取新的初始状态item
33 let item = initialState.item; 43 let item = initialState.item;
34 // 设置item的value 44 // 设置item的value
35 - item = item.setIn(['profile', 'uid'], action.payload); 45 + item = item.setIn(['profile', 'uid'], action.payload.uid);
36 // items新增item 46 // items新增item
37 - let newItems = state.items.push(item); 47 + newItems = newItems.push(item);
38 let nextState = state.set('sid', sid) 48 let nextState = state.set('sid', sid)
39 - .set('items', newItems); 49 + .set('items', newItems)
  50 + .set('cleaned', false);
40 return nextState; 51 return nextState;
41 } 52 }
42 53
@@ -106,7 +117,8 @@ export default function userThatNotMe(state = initialState, action) { @@ -106,7 +117,8 @@ export default function userThatNotMe(state = initialState, action) {
106 if (state.items.size > sid) { 117 if (state.items.size > sid) {
107 let items = state.items.pop(); 118 let items = state.items.pop();
108 let nextState = state.set('sid', sid - 1) 119 let nextState = state.set('sid', sid - 1)
109 - .set('items', items); 120 + .set('items', items)
  121 + .set('cleaned', true);
110 return nextState; 122 return nextState;
111 } 123 }
112 } 124 }