@@ -35,6 +35,7 @@ export class Store {
3535 // store internal state
3636 this . _committing = false
3737 this . _actions = Object . create ( null )
38+ this . _actionSubscribers = [ ]
3839 this . _mutations = Object . create ( null )
3940 this . _wrappedGetters = Object . create ( null )
4041 this . _modules = new ModuleCollection ( options )
@@ -123,29 +124,28 @@ export class Store {
123124 payload
124125 } = unifyObjectStyle ( _type , _payload )
125126
127+ const action = { type, payload }
126128 const entry = this . _actions [ type ]
127129 if ( ! entry ) {
128130 if ( process . env . NODE_ENV !== 'production' ) {
129131 console . error ( `[vuex] unknown action type: ${ type } ` )
130132 }
131133 return
132134 }
135+
136+ this . _actionSubscribers . forEach ( sub => sub ( action , this . state ) )
137+
133138 return entry . length > 1
134139 ? Promise . all ( entry . map ( handler => handler ( payload ) ) )
135140 : entry [ 0 ] ( payload )
136141 }
137142
138143 subscribe ( fn ) {
139- const subs = this . _subscribers
140- if ( subs . indexOf ( fn ) < 0 ) {
141- subs . push ( fn )
142- }
143- return ( ) => {
144- const i = subs . indexOf ( fn )
145- if ( i > - 1 ) {
146- subs . splice ( i , 1 )
147- }
148- }
144+ return genericSubscribe ( fn , this . _subscribers )
145+ }
146+
147+ subscribeAction ( fn ) {
148+ return genericSubscribe ( fn , this . _actionSubscribers )
149149 }
150150
151151 watch ( getter , cb , options ) {
@@ -203,6 +203,18 @@ export class Store {
203203 }
204204}
205205
206+ function genericSubscribe ( fn , subs ) {
207+ if ( subs . indexOf ( fn ) < 0 ) {
208+ subs . push ( fn )
209+ }
210+ return ( ) => {
211+ const i = subs . indexOf ( fn )
212+ if ( i > - 1 ) {
213+ subs . splice ( i , 1 )
214+ }
215+ }
216+ }
217+
206218function resetStore ( store , hot ) {
207219 store . _actions = Object . create ( null )
208220 store . _mutations = Object . create ( null )
0 commit comments