Skip to content

Commit

Permalink
Fetching on client-side when deferred
Browse files Browse the repository at this point in the history
  • Loading branch information
shilpan committed Jun 16, 2016
1 parent e909312 commit ac2fe26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
36 changes: 20 additions & 16 deletions modules/components/AsyncConnect.js
Expand Up @@ -63,22 +63,26 @@ export default class AsyncConnect extends Component {
const store = this.context.store;
const loadResult = loadAsyncConnect({ ...props, store });

// TODO: think of a better solution to a problem?
this.loadDataCounter++;
this.props.beginGlobalLoad();
return (loadDataCounterOriginal => loadResult.then(() => {
// We need to change propsToShow only if loadAsyncData that called this promise
// is the last invocation of loadAsyncData method. Otherwise we can face a situation
// when user is changing route several times and we finally show him route that has
// loaded props last time and not the last called route
if (this.loadDataCounter === loadDataCounterOriginal && this.mounted !== false) {
this.setState({ propsToShow: props });
}

// TODO: investigate race conditions
// do we need to call this if it's not last invocation?
this.props.endGlobalLoad();
}))(this.loadDataCounter);
if (loadResult) {
// TODO: think of a better solution to a problem?
this.loadDataCounter++;
this.props.beginGlobalLoad();
return (loadDataCounterOriginal => loadResult.then(() => {
// We need to change propsToShow only if loadAsyncData that called this promise
// is the last invocation of loadAsyncData method. Otherwise we can face a situation
// when user is changing route several times and we finally show him route that has
// loaded props last time and not the last called route
if (this.loadDataCounter === loadDataCounterOriginal && this.mounted !== false) {
this.setState({ propsToShow: props });
}

// TODO: investigate race conditions
// do we need to call this if it's not last invocation?
this.props.endGlobalLoad();
}))(this.loadDataCounter);
} else {
this.setState({propsToShow: props});
}
}

render() {
Expand Down
4 changes: 2 additions & 2 deletions modules/helpers/utils.js
Expand Up @@ -103,15 +103,15 @@ export function loadAsyncConnect({ components, filter = () => true, ...rest }) {
return itemsResults;
}, []);

return Promise.all(results)
return results.length ? Promise.all(results)
.then(finalResults => finalResults.reduce((finalResult, result, idx) => {
const { key } = asyncItems[idx];
if (key) {
finalResult[key] = result;
}

return finalResult;
}, {}));
}, {})) : null;
});
}

Expand Down

0 comments on commit ac2fe26

Please sign in to comment.