Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

action subscribers would receive a call even when the action failed #1625

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/store.js
Expand Up @@ -148,7 +148,7 @@ export class Store {
try {
this._actionSubscribers
.filter(sub => sub.after)
.forEach(sub => sub.after(action, this.state))
.forEach(sub => sub.after(action, this.state, /* isError: */false, res))
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
console.warn(`[vuex] error in after action subscribers: `)
Expand All @@ -157,6 +157,19 @@ export class Store {
}
return res
})
.catch(errorResult => {
try {
this._actionSubscribers
.filter(sub => sub.after)
.forEach(sub => sub.after(action, this.state, /* isError: */true, errorResult))
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
console.warn(`[vuex] error in after action subscribers: `)
console.error(e)
}
}
throw errorResult
})
}

subscribe (fn) {
Expand Down
42 changes: 39 additions & 3 deletions test/unit/modules.spec.js
Expand Up @@ -669,12 +669,12 @@ describe('Modules', () => {
)
})

it('action before/after subscribers', (done) => {
it('action before/after subscribers with resolve()', (done) => {
const beforeSpy = jasmine.createSpy()
const afterSpy = jasmine.createSpy()
const store = new Vuex.Store({
actions: {
[TEST]: () => Promise.resolve()
[TEST]: () => Promise.resolve('resolve')
},
plugins: [
store => {
Expand All @@ -694,7 +694,43 @@ describe('Modules', () => {
Vue.nextTick(() => {
expect(afterSpy).toHaveBeenCalledWith(
{ type: TEST, payload: 2 },
store.state
store.state,
false,
'resolve'
)
done()
})
})
})

it('action before/after subscribers with reject()', (done) => {
const beforeSpy = jasmine.createSpy()
const afterSpy = jasmine.createSpy()
const store = new Vuex.Store({
actions: {
[TEST]: () => Promise.reject('reject')
},
plugins: [
store => {
store.subscribeAction({
before: beforeSpy,
after: afterSpy
})
}
]
})
store.dispatch(TEST, 2)
expect(beforeSpy).toHaveBeenCalledWith(
{ type: TEST, payload: 2 },
store.state
)
expect(afterSpy).not.toHaveBeenCalled()
Vue.nextTick(() => {
expect(afterSpy).toHaveBeenCalledWith(
{ type: TEST, payload: 2 },
store.state,
false,
'reject'
)
done()
})
Expand Down