1
1
/**
2
- * vuex v2.4.1
2
+ * vuex v2.5.0
3
3
* (c) 2017 Evan You
4
4
* @license MIT
5
5
*/
@@ -246,26 +246,44 @@ function update (path, targetModule, newModule) {
246
246
}
247
247
}
248
248
249
+ var functionAssert = {
250
+ assert : function ( value ) { return typeof value === 'function' ; } ,
251
+ expected : 'function'
252
+ } ;
253
+
254
+ var objectAssert = {
255
+ assert : function ( value ) { return typeof value === 'function' ||
256
+ ( typeof value === 'object' && typeof value . handler === 'function' ) ; } ,
257
+ expected : 'function or object with "handler" function'
258
+ } ;
259
+
260
+ var assertTypes = {
261
+ getters : functionAssert ,
262
+ mutations : functionAssert ,
263
+ actions : objectAssert
264
+ } ;
265
+
249
266
function assertRawModule ( path , rawModule ) {
250
- [ 'getters' , 'actions' , 'mutations' ] . forEach ( function ( key ) {
267
+ Object . keys ( assertTypes ) . forEach ( function ( key ) {
251
268
if ( ! rawModule [ key ] ) { return }
252
269
270
+ var assertOptions = assertTypes [ key ] ;
271
+
253
272
forEachValue ( rawModule [ key ] , function ( value , type ) {
254
273
assert (
255
- typeof value === 'function' ,
256
- makeAssertionMessage ( path , key , type , value )
274
+ assertOptions . assert ( value ) ,
275
+ makeAssertionMessage ( path , key , type , value , assertOptions . expected )
257
276
) ;
258
277
} ) ;
259
278
} ) ;
260
279
}
261
280
262
- function makeAssertionMessage ( path , key , type , value ) {
263
- var buf = key + " should be function but \"" + key + "." + type + "\"" ;
281
+ function makeAssertionMessage ( path , key , type , value , expected ) {
282
+ var buf = key + " should be " + expected + " but \"" + key + "." + type + "\"" ;
264
283
if ( path . length > 0 ) {
265
284
buf += " in module \"" + ( path . join ( '.' ) ) + "\"" ;
266
285
}
267
286
buf += " is " + ( JSON . stringify ( value ) ) + "." ;
268
-
269
287
return buf
270
288
}
271
289
@@ -293,12 +311,13 @@ var Store = function Store (options) {
293
311
294
312
var state = options . state ; if ( state === void 0 ) state = { } ;
295
313
if ( typeof state === 'function' ) {
296
- state = state ( ) ;
314
+ state = state ( ) || { } ;
297
315
}
298
316
299
317
// store internal state
300
318
this . _committing = false ;
301
319
this . _actions = Object . create ( null ) ;
320
+ this . _actionSubscribers = [ ] ;
302
321
this . _mutations = Object . create ( null ) ;
303
322
this . _wrappedGetters = Object . create ( null ) ;
304
323
this . _modules = new ModuleCollection ( options ) ;
@@ -386,34 +405,35 @@ Store.prototype.commit = function commit (_type, _payload, _options) {
386
405
} ;
387
406
388
407
Store . prototype . dispatch = function dispatch ( _type , _payload ) {
408
+ var this$1 = this ;
409
+
389
410
// check object-style dispatch
390
411
var ref = unifyObjectStyle ( _type , _payload ) ;
391
412
var type = ref . type ;
392
413
var payload = ref . payload ;
393
414
415
+ var action = { type : type , payload : payload } ;
394
416
var entry = this . _actions [ type ] ;
395
417
if ( ! entry ) {
396
418
if ( process . env . NODE_ENV !== 'production' ) {
397
419
console . error ( ( "[vuex] unknown action type: " + type ) ) ;
398
420
}
399
421
return
400
422
}
423
+
424
+ this . _actionSubscribers . forEach ( function ( sub ) { return sub ( action , this$1 . state ) ; } ) ;
425
+
401
426
return entry . length > 1
402
427
? Promise . all ( entry . map ( function ( handler ) { return handler ( payload ) ; } ) )
403
428
: entry [ 0 ] ( payload )
404
429
} ;
405
430
406
431
Store . prototype . subscribe = function subscribe ( fn ) {
407
- var subs = this . _subscribers ;
408
- if ( subs . indexOf ( fn ) < 0 ) {
409
- subs . push ( fn ) ;
410
- }
411
- return function ( ) {
412
- var i = subs . indexOf ( fn ) ;
413
- if ( i > - 1 ) {
414
- subs . splice ( i , 1 ) ;
415
- }
416
- }
432
+ return genericSubscribe ( fn , this . _subscribers )
433
+ } ;
434
+
435
+ Store . prototype . subscribeAction = function subscribeAction ( fn ) {
436
+ return genericSubscribe ( fn , this . _actionSubscribers )
417
437
} ;
418
438
419
439
Store . prototype . watch = function watch ( getter , cb , options ) {
@@ -433,7 +453,9 @@ Store.prototype.replaceState = function replaceState (state) {
433
453
} ) ;
434
454
} ;
435
455
436
- Store . prototype . registerModule = function registerModule ( path , rawModule ) {
456
+ Store . prototype . registerModule = function registerModule ( path , rawModule , options ) {
457
+ if ( options === void 0 ) options = { } ;
458
+
437
459
if ( typeof path === 'string' ) { path = [ path ] ; }
438
460
439
461
if ( process . env . NODE_ENV !== 'production' ) {
@@ -442,7 +464,7 @@ Store.prototype.registerModule = function registerModule (path, rawModule) {
442
464
}
443
465
444
466
this . _modules . register ( path , rawModule ) ;
445
- installModule ( this , this . state , path , this . _modules . get ( path ) ) ;
467
+ installModule ( this , this . state , path , this . _modules . get ( path ) , options . preserveState ) ;
446
468
// reset store to update getters...
447
469
resetStoreVM ( this , this . state ) ;
448
470
} ;
@@ -478,6 +500,18 @@ Store.prototype._withCommit = function _withCommit (fn) {
478
500
479
501
Object . defineProperties ( Store . prototype , prototypeAccessors ) ;
480
502
503
+ function genericSubscribe ( fn , subs ) {
504
+ if ( subs . indexOf ( fn ) < 0 ) {
505
+ subs . push ( fn ) ;
506
+ }
507
+ return function ( ) {
508
+ var i = subs . indexOf ( fn ) ;
509
+ if ( i > - 1 ) {
510
+ subs . splice ( i , 1 ) ;
511
+ }
512
+ }
513
+ }
514
+
481
515
function resetStore ( store , hot ) {
482
516
store . _actions = Object . create ( null ) ;
483
517
store . _mutations = Object . create ( null ) ;
@@ -562,8 +596,9 @@ function installModule (store, rootState, path, module, hot) {
562
596
} ) ;
563
597
564
598
module . forEachAction ( function ( action , key ) {
565
- var namespacedType = namespace + key ;
566
- registerAction ( store , namespacedType , action , local ) ;
599
+ var type = action . root ? key : namespace + key ;
600
+ var handler = action . handler || action ;
601
+ registerAction ( store , type , handler , local ) ;
567
602
} ) ;
568
603
569
604
module . forEachGetter ( function ( getter , key ) {
@@ -886,7 +921,7 @@ function getModuleByNamespace (store, helper, namespace) {
886
921
var index = {
887
922
Store : Store ,
888
923
install : install ,
889
- version : '2.4.1 ' ,
924
+ version : '2.5.0 ' ,
890
925
mapState : mapState ,
891
926
mapMutations : mapMutations ,
892
927
mapGetters : mapGetters ,
0 commit comments