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. #1531

Closed
wants to merge 3 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions dist/vuex.common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* vuex v3.1.0
* (c) 2019 Evan You
* (c) 2020 Evan You
* @license MIT
*/
'use strict';
Expand Down Expand Up @@ -41,9 +41,12 @@ function applyMixin (Vue) {
}
}

var devtoolHook =
typeof window !== 'undefined' &&
window.__VUE_DEVTOOLS_GLOBAL_HOOK__;
var target = typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: {};
var devtoolHook = target.__VUE_DEVTOOLS_GLOBAL_HOOK__;

function devtoolPlugin (store) {
if (!devtoolHook) { return }
Expand Down Expand Up @@ -429,18 +432,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 @@ -144,18 +144,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 (process.env.NODE_ENV !== 'production') {
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 (process.env.NODE_ENV !== 'production') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change process.env.NODE_ENV !== 'production' to __DEV__. This change was introduced in the latest commit.

Suggested change
if (process.env.NODE_ENV !== 'production') {
if (__DEV__) {

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one as well.

Suggested change
if (process.env.NODE_ENV !== 'production') {
if (__DEV__) {

console.warn(`[vuex] error in after action catch 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 @@ -701,6 +701,46 @@ describe('Modules', () => {
})
})

it('action catch subscribers', (done) => {
const beforeSpy = jasmine.createSpy()
const afterSpy = jasmine.createSpy()
const catchSpy = jasmine.createSpy()
const error = new Error()
const store = new Vuex.Store({
actions: {
[TEST]: () => Promise.reject(error)
},
plugins: [
store => {
store.subscribeAction({
before: beforeSpy,
after: afterSpy,
catch: catchSpy
})
}
]
})
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(catchSpy).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 @@ -70,10 +70,12 @@ export interface ActionPayload extends Payload {
}

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

export interface ActionSubscribersObject<P, S> {
before?: ActionSubscriber<P, S>;
after?: ActionSubscriber<P, S>;
catch?: ActionCatchSubscriber<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 @@ -66,11 +66,67 @@ namespace StoreInstance {
}
});

store.subscribeAction({
before(action, state) {
action.type;
action.payload;
state.value;
},
catch(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;
},
catch(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;
},
catch(action, state, error) {
action.type;
action.payload;
state.value;
error;
}
});

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

Expand Down