-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathReduxAsyncConnect.js
162 lines (135 loc) · 4.42 KB
/
ReduxAsyncConnect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React from 'react';
import RouterContext from 'react-router/lib/RouterContext';
import { beginGlobalLoad, endGlobalLoad } from './asyncConnect';
import { connect } from 'react-redux';
const { array, func, object, any } = React.PropTypes;
/**
* We need to iterate over all components for specified routes.
* Components array can include objects if named components are used:
* https://github.com/rackt/react-router/blob/latest/docs/API.md#named-components
*
* @param components
* @param iterator
*/
function eachComponents(components, iterator) {
for (let i = 0, l = components.length; i < l; i++) { // eslint-disable-line id-length
if (typeof components[i] === 'object') {
for (let [key, value] of Object.entries(components[i])) {
iterator(value, i, key);
}
} else {
iterator(components[i], i);
}
}
}
function filterAndFlattenComponents(components) {
const flattened = [];
eachComponents(components, (Component) => {
if (Component && Component.reduxAsyncConnect) {
flattened.push(Component);
}
});
return flattened;
}
function loadAsyncConnect({components, filter = () => true, ...rest}) {
let async = false;
const promise = Promise.all(filterAndFlattenComponents(components).map(Component => {
const asyncItems = Component.reduxAsyncConnect;
return Promise.all(asyncItems.reduce((itemsResults, item) => {
let promiseOrResult = item.promise(rest);
if (filter(item, Component)) {
if (promiseOrResult && promiseOrResult.then instanceof Function) {
async = true;
promiseOrResult = promiseOrResult.catch(error => ({error}));
}
return [...itemsResults, promiseOrResult];
} else {
return itemsResults;
}
}, [])).then(results => {
return asyncItems.reduce((result, item, i) => ({...result, [item.key]: results[i]}), {});
});
}));
return {promise, async};
}
export function loadOnServer(args) {
const result = loadAsyncConnect(args);
const {store: {dispatch}} = args
// Some of the routes might have no async components.
// For such cases signal that global loading has been completed
if (!result.async) {
dispatch(endGlobalLoad());
return result.promise;
}
result.promise.then(() => {
dispatch(endGlobalLoad());
});
return result.promise;
}
let loadDataCounter = 0;
class ReduxAsyncConnect extends React.Component {
static propTypes = {
components: array.isRequired,
params: object.isRequired,
render: func.isRequired,
beginGlobalLoad: func.isRequired,
endGlobalLoad: func.isRequired,
helpers: any
};
static contextTypes = {
store: object.isRequired
};
static defaultProps = {
render(props) {
return <RouterContext {...props} />;
}
};
isLoaded() {
return this.context.store.getState().reduxAsyncConnect.loaded;
}
constructor(props, context) {
super(props, context);
this.state = {
propsToShow: this.isLoaded() ? props : null
};
}
componentDidMount() {
const dataLoaded = this.isLoaded();
if (!dataLoaded) { // we dont need it if we already made it on server-side
this.loadAsyncData(this.props);
}
}
componentWillReceiveProps(nextProps) {
this.loadAsyncData(nextProps);
}
shouldComponentUpdate(nextProps, nextState) {
return this.state.propsToShow !== nextState.propsToShow;
}
loadAsyncData(props) {
const store = this.context.store;
const loadResult = loadAsyncConnect({...props, store});
loadDataCounter++;
if (loadResult.async) {
this.props.beginGlobalLoad();
(loadDataCounterOriginal => {
loadResult.promise.then(() => {
// We need to change propsToShow only if loadAsyncData that called this promise
// is the last invocation of loadAsyncData method. Otherwise we can face 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 (loadDataCounter === loadDataCounterOriginal) {
this.setState({propsToShow: props});
}
this.props.endGlobalLoad();
});
})(loadDataCounter);
} else {
this.setState({propsToShow: props});
}
}
render() {
const {propsToShow} = this.state;
return propsToShow && this.props.render(propsToShow);
}
}
export default connect(null, {beginGlobalLoad, endGlobalLoad})(ReduxAsyncConnect);