-
Notifications
You must be signed in to change notification settings - Fork 22
/
module.ts
298 lines (228 loc) · 10 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//@ts-ignore
import getDescriptors from "object.getownpropertydescriptors";
import { VuexModuleOptions, VuexModuleConstructor, DictionaryField, VuexModule, VuexObject, Map } from "./interfaces";
import { isFieldASubModule, extractVuexSubModule } from "./submodule";
import { createLocalProxy } from './proxy';
import { toCamelCase } from "./utils";
import { internalAction } from "./actions";
import { internalMutator } from "./mutations";
import { internalGetter } from "./getters";
export function createModule( options ?:VuexModuleOptions ) {
/**
* We do it like this because we don't want intelissense to pick up the
* options variable as it is an internal variable.
*/
//@ts-ignore
const vuexModule = function(){} as VuexModuleConstructor;
vuexModule.prototype.__options__ = options;
vuexModule.With = defineWithExtension;
return vuexModule as typeof VuexModule;
}
function defineWithExtension( this:typeof VuexModule & VuexModuleConstructor, options?: VuexModuleOptions ) {
// Get the old vuex options
const oldOptions = this.prototype.__options__ = {};
// create a new module constructor
//@ts-ignore
const newVuexModule = function(){} as VuexModuleConstructor;
newVuexModule.prototype.__options__ = {};
// assign all the old options to the new module constructor
Object.assign( newVuexModule.prototype.__options__, oldOptions );
// If there are no new vuex options return as is.
if( options === undefined ) return newVuexModule as typeof VuexModule;
// Assign or new available vuex options to the new vuex module
const prototypeOptions = newVuexModule.prototype.__options__ || {};
if( options.namespaced ) prototypeOptions.namespaced = options.namespaced;
if( options.strict ) prototypeOptions.strict = options.strict;
if( options.target ) prototypeOptions.target = options.target;
if( options.enableLocalWatchers ) prototypeOptions.enableLocalWatchers = options.enableLocalWatchers;
return newVuexModule as typeof VuexModule;
}
function initializeModuleInternals( cls: VuexModuleConstructor ) {
cls.prototype.__namespacedPath__ = "";
cls.prototype.__vuex_module_cache__ = undefined;
cls.prototype.__vuex_proxy_cache__ = undefined;
cls.prototype.__vuex_local_proxy_cache__ = undefined;
cls.prototype.__submodules_cache__ = {};
cls.prototype.__context_store__ = {};
cls.prototype.__watch__ = {};
cls.prototype.__explicit_getter_names__ = [];
}
export function extractVuexModule( cls :typeof VuexModule ) {
const VuexClass = cls as VuexModuleConstructor;
// Check if module has been cached,
// and just return the cached version.
if( VuexClass.prototype.__vuex_module_cache__ ) {
return VuexClass.prototype.__vuex_module_cache__;
}
initializeModuleInternals( VuexClass );
// If not extract vuex module from class.
const fromInstance = extractModulesFromInstance( VuexClass );
const fromPrototype = extractModulesFromPrototype( VuexClass );
// Cache explicit mutations and getter mutations.
VuexClass.prototype.__mutations_cache__ = {
__explicit_mutations__: fromPrototype.mutations.explicitMutations,
__setter_mutations__: fromPrototype.mutations.setterMutations,
}
const className = VuexClass.name.toLowerCase();
const vuexModule :VuexObject = {
namespaced: VuexClass.prototype.__options__ && VuexClass.prototype.__options__.namespaced ? true : false,
state: fromInstance.state,
mutations: { ...fromPrototype.mutations.explicitMutations, ...fromPrototype.mutations.setterMutations, [`__${className}_internal_mutator__`]: internalMutator },
getters: { ...fromPrototype.getters, ...fromInstance.getters , [ `__${className}_internal_getter__`]: internalGetter },
actions: { ...fromPrototype.actions, [`__${className}_internal_action__`]: internalAction },
modules: fromInstance.submodules,
};
// Cache the vuex module on the class.
const path = getNamespacedPath( VuexClass ) || toCamelCase( VuexClass.name );
const rtn = { [ path ]: vuexModule }
VuexClass.prototype.__vuex_module_cache__ = rtn;
return rtn;
}
export function getNamespacedPath( cls :VuexModuleConstructor ) {
const namespaced = cls.prototype.__options__ && cls.prototype.__options__.namespaced;
if( namespaced ) {
const namePaths = namespaced.split("/");
cls.prototype.__namespacedPath__ = namePaths[namePaths.length - 1] || namePaths[namePaths.length - 2];
}
return cls.prototype.__namespacedPath__;
}
function extractModulesFromInstance( cls :VuexModuleConstructor ) {
const instance = new cls() as InstanceType<VuexModuleConstructor> & Map;
const classFields = Object.getOwnPropertyNames( instance );
const state :Map = {};
const mutations :Map = {};
const submodules :Map = {};
const submodulesCache = cls.prototype.__submodules_cache__;
const moduleOptions = cls.prototype.__options__ || {};
for( let field of classFields ) {
// Check if field is a submodule.
const fieldIsSubModule = isFieldASubModule( instance, field );
if( fieldIsSubModule ) {
// Cache submodule class
submodulesCache[ field ] = instance[ field ][ "__submodule_class__" ]
const submodule = extractVuexSubModule( instance, field );
submodules[ field ] = submodule;
continue;
}
// If field is not a submodule, then it must be a state.
state[ field ] = instance[ field ];
}
return {
submodules,
mutations,
getters: extractDecoratorGetterNames( cls.prototype.__decorator_getter_names__ ),
// Check if the vuex module is targeting nuxt return state as function. if not define state as normal.
state: moduleOptions.target === "nuxt" ? () => state : state,
}
}
function extractModulesFromPrototype( cls :VuexModuleConstructor ) {
const setterMutations :Record<DictionaryField, any> = {};
const explicitMutations :Record<DictionaryField, any> = {};
const actions :Record<DictionaryField, any> = {};
const getters :Record<DictionaryField, any> = {};
const descriptors :PropertyDescriptorMap = getDescriptors( cls.prototype );
const gettersList :string[] = Object.keys( descriptors ).filter( field => descriptors[ field ].get );
const explicitMutationNames :string[] = cls.prototype.__explicit_mutations_names__ || [];
const actionNames = cls.prototype.__actions__ || [];
for( let field in descriptors ) {
// Ignore the constructor and module interals.
const fieldIsInternal = (
field === "constructor" ||
field === "__options__" ||
field === "__vuex_module_cache__" ||
field === "__vuex_proxy_cache__" ||
field === "__mutations_cache__" ||
field === "__explicit_mutations__" ||
field === "__getter_mutations__"
);
if( fieldIsInternal ) continue;
const descriptor = descriptors[ field ];
const actionType = (typeof descriptor.value === "function") && actionNames.find( action => action.__name__ === field );
// If prototype field is an mutate action
if( actionType && actionType.__type__ === "mutate" ) {
const func = descriptor.value as Function
const action = function( context :any, payload :any ) {
cls.prototype.__context_store__ = context;
const proxy = createLocalProxy( cls, context );
if( proxy[ "$store" ] === undefined ) {
// @ts-ignore
Object.defineProperty( proxy, "$store", { value: this });
}
return func.call( proxy, payload )
}
actions[ field ] = action;
continue;
}
// if prototype field is a raw action
if( actionType && actionType.__type__ === "raw" ) {
const func = descriptor.value as Function;
const action = ( context :any, payload :any ) => func.call( context, payload );
actions[ field ] = action;
continue;
}
// If prototype field is an explicit mutation
const fieldIsExplicitMutation = (
typeof descriptor.value === "function" &&
explicitMutationNames.indexOf( field ) > -1
);
if( fieldIsExplicitMutation ) {
const mutation = ( state :any, payload :any ) => descriptor.value.call( state, payload );
explicitMutations[ field ] = mutation;
continue;
}
// If the prototype field has a getter.
if( descriptor.get ) {
const getter = ( state :any, context :Map ) => {
const proxy = createLocalProxy( cls, context )
return descriptor.get!.call( proxy )
}
getters[ field ] = getter;
}
// if the prototype field has setter mutation.
if( descriptor.set ) {
const mutation = (state :any, payload :any) => descriptor.set!.call( state, payload );
// Before we push a setter mutation We must verify
// if that mutation has a corresponding getter.
// If not, we dissallow it.
const mutationHasGetter = gettersList.indexOf( field ) > -1;
if( mutationHasGetter === false ) {
// Throw an Error.
throw new Error(
`\nImproper Use of Setter Mutations:\n` +
`at >>\n` +
`set ${ field }( payload ) {\n` +
`\t...\n` +
`}\n` +
`\n` +
`Setter mutations should only be used if there is a corresponding getter defined.\n` +
`\n` +
`Either define a corresponding getter for this setter mutation or,\n` +
`Define them as an explicit mutation using function assignment.\n` +
`Example:\n` +
`--------------------\n` +
`${ field } = ( payload ) => {\n` +
` ...\n` +
`}`
)
}
setterMutations[ field ] = mutation;
}
// Stash getters list. To be used later when creating $watch functionality.
cls.prototype.__explicit_getter_names__ = gettersList;
}
return {
actions,
mutations: {
explicitMutations,
setterMutations,
},
getters
}
}
function extractDecoratorGetterNames( names :string[] = [] ) {
const decorator :Map = {};
for( let name of names ) {
decorator[ name ] = new Function("state", `return state.${name}`);
}
return decorator;
}