Skip to content

Commit 19ff43b

Browse files
committed
Simplify strongly typed promises returned by actions.
1 parent bd92456 commit 19ff43b

File tree

6 files changed

+55
-42
lines changed

6 files changed

+55
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuex-typescript",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "A simple way to get static typing, static code analysis and intellisense with Vuex library.",
55
"files": [
66
"dist/index.js",

src/index.ts

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,22 @@ export type MutationHandler<TModuleState, TPayload> =
1111
export type PayloadlessMutationHandler<TModuleState> =
1212
(state: TModuleState) => void;
1313

14-
export type ActionHandler<TModuleState, TRootState, TPayload> =
15-
(injectee: ActionContext<TModuleState, TRootState>, payload: TPayload) => void | Promise<any>;
16-
export type PromiseActionHandler<TModuleState, TRootState, TPayload, TPromise> =
17-
(injectee: ActionContext<TModuleState, TRootState>, payload: TPayload) => Promise<TPromise>;
18-
export type PayloadlessActionHandler<TModuleState, TRootState> =
19-
(injectee: ActionContext<TModuleState, TRootState>) => void | Promise<any>;
20-
export type PromisePayloadlessActionHandler<TModuleState, TRootState, TPromise> =
21-
(injectee: ActionContext<TModuleState, TRootState>) => Promise<TPromise>;
14+
export type ActionHandler<TModuleState, TRootState, TPayload, TResult> =
15+
(injectee: ActionContext<TModuleState, TRootState>, payload: TPayload) => void | Promise<TResult>;
16+
export type PayloadlessActionHandler<TModuleState, TRootState, TResult> =
17+
(injectee: ActionContext<TModuleState, TRootState>) => void | Promise<TResult>;
2218

2319
export type GetterHandler<TModuleState, TRootState, TResult> =
2420
(state: TModuleState, rootState: TRootState) => TResult;
2521

2622
export type GetAccessor<TModuleState, TRootState, TResult> =
2723
(store: Store<TRootState> | ActionContext<TModuleState, TRootState>) => TResult;
2824

29-
export type DispatchAccessor<TModuleState, TRootState, TPayload> =
25+
export type DispatchAccessor<TModuleState, TRootState, TPayload, TResult> =
3026
(store: Store<TRootState> | ActionContext<TModuleState, TRootState>,
31-
payload: TPayload) => Promise<any[]>;
32-
export type PayloadlessDispatchAccessor<TModuleState, TRootState> =
33-
(store: Store<TRootState> | ActionContext<TModuleState, TRootState>) => Promise<any[]>;
34-
export type PromiseDispatchAccessor<TModuleState, TRootState, TPayload, TPromise> =
35-
(store: Store<TRootState> | ActionContext<TModuleState, TRootState>,
36-
payload: TPayload) => Promise<TPromise>;
37-
export type PromisePayloadlessDispatchAccessor<TModuleState, TRootState, TPromise> =
38-
(store: Store<TRootState> | ActionContext<TModuleState, TRootState>) => Promise<TPromise>;
27+
payload: TPayload) => Promise<TResult>;
28+
export type PayloadlessDispatchAccessor<TModuleState, TRootState, TResult> =
29+
(store: Store<TRootState> | ActionContext<TModuleState, TRootState>) => Promise<TResult>;
3930

