Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Commit bf9afa5

Browse files
committed
fix: dont dispatch resolved promise action if view has changed
Fix #5
1 parent 7eff738 commit bf9afa5

File tree

10 files changed

+233
-123
lines changed

10 files changed

+233
-123
lines changed

bin/main.js

Lines changed: 182 additions & 113 deletions
Large diffs are not rendered by default.

src/components/ProfileFavorites.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ const store = require('../store');
99
class ProfileFavorites extends Profile {
1010
componentWillMount() {
1111
store.dispatch({
12-
type: 'PROFILE_PAGE_LOADED',
12+
type: 'PROFILE_FAVORITES_PAGE_LOADED',
1313
payload: Promise.all([
1414
agent.Profile.get(this.props.params.username),
1515
agent.Articles.favoritedBy(this.props.params.username)
1616
])
1717
});
1818
}
1919

20+
componentWillUnmount() {
21+
store.dispatch({ type: 'PROFILE_FAVORITES_PAGE_UNLOADED' });
22+
}
23+
2024
renderTabs() {
2125
return (
2226
<ul className="nav nav-pills outline-active">

src/middleware.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@ const agent = require('./agent');
55
exports.promiseMiddleware = store => next => action => {
66
if (isPromise(action.payload)) {
77
store.dispatch({ type: 'ASYNC_START', subtype: action.type });
8+
const initialView = store.getState().viewChangeCounter;
89
action.payload.then(
910
res => {
11+
// The view might have changed mid-promise, so if the view unloaded,
12+
// don't dispatch the action.
13+
const finalView = store.getState().viewChangeCounter;
14+
if (finalView !== initialView) {
15+
return;
16+
}
1017
console.log('RESULT', res);
1118
action.payload = res;
1219
store.dispatch(action);
1320
},
1421
error => {
22+
const finalView = store.getState().viewChangeCounter;
23+
if (finalView !== initialView) {
24+
return;
25+
}
1526
console.log('ERROR', error);
1627
action.error = true;
1728
action.payload = error.response.body;
1829
store.dispatch(action);
1930
}
2031
);
2132

22-
store.dispatch({ type: 'LOADING' });
23-
2433
return;
2534
}
2635

src/reducer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const auth = require('./reducers/auth');
55
const editor = require('./reducers/editor');
66
const home = require('./reducers/home');
77
const profile = require('./reducers/profile');
8+
const profileFavorites = require('./reducers/profileFavorites');
89
const settings = require('./reducers/settings');
910

1011
const defaultState = {
1112
appName: 'Conduit',
12-
token: null
13+
token: null,
14+
viewChangeCounter: 0
1315
};
1416

1517
module.exports = (state = defaultState, action) => {
@@ -18,6 +20,7 @@ module.exports = (state = defaultState, action) => {
1820
state = editor(state, action);
1921
state = home(state, action);
2022
state = profile(state, action);
23+
state = profileFavorites(state, action);
2124
state = settings(state, action);
2225
switch (action.type) {
2326
case 'APP_LOAD':
@@ -46,7 +49,9 @@ module.exports = (state = defaultState, action) => {
4649
state = Object.assign({}, state, { redirectTo: '/' });
4750
break;
4851
case 'ARTICLE_PAGE_UNLOADED':
49-
state = Object.assign({}, state);
52+
state = Object.assign({}, state, {
53+
viewChangeCounter: state.viewChangeCounter + 1
54+
});
5055
delete state.article;
5156
delete state.comments;
5257
delete state.commentErrors;
@@ -79,5 +84,6 @@ module.exports = (state = defaultState, action) => {
7984
break;
8085
}
8186

87+
console.log(state);
8288
return state;
8389
};

src/reducers/auth.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ module.exports = (state, action) => {
1616
break;
1717
case 'LOGIN_PAGE_UNLOADED':
1818
case 'REGISTER_PAGE_UNLOADED':
19-
state = Object.assign({}, state);
19+
state = Object.assign({}, state, {
20+
viewChangeCounter: state.viewChangeCounter + 1
21+
});
2022
const props = ['errors', 'username', 'email', 'password', 'inProgress']
2123
for (const key of props) {
2224
delete state[key];

src/reducers/editor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ module.exports = (state, action) => {
2323
}
2424
break;
2525
case 'EDITOR_PAGE_UNLOADED':
26-
state = Object.assign({}, state);
26+
state = Object.assign({}, state, {
27+
viewChangeCounter: state.viewChangeCounter + 1
28+
});
2729
const keys = [
2830
'title',
2931
'description',

src/reducers/home.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module.exports = (state, action) => {
1212
});
1313
break;
1414
case 'HOME_PAGE_UNLOADED':
15-
state = Object.assign({}, state);
15+
state = Object.assign({}, state, {
16+
viewChangeCounter: state.viewChangeCounter + 1
17+
});
1618
delete state.articles;
1719
delete state.tags;
1820
delete state.tab;

src/reducers/profile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module.exports = (state = defaultState, action) => {
1111
});
1212
break;
1313
case 'PROFILE_PAGE_UNLOADED':
14-
state = Object.assign({}, state);
14+
state = Object.assign({}, state, {
15+
viewChangeCounter: state.viewChangeCounter + 1
16+
});
1517
delete state.profile;
1618
delete state.articles;
1719
delete state.articlesCount;

src/reducers/profileFavorites.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = (state = defaultState, action) => {
4+
switch (action.type) {
5+
case 'PROFILE_FAVORITES_PAGE_UNLOADED':
6+
return Object.assign({}, state, {
7+
viewChangeCounter: state.viewChangeCounter + 1
8+
});
9+
}
10+
11+
return state;
12+
};

src/reducers/settings.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ module.exports = (state, action) => {
1313
}
1414
break;
1515
case 'SETTINGS_PAGE_UNLOADED':
16-
state = Object.assign({}, state);
16+
state = Object.assign({}, state, {
17+
viewChangeCounter: state.viewChangeCounter + 1
18+
});
1719
for (const key of ['errors', 'inProgress']) {
1820
delete state[key];
1921
}

0 commit comments

Comments
 (0)