Skip to content

Commit

Permalink
feat: ensure errors in action subscribers do not break actions
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 17, 2019
1 parent 59f31af commit acd7249
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,34 @@ export class Store {
return
}

this._actionSubscribers
.filter(sub => sub.before)
.forEach(sub => sub.before(action, this.state))
try {
this._actionSubscribers
.filter(sub => sub.before)
.forEach(sub => sub.before(action, this.state))
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
console.warn(`[vuex] error in before action subscribers: `)
console.error(e)
}
}

const result = entry.length > 1
? Promise.all(entry.map(handler => handler(payload)))
: entry[0](payload)

result.then(() => this._actionSubscribers
.filter(sub => sub.after)
.forEach(sub => sub.after(action, this.state)))

return result
return result.then(res => {
try {
this._actionSubscribers
.filter(sub => sub.after)
.forEach(sub => sub.after(action, this.state))
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
console.warn(`[vuex] error in after action subscribers: `)
console.error(e)
}
}
return res
})
}

subscribe (fn) {
Expand Down

0 comments on commit acd7249

Please sign in to comment.