1+
2+ import { ActionContext , ActionTree , GetterTree , Module , MutationTree , Store } from "vuex"
3+
4+ const useRootNamespace = { root : true }
5+
6+ export type MutationHandler < S , P > = ( state : S , payload : P ) => void
7+ // export type PayloadlessMutationHandler<S> = (state: S) => void
8+
9+ export type ActionHandler < S , R , P , T > = ( context : BareActionContext < S , R > , payload : P ) => Promise < T >
10+ // export type PayloadlessActionHandler<S, R, T> = (context: BareActionContext<S, R>) => Promise<T>
11+ // export type VoidActionHandler<S, R, P> = (context: BareActionContext<S, R>, payload: P) => void
12+ // export type VoidPayloadlessActionHandler<S, R> = (context: BareActionContext<S, R>) => void
13+ export type GetterHandler < S , R , T > = ( state : S , rootState : R ) => T
14+
15+ export interface BareActionContext < S , R >
16+ {
17+ state : S ;
18+ rootState : R ;
19+ }
20+
21+ export class ModuleBuilder < S , R > {
22+ private store : Store < R >
23+
24+ private getters : GetterTree < S , R > = { }
25+ private mutations : MutationTree < S > = { }
26+ private actions : ActionTree < S , R > = { }
27+
28+ constructor ( private namespace : string , private state : S ) { }
29+
30+ provideStore ( store : Store < R > )
31+ {
32+ this . store = store
33+ }
34+
35+ commit < P > ( handler : MutationHandler < S , void > ) : ( ) => void
36+ commit < P > ( handler : MutationHandler < S , P > ) : ( payload : P ) => void
37+ commit < P > ( handler : MutationHandler < S , P > )
38+ {
39+ const key = qualifyKey ( handler , this . namespace )
40+ return ( ( payload : P ) => this . store . commit ( key , payload , useRootNamespace ) ) as any
41+ }
42+
43+ dispatch < P , T > ( handler : ActionHandler < S , R , void , void > ) : ( ) => Promise < void >
44+ dispatch < P , T > ( handler : ActionHandler < S , R , P , void > ) : ( payload : P ) => Promise < void >
45+ dispatch < P , T > ( handler : ActionHandler < S , R , void , T > ) : ( ) => Promise < T >
46+ dispatch < P , T > ( handler : ActionHandler < S , R , P , T > ) : ( payload : P ) => Promise < T >
47+ dispatch < P , T > ( handler : any ) : any
48+ {
49+ const key = qualifyKey ( handler , this . namespace )
50+ return ( payload : P ) => this . store . dispatch ( key , payload , useRootNamespace )
51+ }
52+
53+ read < T > ( handler : GetterHandler < S , R , T > ) : ( ) => T
54+ read < T > ( handler : GetterHandler < S , R , T > , name : string ) : ( ) => T
55+ read < T > ( handler : GetterHandler < S , R , T > , name ?: string ) : ( ) => T
56+ {
57+ const key = qualifyKey ( handler , this . namespace , name )
58+ return ( ) => this . store . getters [ key ] as T
59+ }
60+
61+ toVuexModule ( ) : Module < S , R >
62+ {
63+ return {
64+ namespaced : true ,
65+ state : this . state ,
66+ getters : this . getters ,
67+ mutations : this . mutations ,
68+ actions : this . actions
69+ }
70+ }
71+ }
72+
73+ function qualifyKey ( handler : Function , namespace : string | undefined , name ?: string )
74+ {
75+ const key : string = name || handler . name
76+ if ( ! key )
77+ {
78+ throw new Error ( `Vuex handler functions must not be anonymous. Possible causes: fat-arrow functions, uglify` )
79+ }
80+ return namespace ? `${ namespace } /${ key } ` : key
81+ }
0 commit comments