Skip to content

Commit

Permalink
feat: Allow action subscribers to catch rejections. (#1740)
Browse files Browse the repository at this point in the history
close #1489 #1558 #1625 

Co-authored-by: Joe Plahitko <joseph.plahitko@egov.com>
  • Loading branch information
jmplahitko and Joe Plahitko authored May 8, 2020
1 parent dfacba6 commit 6ebbe64
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,32 @@ export class Store {
? Promise.all(entry.map(handler => handler(payload)))
: entry[0](payload)

return result.then(res => {
try {
this._actionSubscribers
.filter(sub => sub.after)
.forEach(sub => sub.after(action, this.state))
} catch (e) {
if (__DEV__) {
console.warn(`[vuex] error in after action subscribers: `)
console.error(e)
return new Promise((resolve, reject) => {
result.then(res => {
try {
this._actionSubscribers
.filter(sub => sub.after)
.forEach(sub => sub.after(action, this.state))
} catch (e) {
if (__DEV__) {
console.warn(`[vuex] error in after action subscribers: `)
console.error(e)
}
}
}
return res
resolve(res)
}, e => {
try {
this._actionSubscribers
.filter(sub => sub.error)
.forEach(sub => sub.error(action, this.state, e))
} catch (_e) {
if (__DEV__) {
console.warn(`[vuex] error in error action subscribers: `)
console.error(_e)
}
}
reject(e)
})
})
}

Expand Down
40 changes: 40 additions & 0 deletions test/unit/modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,46 @@ describe('Modules', () => {
})
})

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

it('asserts a mutation should be a function', () => {
expect(() => {
new Vuex.Store({
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ export interface SubscribeOptions {
}

export type ActionSubscriber<P, S> = (action: P, state: S) => any;
export type ActionErrorSubscriber<P, S> = (action: P, state: S, error: Error) => any;

export interface ActionSubscribersObject<P, S> {
before?: ActionSubscriber<P, S>;
after?: ActionSubscriber<P, S>;
error?: ActionErrorSubscriber<P, S>;
}

export type SubscribeActionOptions<P, S> = ActionSubscriber<P, S> | ActionSubscribersObject<P, S>;
Expand Down
56 changes: 56 additions & 0 deletions types/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,67 @@ namespace StoreInstance {
}
});

store.subscribeAction({
before(action, state) {
action.type;
action.payload;
state.value;
},
error(action, state, error) {
action.type;
action.payload;
state.value;
error;
}
});

store.subscribeAction({
before(action, state) {
action.type;
action.payload;
state.value;
},
after(action, state) {
action.type;
action.payload;
state.value;
},
error(action, state, error) {
action.type;
action.payload;
state.value;
error;
}
});

store.subscribeAction({
after(action, state) {
action.type;
action.payload;
state.value;
}
});

store.subscribeAction({
after(action, state) {
action.type;
action.payload;
state.value;
},
error(action, state, error) {
action.type;
action.payload;
state.value;
error;
}
});

store.subscribeAction({
error(action, state, error) {
action.type;
action.payload;
state.value;
error;
}
});

Expand Down

0 comments on commit 6ebbe64

Please sign in to comment.