Skip to content

Commit e33a635

Browse files
committed
integrating legacy test
1 parent 09b525c commit e33a635

File tree

8 files changed

+98
-76
lines changed

8 files changed

+98
-76
lines changed

src/actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ function handleMutateActionMode(target:VuexModule, key:string, descriptor:Action
3939

4040
initializeActionsCache( target );
4141

42-
(target as VuexModuleInternalsPrototype).__actions__.push({
42+
(target as VuexModule & VuexModuleInternalsPrototype).__actions__.push({
4343
__name__: key,
4444
__type__: "mutate",
4545
})
4646

4747
}
4848

49-
function handleRawActionMode(target:any, key:string, descriptor:ActionDescriptor) {
49+
function handleRawActionMode(target:VuexModule, key:string, descriptor:ActionDescriptor) {
5050

5151
initializeActionsCache( target );
5252

53-
(target as VuexModuleInternalsPrototype).__actions__.push({
53+
(target as VuexModule & VuexModuleInternalsPrototype).__actions__.push({
5454
__name__: key,
5555
__type__: "raw",
5656
});

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { extractVuexModule, createModule } from "./module";
22
export { createSubModule } from "./submodule";
3-
export { createProxy } from "./proxy"
3+
export { createProxy, clearProxyCache } from "./proxy"
44
export { action, getRawActionContext } from "./actions";
55
export { mutation } from "./mutations";
66

src/interfaces.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Store } from "vuex";
2+
13
export interface VuexModuleOptions {
24
/*
35
* Takes a boolean or a namespacedPath.
@@ -13,6 +15,11 @@ export interface VuexModuleOptions {
1315
enableLocalActionSubscriptions ?:boolean | string;
1416
}
1517

18+
export interface VuexModuleAddons {
19+
$store :Store<any>
20+
}
21+
22+
export interface VuexModule extends VuexModuleAddons {}
1623
export class VuexModule {}
1724

1825
export type VuexModuleConstructor = typeof VuexModule & VuexModuleInternals;

src/module.legacy.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
import { VuexModuleConstructor, VuexModule, Map } from "./interfaces";
1+
import { VuexModuleConstructor, VuexModule, VuexModuleAddons, Map } from "./interfaces";
22
import { createModule, extractVuexModule } from "./module";
3-
import { createProxy } from './proxy';
3+
import { createProxy, clearProxyCache } from './proxy';
44
import { createSubModule } from './submodule';
55

6+
const defaultModuleOptions :ModuleOptions = {
7+
namespacedPath: "",
8+
target: "core",
9+
}
10+
611
export function Module({ namespacedPath = "", target = "core" as VuexModuleTarget } = defaultModuleOptions ) {
712

8-
return function( module :VuexModule ) :void {
13+
return function( module :unknown ) :void {
914

1015
const VuexClass = module as VuexModuleConstructor;
11-
16+
17+
VuexClass.prototype.__options__ = {
18+
namespaced: namespacedPath,
19+
target: target === "nuxt" ? target : undefined,
20+
}
21+
1222
const mod = createModule({
1323
target: VuexClass.prototype.__options__ && VuexClass.prototype.__options__.target,
14-
namespaced: VuexClass.prototype.__options__ && VuexClass.prototype.__options__.namespaced || false,
24+
namespaced: VuexClass.prototype.__options__ && VuexClass.prototype.__options__.namespaced,
1525
});
1626

1727
// Add all fields in mod prototype without replacing
@@ -21,12 +31,12 @@ export function Module({ namespacedPath = "", target = "core" as VuexModuleTarge
2131
//@ts-ignore
2232
VuexClass.prototype[ field ] = mod.prototype[ field ]
2333
}
24-
25-
34+
2635
}
2736

2837
}
2938

39+
export interface LegacyVuexModule extends VuexModuleAddons {}
3040
export class LegacyVuexModule {
3141

3242
static ExtractVuexModule( cls :typeof VuexModule ) {
@@ -35,23 +45,22 @@ export class LegacyVuexModule {
3545
return vxmodule[ cls.prototype.__namespacedPath__ ]
3646
}
3747

38-
static CreateProxy( cls :typeof VuexModule, $store :Map ) {
39-
return createProxy( cls, $store )
48+
static CreateProxy<T extends typeof VuexModule>( $store :Map, cls :T ) {
49+
return createProxy( $store, cls )
4050
}
4151

4252
static CreateSubModule( cls :typeof VuexModule ) {
4353
return createSubModule( cls );
4454
}
55+
56+
static ClearProxyCache( cls :typeof VuexModule ) {
57+
return clearProxyCache( cls );
58+
}
4559
}
4660

4761
export type VuexModuleTarget = "core" | "nuxt";
4862

4963
interface ModuleOptions {
5064
namespacedPath?: string,
5165
target?: VuexModuleTarget
52-
}
53-
54-
const defaultModuleOptions :ModuleOptions = {
55-
namespacedPath: "",
56-
target: "core",
5766
}

src/module.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export function extractVuexModule( cls :typeof VuexModule ) {
4141

4242
// Check if module has been cached,
4343
// and just return the cached version.
44-
if( VuexClass.prototype.__vuex_module_cache__ ) {
45-
return VuexClass.prototype.__vuex_module_cache__;
46-
}
44+
// if( VuexClass.prototype.__vuex_module_cache__ ) {
45+
// return VuexClass.prototype.__vuex_module_cache__;
46+
// }
4747

4848
// If not extract vuex module from class.
4949
const fromInstance = extractModulesFromInstance( VuexClass );
@@ -72,7 +72,7 @@ export function extractVuexModule( cls :typeof VuexModule ) {
7272

7373
}
7474

75-
function getNamespacedPath( cls :VuexModuleConstructor ) {
75+
export function getNamespacedPath( cls :VuexModuleConstructor ) {
7676

7777
if( cls.prototype.__options__ && cls.prototype.__options__.namespaced ) {
7878
switch( cls.prototype.__options__.namespaced ) {
@@ -113,27 +113,16 @@ function extractModulesFromInstance( cls :VuexModuleConstructor ) {
113113
continue;
114114
}
115115

116-
// // Check if field is an explicit mutation.
117-
// if( typeof instance[ field ] === "function" ) {
118-
// const mutation = ( state :any, payload :any ) => instance[ field ].call( state, payload );
119-
120-
// mutations[ field ] = mutation;
121-
122-
// continue;
123-
// }
124-
125-
126116
// If field is not a submodule, then it must be a state.
127-
// Check if the vuex module is targeting nuxt. if not define state as normal.
128-
if( moduleOptions.target === "nuxt" ) state[ field ] = () => instance[ field ];
129-
else state[ field ] = instance[ field ];
117+
state[ field ] = instance[ field ];
130118

131119
}
132120

133121
return {
134122
submodules,
135123
mutations,
136-
state,
124+
// Check if the vuex module is targeting nuxt return state as function. if not define state as normal.
125+
state: moduleOptions.target === "nuxt" ? () => state : state,
137126
}
138127
}
139128

@@ -173,6 +162,11 @@ function extractModulesFromPrototype( cls :VuexModuleConstructor ) {
173162
const action = function( context :any, payload :any ) {
174163
cls.prototype.__context_store__ = context;
175164
const proxy = createLocalProxy( cls, context );
165+
166+
if( proxy[ "$store" ] === undefined ) {
167+
Object.defineProperty( proxy, "$store", { value: context });
168+
}
169+
176170
return func.call( proxy, payload )
177171
}
178172

src/proxy.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { extractVuexModule } from "./module";
22
import { VuexModuleConstructor, Map, VuexModule, ProxyWatchers } from "./interfaces";
33

4-
export function createProxy<T extends typeof VuexModule>( cls :T, $store :any ) :ProxyWatchers & InstanceType<T> {
4+
5+
export function clearProxyCache<T extends typeof VuexModule>( cls :T ) {
6+
7+
}
8+
9+
export function createProxy<T extends typeof VuexModule>( $store :any, cls :T ) :ProxyWatchers & InstanceType<T> {
510
//@ts-ignore
611
const VuexClass = cls as VuexModuleConstructor;
712

813
// Check cache and return from cache if defined.
9-
if( VuexClass.prototype.__vuex_proxy_cache__ ) {
10-
return VuexClass.prototype.__vuex_proxy_cache__ as InstanceType<T> & ProxyWatchers;
11-
}
14+
// if( VuexClass.prototype.__vuex_proxy_cache__ ) {
15+
// return VuexClass.prototype.__vuex_proxy_cache__ as InstanceType<T> & ProxyWatchers;
16+
// }
1217

1318
const namespacedPath = VuexClass.prototype.__namespacedPath__ ? VuexClass.prototype.__namespacedPath__ + "/" : "";
1419

@@ -63,6 +68,7 @@ export function createProxy<T extends typeof VuexModule>( cls :T, $store :any )
6368

6469
if( typeof callback === "function" ) {
6570
return $store.subscribeAction(( action :any ) => {
71+
//@ts-ignore
6672
if( action.type === namespacedPath + field ) callback( action.payload )
6773
})
6874
}
@@ -101,12 +107,13 @@ export function createLocalProxy<T extends typeof VuexModule>( cls :T, $store :a
101107
}
102108

103109
export function _createProxy<T>(cls: T, $store: any, namespacedPath = "") {
110+
104111
//@ts-ignore
105112
const VuexClass = cls as VuexModuleConstructor;
106113
const proxy = {};
107-
const { state, mutations, actions, getters, modules } = extractVuexModule( VuexClass );
114+
const { state, mutations, actions, getters, modules } = extractVuexModule( VuexClass )[ VuexClass.prototype.__namespacedPath__ ];
108115

109-
createGettersAndMutationProxyFromState({ cls: VuexClass, proxy, state, $store, namespacedPath });
116+
createGettersAndMutationProxyFromState({ cls: VuexClass, proxy, state, $store, namespacedPath, maxDepth: 7 });
110117
createExplicitMutationsProxy({ cls: VuexClass, proxy, $store, namespacedPath });
111118
createGettersAndGetterMutationsProxy({ cls: VuexClass, mutations, getters, proxy, $store, namespacedPath });
112119
createActionProxy({ actions, proxy, $store, namespacedPath });
@@ -169,13 +176,6 @@ function createLocalSubscriberAction( cls :VuexModuleConstructor, $store :Map, n
169176

170177
}
171178

172-
173-
// function getSubscriptionStates() {
174-
175-
// const before =
176-
177-
// }
178-
179179
}
180180

181181
function createLocalSubscriber( cls :VuexModuleConstructor, $store :Map, namespacedPath :string ) {
@@ -259,12 +259,12 @@ function createSubModuleProxy( $store :Map, cls:VuexModuleConstructor, proxy :Ma
259259

260260
for( let field in modules ) {
261261
const subModuleClass = cls.prototype.__submodules_cache__[ field ];
262-
proxy[ field ] = createProxy( subModuleClass, $store );
262+
proxy[ field ] = createProxy( $store, subModuleClass );
263263
}
264264

265265
}
266266

267-
function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, namespacedPath = "", currentField = "" }: { cls: VuexModuleConstructor, proxy: Map; state: Map; $store: any; namespacedPath?: string; currentField?: string; }) {
267+
function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, namespacedPath = "", currentField = "", maxDepth = 7 }: { cls: VuexModuleConstructor, proxy: Map; state: Map; $store: any; namespacedPath?: string; currentField?: string; maxDepth: number}) {
268268
/**
269269
* 1. Go through all fields in the object and check the values of those fields.
270270
*
@@ -279,12 +279,12 @@ function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, nam
279279
*/
280280
for (let field in state) {
281281

282-
let value = state[field];
282+
let value = state[ field ];
283283

284284
if (currentField.length && !currentField.endsWith(".")) currentField += ".";
285285
const path = currentField + field;
286286

287-
if (typeof value !== "object") {
287+
if ( maxDepth === 0 || typeof value !== "object") {
288288
Object.defineProperty(proxy, field, {
289289
get: () => {
290290
// When creating local proxies getters doesn't exist on that context, so we have to account
@@ -305,13 +305,15 @@ function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, nam
305305
continue;
306306
}
307307

308-
proxy[field] = {};
308+
proxy[ field ] = {};
309+
309310
createGettersAndMutationProxyFromState({
310311
cls, proxy:
311312
proxy[field],
312313
state: value,
313314
$store, namespacedPath,
314-
currentField: currentField + field
315+
currentField: currentField + field,
316+
maxDepth: maxDepth - 1,
315317
});
316318

317319
}
@@ -332,23 +334,25 @@ function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy,
332334
const getterMutations = Object.keys( cls.prototype.__mutations_cache__.__setter_mutations__ );
333335
// If there are defined setter mutations that do not have a corresponding getter,
334336
// throw an error.
335-
if( $store.__internal_getter__ ) {
337+
if( $store && $store.__internal_getter__ ) {
336338
$store.__internal_mutator__ = mutations.__internal_mutator__;
337339
}
338340

339341
for( let field in getters ) {
340342

343+
if( $store === undefined ) continue;
344+
341345
const fieldHasGetterAndMutation = getterMutations.indexOf( field ) > -1;
342346
if( fieldHasGetterAndMutation ) {
343347

344348
Object.defineProperty( proxy, field, {
345-
get: () => {
349+
get: () => {
346350
if( $store.getters ) return $store.getters[ namespacedPath + field ]
347351
else return $store[ namespacedPath + field ];
348352
},
349353
set: ( payload :any ) => $store.commit( namespacedPath + field, payload ),
350354
})
351-
355+
352356
continue;
353357
}
354358

@@ -365,7 +369,10 @@ function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy,
365369

366370
function createActionProxy({ actions, proxy, $store, namespacedPath } :ActionProxyCreator) {
367371
for( let field in actions ) {
368-
proxy[ field ] = ( payload :any ) => $store.dispatch( namespacedPath + field, payload );
372+
if( $store === undefined ) continue;
373+
proxy[ field ] = function( payload :any ) {
374+
return $store.dispatch( namespacedPath + field, payload );
375+
}
369376
}
370377
}
371378

0 commit comments

Comments
 (0)