11/**
2- * vuex v2.4.1
2+ * vuex v2.5.0
33 * (c) 2017 Evan You
44 * @license MIT
55 */
@@ -246,26 +246,44 @@ function update (path, targetModule, newModule) {
246246 }
247247}
248248
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+
249266function assertRawModule ( path , rawModule ) {
250- [ 'getters' , 'actions' , 'mutations' ] . forEach ( function ( key ) {
267+ Object . keys ( assertTypes ) . forEach ( function ( key ) {
251268 if ( ! rawModule [ key ] ) { return }
252269
270+ var assertOptions = assertTypes [ key ] ;
271+
253272 forEachValue ( rawModule [ key ] , function ( value , type ) {
254273 assert (
255- typeof value === 'function' ,
256- makeAssertionMessage ( path , key , type , value )
274+ assertOptions . assert ( value ) ,
275+ makeAssertionMessage ( path , key , type , value , assertOptions . expected )
257276 ) ;
258277 } ) ;
259278 } ) ;
260279}
261280
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 + "\"" ;
264283 if ( path . length > 0 ) {
265284 buf += " in module \"" + ( path . join ( '.' ) ) + "\"" ;
266285 }
267286 buf += " is " + ( JSON . stringify ( value ) ) + "." ;
268-
269287 return buf
270288}
271289
@@ -293,12 +311,13 @@ var Store = function Store (options) {
293311
294312 var state = options . state ; if ( state === void 0 ) state = { } ;
295313 if ( typeof state === 'function' ) {
296- state = state ( ) ;
314+ state = state ( ) || { } ;
297315 }
298316
299317 // store internal state
300318 this . _committing = false ;
301319 this . _actions = Object . create ( null ) ;
320+ this . _actionSubscribers = [ ] ;
302321 this . _mutations = Object . create ( null ) ;
303322 this . _wrappedGetters = Object . create ( null ) ;
304323 this . _modules = new ModuleCollection ( options ) ;
@@ -386,34 +405,35 @@ Store.prototype.commit = function commit (_type, _payload, _options) {
386405} ;
387406
388407Store . prototype . dispatch = function dispatch ( _type , _payload ) {
408+ var this$1 = this ;
409+
389410 // check object-style dispatch
390411 var ref = unifyObjectStyle ( _type , _payload ) ;
391412 var type = ref . type ;
392413 var payload = ref . payload ;
393414
415+ var action = { type : type , payload : payload } ;
394416 var entry = this . _actions [ type ] ;
395417 if ( ! entry ) {
396418 if ( process . env . NODE_ENV !== 'production' ) {
397419 console . error ( ( "[vuex] unknown action type: " + type ) ) ;
398420 }
399421 return
400422 }
423+
424+ this . _actionSubscribers . forEach ( function ( sub ) { return sub ( action , this$1 . state ) ; } ) ;
425+
401426 return entry . length > 1
402427 ? Promise . all ( entry . map ( function ( handler ) { return handler ( payload ) ; } ) )
403428 : entry [ 0 ] ( payload )
404429} ;
405430
406431Store . 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 )
417437} ;
418438
419439Store . prototype . watch = function watch ( getter , cb , options ) {
@@ -433,7 +453,9 @@ Store.prototype.replaceState = function replaceState (state) {
433453 } ) ;
434454} ;
435455
436- Store . prototype . registerModule = function registerModule ( path , rawModule ) {
456+ Store . prototype . registerModule = function registerModule ( path , rawModule , options ) {
457+ if ( options === void 0 ) options = { } ;
458+
437459 if ( typeof path === 'string' ) { path = [ path ] ; }
438460
439461 if ( process . env . NODE_ENV !== 'production' ) {
@@ -442,7 +464,7 @@ Store.prototype.registerModule = function registerModule (path, rawModule) {
442464 }
443465
444466 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 ) ;
446468 // reset store to update getters...
447469 resetStoreVM ( this , this . state ) ;
448470} ;
@@ -478,6 +500,18 @@ Store.prototype._withCommit = function _withCommit (fn) {
478500
479501Object . defineProperties ( Store . prototype , prototypeAccessors ) ;
480502
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+
481515function resetStore ( store , hot ) {
482516 store . _actions = Object . create ( null ) ;
483517 store . _mutations = Object . create ( null ) ;
@@ -562,8 +596,9 @@ function installModule (store, rootState, path, module, hot) {
562596 } ) ;
563597
564598 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 ) ;
567602 } ) ;
568603
569604 module . forEachGetter ( function ( getter , key ) {
@@ -886,7 +921,7 @@ function getModuleByNamespace (store, helper, namespace) {
886921var index = {
887922 Store : Store ,
888923 install : install ,
889- version : '2.4.1 ' ,
924+ version : '2.5.0 ' ,
890925 mapState : mapState ,
891926 mapMutations : mapMutations ,
892927 mapGetters : mapGetters ,
0 commit comments