4031
export type CommitAccessor<TModuleState, TRootState, TPayload> =
4132
(store: Store<TRootState> | ActionContext<TModuleState, TRootState>,
@@ -51,18 +42,12 @@ export interface StoreAccessors<TModuleState, TRootState> {
5142
handler: PayloadlessMutationHandler<TModuleState>):
5243
PayloadlessCommitAccessor<TModuleState, TRootState>;
5344

54-
dispatch<TPayload, TPromise>(
55-
handler: PromiseActionHandler<TModuleState, TRootState, TPayload, TPromise>):
56-
PromiseDispatchAccessor<TModuleState, TRootState, TPayload, TPromise>;
57-
dispatch<TPayload>(
58-
handler: ActionHandler<TModuleState, TRootState, TPayload>):
59-
DispatchAccessor<TModuleState, TRootState, TPayload>;
60-
dispatchNoPayload(
61-
handler: PayloadlessActionHandler<TModuleState, TRootState>):
62-
PayloadlessDispatchAccessor<TModuleState, TRootState>;
63-
dispatchNoPayload<TPromise>(
64-
handler: PromisePayloadlessActionHandler<TModuleState, TRootState, TPromise>):
65-
PromisePayloadlessDispatchAccessor<TModuleState, TRootState, TPromise>;
45+
dispatch<TPayload, TResult>(
46+
handler: ActionHandler<TModuleState, TRootState, TPayload, TResult>):
47+
DispatchAccessor<TModuleState, TRootState, TPayload, TResult>;
48+
dispatchNoPayload<TResult>(
49+
handler: PayloadlessActionHandler<TModuleState, TRootState, TResult>):
50+
PayloadlessDispatchAccessor<TModuleState, TRootState, TResult>;
6651

6752
read<TResult>(
6853
handler: GetterHandler<TModuleState, TRootState, TResult>):
@@ -77,9 +62,9 @@ export function getStoreAccessors<TModuleState, TRootState>(
7762
commitNoPayload: (handler: PayloadlessMutationHandler<TModuleState>) =>
7863
commitNoPayload(handler, namespace),
7964

80-
dispatch: <TPayload>(handler: ActionHandler<TModuleState, TRootState, TPayload>) =>
65+
dispatch: <TPayload, TResult>(handler: ActionHandler<TModuleState, TRootState, TPayload, TResult>) =>
8166
dispatch(handler, namespace),
82-
dispatchNoPayload: (handler: PayloadlessActionHandler<TModuleState, TRootState>) =>
67+
dispatchNoPayload: <TResult>(handler: PayloadlessActionHandler<TModuleState, TRootState, TResult>) =>
8368
dispatchNoPayload(handler, namespace),
8469
read: <TResult>(handler: GetterHandler<TModuleState, TRootState, TResult>) =>
8570
read(handler, namespace),
@@ -97,21 +82,21 @@ function read<TModuleState, TRootState, TResult>(
9782
};
9883
}
9984

100-
function dispatch<TModuleState, TRootState, TPayload>(
101-
handler: ActionHandler<TModuleState, TRootState, TPayload>,
102-
namespace: string): DispatchAccessor<TModuleState, TRootState, TPayload> {
85+
function dispatch<TModuleState, TRootState, TPayload, TResult>(
86+
handler: ActionHandler<TModuleState, TRootState, TPayload, TResult>,
87+
namespace: string): DispatchAccessor<TModuleState, TRootState, TPayload, TResult> {
10388
const key = qualifyKey(handler, namespace);
10489
return (store, payload) => {
105-
return store.dispatch(key, payload, useRootNamespace);
90+
return <any>store.dispatch(key, payload, useRootNamespace);
10691
};
10792
}
10893

109-
function dispatchNoPayload<TModuleState, TRootState>(
110-
handler: PayloadlessActionHandler<TModuleState, TRootState>,
111-
namespace: string): PayloadlessDispatchAccessor<TModuleState, TRootState> {
94+
function dispatchNoPayload<TModuleState, TRootState, TResult>(
95+
handler: PayloadlessActionHandler<TModuleState, TRootState, TResult>,
96+
namespace: string): PayloadlessDispatchAccessor<TModuleState, TRootState, TResult> {
11297
const key = qualifyKey(handler, namespace);
11398
return (store) => {
114-
return store.dispatch(key, undefined, useRootNamespace);
99+
return <any>store.dispatch(key, undefined, useRootNamespace);
115100
};
116101
}
117102

src/tests/withModules/actions.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ describe("Given store with modules exposing actions", () => {
9595
});
9696
});
9797

98+
describe("when action returning promise which resolves to non-void value is dispatched in a module", () => {
99+
let actualResult: number;
100+
101+
beforeEach(async () => {
102+
actualResult = await basket.dispatchUpdateTotalAmount(store, 0.5);
103+
});
104+
105+
it("returns promise which resolves to the same value", () => {
106+
expect(actualResult).to.equal(40);
107+
});
108+
});
109+
98110
describe("when action which delegates work to other actions is dispatched in a module ", () => {
99111
beforeEach(async () => {
100112
await basket.dispatchSelectAvailablieItemsAndUpdateTotalAmount(store, 0.5);

src/tests/withModules/store/basket/basket.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ export const basket = {
4949
},
5050

5151
actions: {
52-
async updateTotalAmount(context: BasketContext, discount: number): Promise<void> {
52+
async updateTotalAmount(context: BasketContext, discount: number): Promise<number> {
5353
const totalBeforeDiscount = readTotalAmountWithoutDiscount(context);
5454

5555
// Imagine this is a server API call to compute the discounted value:
5656
await new Promise((resolve, _) => setTimeout(() => resolve(), 500));
5757
const totalAfterDiscount = totalBeforeDiscount * discount;
5858

5959
commitSetTotalAmount(context, totalAfterDiscount);
60+
61+
return totalAfterDiscount;
6062
},
6163

6264
async selectAvailableItems(context: BasketContext): Promise<void> {

src/tests/withoutModules/actions.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ describe("Given store without modules exposing actions", () => {
9595
});
9696
});
9797

98+
describe("when action returning promise which resolves to non-void value is dispatched", () => {
99+
let actualResult: number;
100+
101+
beforeEach(async () => {
102+
actualResult = await api.dispatchUpdateTotalAmount(store, 0.5);
103+
});
104+
105+
it("returns promise which resolves to the same value", () => {
106+
expect(actualResult).to.equal(40);
107+
});
108+
});
109+
98110
describe("when action which delegates work to other actions is dispatched ", () => {
99111
beforeEach(async () => {
100112
await api.dispatchSelectAvailablieItemsAndUpdateTotalAmount(store, 0.5);

src/tests/withoutModules/store/store.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ const storeOptions = {
5050
},
5151

5252
actions: {
53-
async updateTotalAmount(context: Context, discount: number): Promise<void> {
53+
async updateTotalAmount(context: Context, discount: number): Promise<number> {
5454
const totalBeforeDiscount = readTotalAmountWithoutDiscount(context);
5555

5656
// Imagine this is a server API call to compute the discounted value:
5757
await new Promise((resolve, _) => setTimeout(() => resolve(), 500));
5858
const totalAfterDiscount = totalBeforeDiscount * discount;
5959

6060
commitSetTotalAmount(context, totalAfterDiscount);
61+
62+
return totalAfterDiscount;
6163
},
6264

6365
async selectAvailableItems(context: Context): Promise<void> {

0 commit comments

Comments
 (0)