Skip to content

Commit cdba92e

Browse files
author
Wael Al-Sallami
committed
allow action subscribers to specify before/after hooks (vuejs#1098)
Action subscribers are called before the action by default. This allows them to specify before and after subscribers where the after subscriber is called when the action resolves
1 parent 41e58f5 commit cdba92e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/store.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,28 @@ export class Store {
128128
return
129129
}
130130

131-
this._actionSubscribers.forEach(sub => sub(action, this.state))
131+
this._actionSubscribers
132+
.filter(sub => sub.before)
133+
.forEach(sub => sub.before(action, this.state))
132134

133-
return entry.length > 1
135+
const result = entry.length > 1
134136
? Promise.all(entry.map(handler => handler(payload)))
135137
: entry[0](payload)
138+
139+
result.then(() => this._actionSubscribers
140+
.filter(sub => sub.after)
141+
.forEach(sub => sub.after(action, this.state)))
142+
143+
return result
136144
}
137145

138146
subscribe (fn) {
139147
return genericSubscribe(fn, this._subscribers)
140148
}
141149

142150
subscribeAction (fn) {
143-
return genericSubscribe(fn, this._actionSubscribers)
151+
const subs = typeof fn === 'function' ? { before: fn } : fn
152+
return genericSubscribe(subs, this._actionSubscribers)
144153
}
145154

146155
watch (getter, cb, options) {

0 commit comments

Comments
 (0)