@@ -35,16 +35,34 @@ function filterAndFlattenComponents(components) {
3535 return flattened ;
3636}
3737
38- function asyncConnectPromises ( components , params , store , helpers ) {
39- return components . map ( Component => Component . reduxAsyncConnect ( params , store , helpers ) )
40- . filter ( result => result && result . then instanceof Function ) ;
38+ function loadAsyncConnect ( { components, filter = ( ) => true , ...rest } ) {
39+ let async = false ;
40+ const promise = Promise . all ( filterAndFlattenComponents ( components ) . map ( Component => {
41+ const asyncItems = Component . reduxAsyncConnect ;
42+
43+ return Promise . all ( asyncItems . reduce ( ( itemsResults , item ) => {
44+ if ( filter ( item , Component ) ) {
45+ let promiseOrResult = item . promise ( rest ) ;
46+ if ( promiseOrResult && promiseOrResult . then instanceof Function ) {
47+ async = true ;
48+ promiseOrResult = promiseOrResult . catch ( error => ( { error} ) ) ;
49+ }
50+ return [ ...itemsResults , promiseOrResult ] ;
51+ } else {
52+ return itemsResults ;
53+ }
54+ } , [ ] ) ) . then ( results => {
55+ return asyncItems . reduce ( ( result , item , i ) => ( { ...result , [ item . key ] : results [ i ] } ) , { } ) ;
56+ } ) ;
57+ } ) ) ;
58+
59+ return { promise, async} ;
4160}
4261
43- export function loadOnServer ( { components, params } , store , helpers ) {
44- return Promise . all ( asyncConnectPromises ( filterAndFlattenComponents ( components ) , params , store , helpers ) )
45- . catch ( error => console . error ( 'reduxAsyncConnect server promise error: ' , error ) ) . then ( ( ) => {
46- store . dispatch ( endGlobalLoad ( ) ) ;
47- } ) ;
62+ export function loadOnServer ( args ) {
63+ return loadAsyncConnect ( args ) . promise . then ( ( ) => {
64+ args . store . dispatch ( endGlobalLoad ( ) ) ;
65+ } ) ;
4866}
4967
5068let loadDataCounter = 0 ;
@@ -98,26 +116,24 @@ class ReduxAsyncConnect extends React.Component {
98116 }
99117
100118 loadAsyncData ( props ) {
101- const { components, params, helpers } = props ;
102119 const store = this . context . store ;
103- const promises = asyncConnectPromises ( filterAndFlattenComponents ( components ) , params , store , helpers ) ;
120+ const loadResult = loadAsyncConnect ( { ... props , store} ) ;
104121
105122 loadDataCounter ++ ;
106123
107- if ( promises . length ) {
124+ if ( loadResult . async ) {
108125 this . props . beginGlobalLoad ( ) ;
109126 ( loadDataCounterOriginal => {
110- Promise . all ( promises ) . catch ( error => console . error ( 'reduxAsyncConnect server promise error: ' , error ) )
111- . then ( ( ) => {
112- // We need to change propsToShow only if loadAsyncData that called this promise
113- // is the last invocation of loadAsyncData method. Otherwise we can face situation
114- // when user is changing route several times and we finally show him route that has
115- // loaded props last time and not the last called route
116- if ( loadDataCounter === loadDataCounterOriginal ) {
117- this . setState ( { propsToShow : props } ) ;
118- }
119- this . props . endGlobalLoad ( ) ;
120- } ) ;
127+ loadResult . promise . then ( ( ) => {
128+ // We need to change propsToShow only if loadAsyncData that called this promise
129+ // is the last invocation of loadAsyncData method. Otherwise we can face situation
130+ // when user is changing route several times and we finally show him route that has
131+ // loaded props last time and not the last called route
132+ if ( loadDataCounter === loadDataCounterOriginal ) {
133+ this . setState ( { propsToShow : props } ) ;
134+ }
135+ this . props . endGlobalLoad ( ) ;
136+ } ) ;
121137 } ) ( loadDataCounter ) ;
122138 } else {
123139 this . setState ( { propsToShow : props } ) ;
0 commit comments