File tree Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ class Store {
22
22
this . _actions = Object . create ( null )
23
23
this . _mutations = Object . create ( null )
24
24
this . _subscribers = [ ]
25
+ this . _pendingActions = [ ]
25
26
26
27
// bind commit and dispatch to self
27
28
const store = this
@@ -162,9 +163,19 @@ class Store {
162
163
console . error ( `[vuex] unknown action type: ${ type } ` )
163
164
return
164
165
}
165
- return entry . length > 1
166
+ const res = entry . length > 1
166
167
? Promise . all ( entry . map ( handler => handler ( payload ) ) )
167
168
: entry [ 0 ] ( payload )
169
+ const pending = this . _pendingActions
170
+ pending . push ( res )
171
+ return res . then ( value => {
172
+ pending . splice ( pending . indexOf ( res ) , 1 )
173
+ return value
174
+ } )
175
+ }
176
+
177
+ onActionsResolved ( cb ) {
178
+ Promise . all ( this . _pendingActions ) . then ( cb )
168
179
}
169
180
170
181
subscribe ( fn ) {
Original file line number Diff line number Diff line change @@ -133,6 +133,45 @@ describe('Vuex', () => {
133
133
} )
134
134
} )
135
135
136
+ it ( 'onActionsResolved' , done => {
137
+ const store = new Vuex . Store ( {
138
+ state : {
139
+ count : 0
140
+ } ,
141
+ mutations : {
142
+ inc : state => state . count ++
143
+ } ,
144
+ actions : {
145
+ one ( { commit } ) {
146
+ return new Promise ( r => {
147
+ commit ( 'inc' )
148
+ r ( 1 )
149
+ } )
150
+ } ,
151
+ two ( { commit } ) {
152
+ return new Promise ( r => {
153
+ setTimeout ( ( ) => {
154
+ commit ( 'inc' )
155
+ r ( 2 )
156
+ } , 0 )
157
+ } )
158
+ }
159
+ }
160
+ } )
161
+ store . dispatch ( 'one' )
162
+ store . dispatch ( 'two' )
163
+ expect ( store . state . count ) . to . equal ( 1 )
164
+ expect ( store . _pendingActions . length ) . to . equal ( 2 )
165
+ store . onActionsResolved ( res => {
166
+ expect ( store . _pendingActions . length ) . to . equal ( 0 )
167
+ expect ( store . state . count ) . to . equal ( 2 )
168
+ expect ( res . length ) . to . equal ( 2 )
169
+ expect ( res [ 0 ] ) . to . equal ( 1 )
170
+ expect ( res [ 1 ] ) . to . equal ( 2 )
171
+ done ( )
172
+ } )
173
+ } )
174
+
136
175
it ( 'getters' , ( ) => {
137
176
const store = new Vuex . Store ( {
138
177
state : {
You can’t perform that action at this time.
0 commit comments