@@ -2,18 +2,18 @@ import React from 'react';
2
2
import { Provider } from 'react-redux' ;
3
3
import { createStore , applyMiddleware , compose , combineReducers } from 'redux' ;
4
4
import createSagaMiddleware from 'redux-saga/lib/internal/middleware' ;
5
- import {
6
- takeEveryHelper as takeEvery ,
7
- takeLatestHelper as takeLatest ,
8
- throttleHelper as throttle
9
- } from 'redux-saga/lib/internal/sagaHelpers' ;
10
- import handleActions from './handleActions' ;
11
5
import * as sagaEffects from 'redux-saga/effects' ;
12
6
import isPlainObject from 'is-plain-object' ;
13
7
import invariant from 'invariant' ;
14
8
import warning from 'warning' ;
15
9
import flatten from 'flatten' ;
16
10
import window from 'global/window' ;
11
+ import {
12
+ takeEveryHelper as takeEvery ,
13
+ takeLatestHelper as takeLatest ,
14
+ throttleHelper as throttle ,
15
+ } from 'redux-saga/lib/internal/sagaHelpers' ;
16
+ import handleActions from './handleActions' ;
17
17
import Plugin from './plugin' ;
18
18
19
19
const SEP = '/' ;
@@ -55,7 +55,7 @@ export default function createDva(createOpts) {
55
55
} ;
56
56
return app ;
57
57
58
- ////////////////////////////////////
58
+ // //////////////////////////////////
59
59
// Methods
60
60
61
61
/**
@@ -117,14 +117,14 @@ export default function createDva(createOpts) {
117
117
// support selector
118
118
if ( typeof container === 'string' ) {
119
119
container = document . querySelector ( container ) ;
120
- invariant ( container , ' app.start: could not query selector: ' + container ) ;
120
+ invariant ( container , ` app.start: could not query selector: ${ container } ` ) ;
121
121
}
122
122
123
123
invariant ( ! container || isHTMLElement ( container ) , 'app.start: container should be HTMLElement' ) ;
124
124
invariant ( this . _router , 'app.start: router should be defined' ) ;
125
125
126
126
// error wrapper
127
- const onError = plugin . apply ( 'onError' , function ( err ) {
127
+ const onError = plugin . apply ( 'onError' , ( err ) => {
128
128
throw new Error ( err . stack || err ) ;
129
129
} ) ;
130
130
const onErrorWrapper = ( err ) => {
@@ -135,9 +135,9 @@ export default function createDva(createOpts) {
135
135
} ;
136
136
137
137
// get reducers and sagas from model
138
- let sagas = [ ] ;
139
- let reducers = { ...initialReducer } ;
140
- for ( let m of this . _models ) {
138
+ const sagas = [ ] ;
139
+ const reducers = { ...initialReducer } ;
140
+ for ( const m of this . _models ) {
141
141
reducers [ m . namespace ] = getReducer ( m . reducers , m . state ) ;
142
142
if ( m . effects ) sagas . push ( getSaga ( m . effects , m , onErrorWrapper ) ) ;
143
143
}
@@ -146,7 +146,7 @@ export default function createDva(createOpts) {
146
146
const extraReducers = plugin . get ( 'extraReducers' ) ;
147
147
invariant (
148
148
Object . keys ( extraReducers ) . every ( key => ! ( key in reducers ) ) ,
149
- 'app.start: extraReducers is conflict with other reducers'
149
+ 'app.start: extraReducers is conflict with other reducers' ,
150
150
) ;
151
151
152
152
// create store
@@ -168,7 +168,7 @@ export default function createDva(createOpts) {
168
168
const store = this . _store = createStore (
169
169
createReducer ( ) ,
170
170
initialState ,
171
- compose ( ...enhancers )
171
+ compose ( ...enhancers ) ,
172
172
) ;
173
173
174
174
function createReducer ( asyncReducers ) {
@@ -185,7 +185,7 @@ export default function createDva(createOpts) {
185
185
186
186
// store change
187
187
const listeners = plugin . get ( 'onStateChange' ) ;
188
- for ( let listener of listeners ) {
188
+ for ( const listener of listeners ) {
189
189
store . subscribe ( listener ) ;
190
190
}
191
191
@@ -196,7 +196,7 @@ export default function createDva(createOpts) {
196
196
if ( setupHistory ) setupHistory . call ( this , history ) ;
197
197
198
198
// run subscriptions
199
- for ( let model of this . _models ) {
199
+ for ( const model of this . _models ) {
200
200
if ( model . subscriptions ) {
201
201
runSubscriptions ( model . subscriptions , model , this , onErrorWrapper ) ;
202
202
}
@@ -214,11 +214,11 @@ export default function createDva(createOpts) {
214
214
}
215
215
}
216
216
217
- ////////////////////////////////////
217
+ // //////////////////////////////////
218
218
// Helpers
219
219
220
220
function getProvider ( store , app , router ) {
221
- return ( extraProps ) => (
221
+ return extraProps => (
222
222
< Provider store = { store } >
223
223
{ router ( { app, history : app . _history , ...extraProps } ) }
224
224
</ Provider >
@@ -237,41 +237,41 @@ export default function createDva(createOpts) {
237
237
238
238
invariant (
239
239
namespace ,
240
- 'app.model: namespace should be defined'
240
+ 'app.model: namespace should be defined' ,
241
241
) ;
242
242
243
243
invariant (
244
244
! app . _models . some ( model => model . namespace === namespace ) ,
245
- 'app.model: namespace should be unique'
245
+ 'app.model: namespace should be unique' ,
246
246
) ;
247
247
248
248
invariant (
249
249
mobile || namespace !== 'routing' ,
250
- 'app.model: namespace should not be routing, it\'s used by react-redux-router'
250
+ 'app.model: namespace should not be routing, it\'s used by react-redux-router' ,
251
251
) ;
252
252
invariant (
253
253
! model . subscriptions || isPlainObject ( model . subscriptions ) ,
254
- 'app.model: subscriptions should be Object'
254
+ 'app.model: subscriptions should be Object' ,
255
255
) ;
256
256
invariant (
257
257
! reducers || isPlainObject ( reducers ) || Array . isArray ( reducers ) ,
258
- 'app.model: reducers should be Object or array'
258
+ 'app.model: reducers should be Object or array' ,
259
259
) ;
260
260
invariant (
261
261
! Array . isArray ( reducers ) || ( isPlainObject ( reducers [ 0 ] ) && typeof reducers [ 1 ] === 'function' ) ,
262
- 'app.model: reducers with array should be app.model({ reducers: [object, function] })'
262
+ 'app.model: reducers with array should be app.model({ reducers: [object, function] })' ,
263
263
) ;
264
264
invariant (
265
265
! effects || isPlainObject ( effects ) ,
266
- 'app.model: effects should be Object'
266
+ 'app.model: effects should be Object' ,
267
267
) ;
268
268
269
269
function applyNamespace ( type ) {
270
270
function getNamespacedReducers ( reducers ) {
271
271
return Object . keys ( reducers ) . reduce ( ( memo , key ) => {
272
272
warning (
273
273
key . indexOf ( `${ namespace } ${ SEP } ` ) !== 0 ,
274
- `app.model: ${ type . slice ( 0 , - 1 ) } ${ key } should not be prefixed with namespace ${ namespace } `
274
+ `app.model: ${ type . slice ( 0 , - 1 ) } ${ key } should not be prefixed with namespace ${ namespace } ` ,
275
275
) ;
276
276
memo [ `${ namespace } ${ SEP } ${ key } ` ] = reducers [ key ] ;
277
277
return memo ;
@@ -309,11 +309,13 @@ export default function createDva(createOpts) {
309
309
310
310
function getSaga ( effects , model , onError ) {
311
311
return function * ( ) {
312
- for ( let key in effects ) {
313
- const watcher = getWatcher ( key , effects [ key ] , model , onError ) ;
314
- yield sagaEffects . fork ( watcher ) ;
312
+ for ( const key in effects ) {
313
+ if ( Object . prototype . hasOwnProperty . call ( effects , key ) ) {
314
+ const watcher = getWatcher ( key , effects [ key ] , model , onError ) ;
315
+ yield sagaEffects . fork ( watcher ) ;
316
+ }
315
317
}
316
- }
318
+ } ;
317
319
}
318
320
319
321
function getWatcher ( key , _effect , model , onError ) {
@@ -329,21 +331,21 @@ export default function createDva(createOpts) {
329
331
if ( type === 'throttle' ) {
330
332
invariant (
331
333
opts . ms ,
332
- 'app.start: opts.ms should be defined if type is throttle'
334
+ 'app.start: opts.ms should be defined if type is throttle' ,
333
335
) ;
334
336
ms = opts . ms ;
335
337
}
336
338
}
337
339
invariant (
338
340
[ 'watcher' , 'takeEvery' , 'takeLatest' , 'throttle' ] . indexOf ( type ) > - 1 ,
339
- 'app.start: effect type should be takeEvery, takeLatest, throttle or watcher'
341
+ 'app.start: effect type should be takeEvery, takeLatest, throttle or watcher' ,
340
342
) ;
341
343
}
342
344
343
345
function * sagaWithCatch ( ...args ) {
344
346
try {
345
347
yield effect ( ...args . concat ( createEffects ( model ) ) ) ;
346
- } catch ( e ) {
348
+ } catch ( e ) {
347
349
onError ( e ) ;
348
350
}
349
351
}
@@ -371,13 +373,15 @@ export default function createDva(createOpts) {
371
373
}
372
374
373
375
function runSubscriptions ( subs , model , app , onError ) {
374
- for ( let key in subs ) {
375
- const sub = subs [ key ] ;
376
- invariant ( typeof sub === 'function' , 'app.start: subscription should be function' ) ;
377
- sub ( {
378
- dispatch : createDispatch ( app . _store . dispatch , model ) ,
379
- history : app . _history ,
380
- } , onError ) ;
376
+ for ( const key in subs ) {
377
+ if ( Object . prototype . hasOwnProperty . call ( subs , key ) ) {
378
+ const sub = subs [ key ] ;
379
+ invariant ( typeof sub === 'function' , 'app.start: subscription should be function' ) ;
380
+ sub ( {
381
+ dispatch : createDispatch ( app . _store . dispatch , model ) ,
382
+ history : app . _history ,
383
+ } , onError ) ;
384
+ }
381
385
}
382
386
}
383
387
@@ -396,31 +400,30 @@ export default function createDva(createOpts) {
396
400
invariant ( type , 'dispatch: action should be a plain Object with type' ) ;
397
401
warning (
398
402
type . indexOf ( `${ model . namespace } ${ SEP } ` ) !== 0 ,
399
- `effects.put: ${ type } should not be prefixed with namespace ${ model . namespace } `
403
+ `effects.put: ${ type } should not be prefixed with namespace ${ model . namespace } ` ,
400
404
) ;
401
405
return sagaEffects . put ( { ...action , type : prefixType ( type , model ) } ) ;
402
406
}
403
407
return { ...sagaEffects , put } ;
404
408
}
405
409
406
410
function createDispatch ( dispatch , model ) {
407
- return action => {
411
+ return ( action ) => {
408
412
const { type } = action ;
409
413
invariant ( type , 'dispatch: action should be a plain Object with type' ) ;
410
414
warning (
411
415
type . indexOf ( `${ model . namespace } ${ SEP } ` ) !== 0 ,
412
- `dispatch: ${ type } should not be prefixed with namespace ${ model . namespace } `
416
+ `dispatch: ${ type } should not be prefixed with namespace ${ model . namespace } ` ,
413
417
) ;
414
418
return dispatch ( { ...action , type : prefixType ( type , model ) } ) ;
415
419
} ;
416
420
}
417
421
418
422
function applyOnEffect ( fns , effect , model , key ) {
419
- for ( let fn of fns ) {
423
+ for ( const fn of fns ) {
420
424
effect = fn ( effect , sagaEffects , model , key ) ;
421
425
}
422
426
return effect ;
423
427
}
424
-
425
428
} ;
426
429
}
0 commit comments