@@ -6,49 +6,98 @@ export function Handler(target: any, key: string) {
6
6
target [ key ] . _vuexKey = key ;
7
7
}
8
8
9
- export type MutationHandler < TModuleState , TPayload > =
10
- ( state : TModuleState , payload : TPayload ) => void ;
11
- export type PayloadlessMutationHandler < TModuleState > =
12
- ( state : TModuleState ) => void ;
9
+ /**
10
+ * Vuex getter handler specified in Vuex options.
11
+ */
12
+ export type GetterHandler < TModuleState , TRootState , TResult > =
13
+ ( state : TModuleState , rootState : TRootState ) => TResult ;
13
14
14
- export type ActionHandler < TModuleState , TRootState , TPayload , TResult > =
15
+ /**
16
+ * Vuex action handler which takes payload as specified in Vuex options.
17
+ */
18
+ export type ActionHandlerWithPayload < TModuleState , TRootState , TPayload , TResult > =
15
19
( injectee : ActionContext < TModuleState , TRootState > , payload : TPayload ) => void | Promise < TResult > ;
16
- export type PayloadlessActionHandler < TModuleState , TRootState , TResult > =
20
+ /**
21
+ * Vuex action handler which does not take payload as specified in Vuex options.
22
+ */
23
+ export type ActionHandlerNoPayload < TModuleState , TRootState , TResult > =
17
24
( injectee : ActionContext < TModuleState , TRootState > ) => void | Promise < TResult > ;
18
25
19
- export type GetterHandler < TModuleState , TRootState , TResult > =
20
- ( state : TModuleState , rootState : TRootState ) => TResult ;
26
+ /**
27
+ * Vuex mutation handler which takes payload as specified in Vuex options.
28
+ */
29
+ export type MutationHandlerWithPayload < TModuleState , TPayload > =
30
+ ( state : TModuleState , payload : TPayload ) => void ;
31
+ /**
32
+ * Vuex mutation handler which does not take payload as specified in Vuex options.
33
+ */
34
+ export type MutationHandlerNoPayload < TModuleState > =
35
+ ( state : TModuleState ) => void ;
21
36
37
+ /**
38
+ * Function which gets value of a concrete Vuex getter.
39
+ */
22
40
export type GetAccessor < TModuleState , TRootState , TResult > =
23
41
( store : Store < TRootState > | ActionContext < TModuleState , TRootState > ) => TResult ;
24
42
25
- export type DispatchAccessor < TModuleState , TRootState , TPayload , TResult > =
43
+ /**
44
+ * Function which dispatches a concrete Vuex action with payload.
45
+ */
46
+ export type DispatchAccessorWithPayload < TModuleState , TRootState , TPayload , TResult > =
26
47
( store : Store < TRootState > | ActionContext < TModuleState , TRootState > ,
27
48
payload : TPayload ) => Promise < TResult > ;
28
- export type PayloadlessDispatchAccessor < TModuleState , TRootState , TResult > =
49
+ /**
50
+ * Function which dispatches a concrete Vuex action without payload.
51
+ */
52
+ export type DispatchAccessorNoPayload < TModuleState , TRootState , TResult > =
29
53
( store : Store < TRootState > | ActionContext < TModuleState , TRootState > ) => Promise < TResult > ;
30
54
31
- export type CommitAccessor < TModuleState , TRootState , TPayload > =
55
+ /**
56
+ * Function which commits a concrete Vuex mutation with payload.
57
+ */
58
+ export type CommitAccessorWithPayload < TModuleState , TRootState , TPayload > =
32
59
( store : Store < TRootState > | ActionContext < TModuleState , TRootState > ,
33
60
payload : TPayload ) => void ;
34
- export type PayloadlessCommitAccessor < TModuleState , TRootState > =
61
+ /**
62
+ * Function which commits a concrete Vuex mutation without payload.
63
+ */
64
+ export type CommitAccessorNoPayload < TModuleState , TRootState > =
35
65
( store : Store < TRootState > | ActionContext < TModuleState , TRootState > ) => void ;
36
66
37
67
export interface StoreAccessors < TModuleState , TRootState > {
68
+ /**
69
+ * Returns a function committing mutations directed to the specified mutation handler.
70
+ * This overload is for handlers which do not expect payload.
71
+ */
72
+ commit (
73
+ handler : MutationHandlerNoPayload < TModuleState > ) :
74
+ CommitAccessorNoPayload < TModuleState , TRootState > ;
75
+ /**
76
+ * Returns a function committing mutations directed to the specified mutation handler.
77
+ * This overload is for handlers which expect payload.
78
+ */
38
79
commit < TPayload > (
39
- handler : MutationHandler < TModuleState , TPayload > ) :
40
- CommitAccessor < TModuleState , TRootState , TPayload > ;
41
- commitNoPayload (
42
- handler : PayloadlessMutationHandler < TModuleState > ) :
43
- PayloadlessCommitAccessor < TModuleState , TRootState > ;
44
-
80
+ handler : MutationHandlerWithPayload < TModuleState , TPayload > ) :
81
+ CommitAccessorWithPayload < TModuleState , TRootState , TPayload > ;
82
+
83
+ /**
84
+ * Returns a function dispatching actions directed to the specified action handler.
85
+ * This overload is for handlers which do not expect payload.
86
+ */
87
+ dispatch < TResult > (
88
+ handler : ActionHandlerNoPayload < TModuleState , TRootState , TResult > ) :
89
+ DispatchAccessorNoPayload < TModuleState , TRootState , TResult > ;
90
+ /**
91
+ * Returns a function dispatching actions directed to the specified action handler.
92
+ * This overload is for handlers which expect payload.
93
+ */
45
94
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 > ;
95
+ handler : ActionHandlerWithPayload < TModuleState , TRootState , TPayload , TResult > ) :
96
+ DispatchAccessorWithPayload < TModuleState , TRootState , TPayload , TResult > ;
51
97
98
+ /**
99
+ * Returns a function returning value of the specified getter.
100
+ */
52
101
read < TResult > (
53
102
handler : GetterHandler < TModuleState , TRootState , TResult > ) :
54
103
GetAccessor < TModuleState , TRootState , TResult > ;
@@ -57,64 +106,26 @@ export interface StoreAccessors<TModuleState, TRootState> {
57
106
export function getStoreAccessors < TModuleState , TRootState > (
58
107
namespace : string ) : StoreAccessors < TModuleState , TRootState > {
59
108
return {
60
- commit : < TPayload > ( handler : MutationHandler < TModuleState , TPayload > ) =>
61
- commit ( handler , namespace ) ,
62
- commitNoPayload : ( handler : PayloadlessMutationHandler < TModuleState > ) =>
63
- commitNoPayload ( handler , namespace ) ,
64
-
65
- dispatch : < TPayload , TResult > ( handler : ActionHandler < TModuleState , TRootState , TPayload , TResult > ) =>
66
- dispatch ( handler , namespace ) ,
67
- dispatchNoPayload : < TResult > ( handler : PayloadlessActionHandler < TModuleState , TRootState , TResult > ) =>
68
- dispatchNoPayload ( handler , namespace ) ,
69
- read : < TResult > ( handler : GetterHandler < TModuleState , TRootState , TResult > ) =>
70
- read ( handler , namespace ) ,
71
- } ;
72
- }
73
-
74
- function read < TModuleState , TRootState , TResult > (
75
- handler : GetterHandler < TModuleState , TRootState , TResult > ,
76
- namespace : string ) : GetAccessor < TModuleState , TRootState , TResult > {
77
- const key = qualifyKey ( handler , namespace ) ;
78
- return ( store : any ) => {
79
- return store . rootGetters
80
- ? < TResult > store . rootGetters [ key ] // ActionContext
81
- : < TResult > store . getters [ key ] ; // Store
82
- } ;
83
- }
84
-
85
- function dispatch < TModuleState , TRootState , TPayload , TResult > (
86
- handler : ActionHandler < TModuleState , TRootState , TPayload , TResult > ,
87
- namespace : string ) : DispatchAccessor < TModuleState , TRootState , TPayload , TResult > {
88
- const key = qualifyKey ( handler , namespace ) ;
89
- return ( store , payload ) => {
90
- return < any > store . dispatch ( key , payload , useRootNamespace ) ;
91
- } ;
92
- }
93
-
94
- function dispatchNoPayload < TModuleState , TRootState , TResult > (
95
- handler : PayloadlessActionHandler < TModuleState , TRootState , TResult > ,
96
- namespace : string ) : PayloadlessDispatchAccessor < TModuleState , TRootState , TResult > {
97
- const key = qualifyKey ( handler , namespace ) ;
98
- return ( store ) => {
99
- return < any > store . dispatch ( key , undefined , useRootNamespace ) ;
100
- } ;
101
- }
102
-
103
- function commit < TModuleState , TRootState , TPayload > (
104
- handler : MutationHandler < TModuleState , TPayload > ,
105
- namespace : string ) : CommitAccessor < TModuleState , TRootState , TPayload > {
106
- const key = qualifyKey ( handler , namespace ) ;
107
- return ( store , payload ) => {
108
- store . commit ( key , payload , useRootNamespace ) ;
109
+ commit : ( handler : Function ) => createAccessor ( "commit" , handler , namespace ) ,
110
+ dispatch : ( handler : Function ) => createAccessor ( "dispatch" , handler , namespace ) ,
111
+ read : ( handler : Function ) => {
112
+ const key = qualifyKey ( handler , namespace ) ;
113
+ return ( store : any ) => {
114
+ return store . rootGetters
115
+ ? store . rootGetters [ key ] // ActionContext
116
+ : store . getters [ key ] ; // Store
117
+ } ;
118
+ } ,
109
119
} ;
110
120
}
111
121
112
- function commitNoPayload < TModuleState , TRootState > (
113
- handler : PayloadlessMutationHandler < TModuleState > ,
114
- namespace : string ) : PayloadlessCommitAccessor < TModuleState , TRootState > {
122
+ function createAccessor (
123
+ operation : string ,
124
+ handler : Function ,
125
+ namespace : string ) : any {
115
126
const key = qualifyKey ( handler , namespace ) ;
116
- return ( store ) => {
117
- store . commit ( key , undefined , useRootNamespace ) ;
127
+ return ( store : any , payload : any ) => {
128
+ return store [ operation ] ( key , payload , useRootNamespace ) ;
118
129
} ;
119
130
}
120
131
0 commit comments