@@ -7,34 +7,32 @@ export type MutationHandler<S, P> = (state: S, payload: P) => void
7
7
export type ActionHandler < S , R , P , T > = ( context : BareActionContext < S , R > , payload : P ) => Promise < T > | T
8
8
export type GetterHandler < S , R , T > = ( state : S , rootState : R ) => T
9
9
10
+ interface RootStore < R > extends Store < R > { rootGetters ?: any }
11
+
10
12
export interface BareActionContext < S , R >
11
13
{
12
- state : S ;
13
- rootState : R ;
14
+ state : S
15
+ rootState : R
14
16
}
15
17
16
18
export class ModuleBuilder < S , R > {
17
- private store : Store < R >
19
+ private store : RootStore < R >
18
20
19
21
private getters : GetterTree < S , R > = { }
20
22
private mutations : MutationTree < S > = { }
21
23
private actions : ActionTree < S , R > = { }
22
24
23
25
constructor ( private namespace : string , private state : S ) { }
24
26
25
- provideStore ( store : Store < R > )
26
- {
27
- this . store = store
28
- }
29
-
30
27
commit < P > ( handler : MutationHandler < S , void > ) : ( ) => void
31
28
commit < P > ( handler : MutationHandler < S , P > ) : ( payload : P ) => void
32
29
commit < P > ( handler : MutationHandler < S , void > , name : string ) : ( ) => void
33
30
commit < P > ( handler : MutationHandler < S , P > , name : string ) : ( payload : P ) => void
34
31
commit < P > ( handler : MutationHandler < S , P > , name ?: string )
35
32
{
36
- const key = qualifyKey ( handler , this . namespace , name )
37
- return ( ( payload : P ) => this . store . commit ( key , payload , useRootNamespace ) ) as any
33
+ const { key, namespacedKey } = qualifyKey ( handler , this . namespace , name )
34
+ this . mutations [ key ] = handler
35
+ return ( ( payload : P ) => this . store . commit ( namespacedKey , payload , useRootNamespace ) ) as any
38
36
}
39
37
40
38
dispatch < P , T > ( handler : ActionHandler < S , R , void , void > ) : ( ) => Promise < void >
@@ -47,16 +45,30 @@ export class ModuleBuilder<S, R> {
47
45
dispatch < P , T > ( handler : ActionHandler < S , R , P , T > , name : string ) : ( payload : P ) => Promise < T >
48
46
dispatch < P , T > ( handler : any , name ?: string ) : any
49
47
{
50
- const key = qualifyKey ( handler , this . namespace , name )
51
- return ( payload : P ) => this . store . dispatch ( key , payload , useRootNamespace )
48
+ const { key, namespacedKey } = qualifyKey ( handler , this . namespace , name )
49
+ this . actions [ key ] = handler
50
+ return ( payload : P ) => this . store . dispatch ( namespacedKey , payload , useRootNamespace )
52
51
}
53
52
54
53
read < T > ( handler : GetterHandler < S , R , T > ) : ( ) => T
55
54
read < T > ( handler : GetterHandler < S , R , T > , name : string ) : ( ) => T
56
55
read < T > ( handler : GetterHandler < S , R , T > , name ?: string ) : ( ) => T
57
56
{
58
- const key = qualifyKey ( handler , this . namespace , name )
59
- return ( ) => this . store . getters [ key ] as T
57
+ const { key, namespacedKey } = qualifyKey ( handler , this . namespace , name )
58
+ this . getters [ key ] = handler
59
+ return ( ) =>
60
+ {
61
+ if ( this . store . rootGetters )
62
+ {
63
+ return this . store . rootGetters [ namespacedKey ] as T
64
+ }
65
+ return this . store . getters [ namespacedKey ] as T
66
+ }
67
+ }
68
+
69
+ provideStore ( ) : ( store : Store < R > ) => void
70
+ {
71
+ return ( store ) => this . store = store
60
72
}
61
73
62
74
toVuexModule ( ) : Module < S , R >
@@ -71,12 +83,12 @@ export class ModuleBuilder<S, R> {
71
83
}
72
84
}
73
85
74
- function qualifyKey ( handler : Function , namespace : string | undefined , name ?: string )
86
+ function qualifyKey ( handler : Function , namespace : string | undefined , name ?: string ) : { key : string , namespacedKey : string }
75
87
{
76
88
const key : string = name || handler . name
77
89
if ( ! key )
78
90
{
79
91
throw new Error ( `Vuex handler functions must not be anonymous. Possible causes: fat-arrow functions, uglify` )
80
92
}
81
- return namespace ? `${ namespace } /${ key } ` : key
93
+ return namespace ? { key , namespacedKey : `${ namespace } /${ key } ` } : { key, namespacedKey : key }
82
94
}
0 commit comments