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

Allow action subscribers to catch rejections. #1740

Merged
merged 9 commits into from
May 8, 2020
36 changes: 25 additions & 11 deletions dist/vuex.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,18 +458,32 @@ Store.prototype.dispatch = function dispatch (_type, _payload) {
? Promise.all(entry.map(function (handler) { return handler(payload); }))
: entry[0](payload);

return result.then(function (res) {
try {
this$1._actionSubscribers
.filter(function (sub) { return sub.after; })
.forEach(function (sub) { return sub.after(action, this$1.state); });
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
console.warn("[vuex] error in after action subscribers: ");
console.error(e);
return new Promise(function (resolve, reject) {
result.then(function (res) {
try {
this$1._actionSubscribers
.filter(function (sub) { return sub.after; })
.forEach(function (sub) { return sub.after(action, this$1.state); });
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
console.warn("[vuex] error in after action subscribers: ");
console.error(e);
}
}
}
return res
resolve(res);
}, function (e) {
try {
this$1._actionSubscribers
.filter(function (sub) { return sub.catch; })
.forEach(function (sub) { return sub.catch(action, this$1.state, e); });
} catch (_e) {
if (process.env.NODE_ENV !== 'production') {
console.warn("[vuex] error in after action catch subscribers: ");
console.error(_e);
}
}
reject(e);
});
})
};

Expand Down
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 after action catch subscribers: `)
jmplahitko marked this conversation as resolved.
Show resolved Hide resolved
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