@@ -77,97 +77,97 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
77
77
const {
78
78
name = '' ,
79
79
load,
80
- getInStore,
81
80
loadOnlyOnce,
81
+ getInStore,
82
82
getInitialState,
83
83
update,
84
84
partialReducer,
85
85
} = config ;
86
86
generatedCount += 1 ;
87
87
88
88
const safeDataName = `${ name } /${ generatedCount } ` ;
89
- const Getters = new function ( ) {
90
- const wrappedGetInitialState : ( ) => BoilerState < T > = createGetInitialState ( getInitialState ) ;
91
- this . getInitialState = wrappedGetInitialState ;
89
+ const wrappedGetInitialState : ( ) = > BoilerState < T > = createGetInitialState ( getInitialState ) ;
90
+ class Getters {
91
+ static getInitialState = wrappedGetInitialState ;
92
92
93
- this . get = getInStore ||
93
+ static get = getInStore ||
94
94
( ( reduxStore : Object ) : BoilerState < T > =>
95
- reduxStore [ prefix ] [ safeDataName ] || this . getInitialState ( ) ) ;
95
+ reduxStore [ prefix ] [ safeDataName ] || Getters . getInitialState ( ) ) ;
96
96
97
- this . getData = ( reduxStore : Object ) : $PropertyType < BoilerState < T > , 'data' > => {
98
- const state = this . get ( reduxStore ) ;
97
+ static getData = ( reduxStore : Object ) : $PropertyType < BoilerState < T > , 'data' > => {
98
+ const state = Getters . get ( reduxStore ) ;
99
99
if ( ! state . hasOwnProperty ( 'data' ) ) {
100
100
warning ( `redux-rags: getData failed to find the property \'data\' on the object returned by Getters.get.
101
101
This is likely caused by providing an incorrect 'getInStore' configuration option.` ) ;
102
102
}
103
103
return state . data ;
104
104
} ;
105
105
106
- this . getMeta = ( reduxStore : Object ) : $PropertyType < BoilerState < T > , 'meta' > => {
107
- const state = this . get ( reduxStore ) ;
106
+ static getMeta = ( reduxStore : Object ) : $PropertyType < BoilerState < T > , 'meta' > => {
107
+ const state = Getters . get ( reduxStore ) ;
108
108
if ( ! state . hasOwnProperty ( 'meta' ) ) {
109
109
warning ( `redux-rags: getData failed to find the property \'meta\' on the object returned by Getters.get.
110
110
This is likely caused by providing an incorrect 'getInStore' configuration option.` ) ;
111
111
}
112
112
return state . meta ;
113
113
} ;
114
114
115
- this . getIsLoading = ( reduxStore : Object ) => {
116
- const meta = this . getMeta ( reduxStore ) ;
115
+ static getIsLoading = ( reduxStore : Object ) => {
116
+ const meta = Getters . getMeta ( reduxStore ) ;
117
117
return meta && meta . loading ;
118
118
} ;
119
- } ;
119
+ }
120
120
121
121
const BEGIN_LOADING = getLoadingType ( name ) ;
122
122
const END_LOADING = getEndLoadingType ( name ) ;
123
123
const ERRORS = getErrorsType ( name ) ;
124
124
const UPDATE_DATA = getUpdateType ( name ) ;
125
125
const RESET = getResetType ( name ) ;
126
126
127
- const Actions = new function ( ) {
128
- this . beginLoading = ( ) => ( {
127
+ class Actions {
128
+ static beginLoading = ( ) = > ( {
129
129
type : BEGIN_LOADING ,
130
130
payload : null ,
131
131
} ) ;
132
132
133
- this . endLoading = ( ) => ( {
133
+ static endLoading = ( ) = > ( {
134
134
type : END_LOADING ,
135
135
payload : null ,
136
136
} ) ;
137
137
138
- this . reset = ( ) => ( {
138
+ static reset = ( ) = > ( {
139
139
type : RESET ,
140
140
payload : null ,
141
141
} ) ;
142
142
143
- this . errors = ( errors : Object | String ) => ( {
143
+ static errors = ( errors : Object | String ) = > ( {
144
144
type : ERRORS ,
145
145
payload : errors ,
146
146
} ) ;
147
147
148
- this . clearErrors = ( ) => ( {
148
+ static clearErrors = ( ) = > ( {
149
149
type : ERRORS ,
150
150
payload : null ,
151
151
} ) ;
152
152
153
- this . updateData = ( data : ?T ) => ( {
153
+ static updateData = ( data : ?T ) = > ( {
154
154
type : UPDATE_DATA ,
155
155
payload : data ,
156
156
} ) ;
157
157
158
- this . update = ( ...args : * ) => async ( dispatch , getState : ( ) = > Object ) = > {
158
+ static update = ( ...args : * ) = > async ( dispatch , getState : ( ) = > Object ) = > {
159
159
if ( ! update || typeof update !== 'function' ) {
160
160
return ;
161
161
}
162
162
try {
163
163
const manipulated = await Promise . resolve ( update ( Getters . getData ( getState ( ) ) , ...args ) ) ;
164
- dispatch ( this . updateData ( manipulated ) ) ;
164
+ dispatch ( Actions . updateData ( manipulated ) ) ;
165
165
} catch ( err ) {
166
- dispatch ( this . errors ( err ) ) ;
166
+ dispatch ( Actions . errors ( err ) ) ;
167
167
}
168
168
} ;
169
169
170
- this . load = ( ...args : G ) => async (
170
+ static load = ( ...args : G ) => async (
171
171
dispatch : Dispatch ,
172
172
getState : ( ) = > Object
173
173
) : Promise < ?T > => {
@@ -180,18 +180,18 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
180
180
return state . data ;
181
181
}
182
182
}
183
- dispatch ( this . beginLoading ( ) ) ;
183
+ dispatch ( Actions . beginLoading ( ) ) ;
184
184
try {
185
185
const data = await Promise . resolve ( load ( ...args ) ) ;
186
- dispatch ( this . updateData ( data ) ) ;
186
+ dispatch ( Actions . updateData ( data ) ) ;
187
187
return data ;
188
188
} catch ( err ) {
189
- dispatch ( this . errors ( err ) ) ;
190
- dispatch ( this . endLoading ( ) ) ;
189
+ dispatch ( Actions . errors ( err ) ) ;
190
+ dispatch ( Actions . endLoading ( ) ) ;
191
191
return null ;
192
192
}
193
193
} ;
194
- } ;
194
+ }
195
195
196
196
type Interpret = < R > ((...Iterable< any > ) => R ) => R ;
197
197
type ExtractReturn < Fn > = $Call< Interpret , Fn > ;
@@ -202,10 +202,10 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
202
202
| ExtractReturn< typeof Actions . clearErrors >
203
203
| ExtractReturn< typeof Actions . updateData >
204
204
| ExtractReturn< typeof Actions . endLoading > ;
205
- const Subreducer = new function() {
206
- this . partialReducer = partialReducer ;
205
+ class Subreducer {
206
+ static partialReducer = partialReducer ;
207
207
208
- this . subreduce = (
208
+ static subreduce = (
209
209
state ?: BoilerState < T > = Getters . getInitialState ( ) ,
210
210
action ?: GeneratedAction | * // Support other action types
211
211
) => {
@@ -235,13 +235,13 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
235
235
case RESET:
236
236
return (Getters.getInitialState(): BoilerState< T > );
237
237
default:
238
- if (typeof this .partialReducer === 'function') {
239
- return { ...state , ...( this . partialReducer ( state , action ) || { } ) } ;
238
+ if (typeof Subreducer .partialReducer === 'function') {
239
+ return { ...state , ...( Subreducer . partialReducer ( state , action ) || { } ) } ;
240
240
}
241
241
return state;
242
242
}
243
243
} ;
244
- } ;
244
+ }
245
245
246
246
if ( ! getInStore ) {
247
247
injectReducer ( [ prefix , safeDataName ] , Subreducer . subreduce ) ;
0 commit comments