Skip to content

Commit c46e923

Browse files
committed
About to fix __internal_getter__ issues
1 parent d6b1ae9 commit c46e923

File tree

8 files changed

+49
-18
lines changed

8 files changed

+49
-18
lines changed

src/getters.legacy.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33
* The getter decorator exists just for backward compatibility.
44
* Not doing anything.
55
*/
6-
export const getter = (target:any, propertyKey:string) => undefined
6+
export const getter = (target:any, propertyKey:string) => {
7+
if( target.__decorator_getter_names__ === undefined ) {
8+
target.__decorator_getter_names__ = [ propertyKey ];
9+
}
10+
else target.__decorator_getter_names__.push( propertyKey );
11+
}

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface VuexModuleInternalsPrototype {
4141
__setter_mutations__: {},
4242
};
4343
__explicit_getter_names__ :string[];
44+
__decorator_getter_names__ :string[];
4445
__explicit_mutations_names__: string[],
4546
__actions__: {
4647
__name__ :string,

src/module.legacy.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { VuexModuleConstructor, VuexModule, VuexModuleAddons, Map } from "./inte
22
import { createModule, extractVuexModule } from "./module";
33
import { createProxy, clearProxyCache } from './proxy';
44
import { createSubModule } from './submodule';
5+
import { getClassPath, toCamelCase } from "./utils";
56

67
const defaultModuleOptions :ModuleOptions = {
78
namespacedPath: "",
@@ -44,15 +45,16 @@ export class LegacyVuexModule {
4445
const VuexClass = cls as VuexModule & VuexModuleConstructor;
4546

4647
const vxmodule = extractVuexModule( VuexClass );
47-
48-
return vxmodule[ VuexClass.prototype.__namespacedPath__ ];
48+
const path = getClassPath( VuexClass.prototype.__namespacedPath__ ) || toCamelCase( VuexClass.name );
49+
console.log( "Module", vxmodule, "Path", path );
50+
return vxmodule[ path ];
4951
}
5052

5153
static CreateProxy<T extends typeof VuexModule>( $store :Map, cls :T ) {
5254
return createProxy( $store, cls );
5355
}
5456

55-
static CreateSubModule( cls :typeof VuexModule ) {
57+
static CreateSubModule<T extends typeof VuexModule>( cls :T ) {
5658
return createSubModule( cls );
5759
}
5860

src/module.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function createModule( options ?:VuexModuleOptions ) {
3030
(VuexModule as VuexModuleConstructor).prototype.__actions__ = [];
3131
(VuexModule as VuexModuleConstructor).prototype.__watch__ = {};
3232
(VuexModule as VuexModuleConstructor).prototype.__explicit_getter_names__ = [];
33+
(VuexModule as VuexModuleConstructor).prototype.__decorator_getter_names__ = [];
3334

3435
return VuexModule;
3536

@@ -57,17 +58,18 @@ export function extractVuexModule( cls :typeof VuexModule ) {
5758
namespaced: VuexClass.prototype.__options__ && VuexClass.prototype.__options__.namespaced ? true : false,
5859
state: fromInstance.state,
5960
mutations: { ...fromPrototype.mutations.explicitMutations, ...fromPrototype.mutations.setterMutations, __internal_mutator__: internalMutator },
60-
getters: { ...fromPrototype.getters, __internal_getter__: internalGetter },
61+
getters: { ...fromPrototype.getters, ...fromInstance.getters , __internal_getter__: internalGetter },
6162
actions: { ...fromPrototype.actions, __internal_action__: internalAction },
6263
modules: fromInstance.submodules,
6364
};
6465

65-
// Cache the vuex module on the class.
66-
const className = getNamespacedPath( VuexClass );
6766

68-
const rtn = { [ className ]: vuexModule }
69-
VuexClass.prototype.__vuex_module_cache__ = rtn;
67+
// Cache the vuex module on the class.
68+
const path = getNamespacedPath( VuexClass ) || toCamelCase( VuexClass.name );
7069

70+
const rtn = { [ path ]: vuexModule }
71+
VuexClass.prototype.__vuex_module_cache__ = rtn;
72+
7173
return rtn;
7274

7375
}
@@ -121,6 +123,7 @@ function extractModulesFromInstance( cls :VuexModuleConstructor ) {
121123
return {
122124
submodules,
123125
mutations,
126+
getters: extractDecoratorGetterNames( cls.prototype.__decorator_getter_names__ ),
124127
// Check if the vuex module is targeting nuxt return state as function. if not define state as normal.
125128
state: moduleOptions.target === "nuxt" ? () => state : state,
126129
}
@@ -256,4 +259,12 @@ function extractModulesFromPrototype( cls :VuexModuleConstructor ) {
256259
getters
257260
}
258261

262+
}
263+
264+
function extractDecoratorGetterNames( names :string[] ) {
265+
const decorator = {};
266+
for( let name of names ) {
267+
decorator[ name ] = new Function("state", `return state.${name}`);
268+
}
269+
return decorator;
259270
}

src/proxy.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { extractVuexModule } from "./module";
22
import { VuexModuleConstructor, Map, VuexModule, ProxyWatchers } from "./interfaces";
3+
import { getClassPath, toCamelCase } from "./utils";
34

45

56
export function clearProxyCache<T extends typeof VuexModule>( cls :T ) {
@@ -114,7 +115,9 @@ export function _createProxy<T>(cls: T, $store: any, namespacedPath = "") {
114115
//@ts-ignore
115116
const VuexClass = cls as VuexModuleConstructor;
116117
const proxy = {};
117-
const { state, mutations, actions, getters, modules } = extractVuexModule( VuexClass )[ VuexClass.prototype.__namespacedPath__ ];
118+
119+
const classPath = getClassPath( VuexClass.prototype.__namespacedPath__ ) || toCamelCase( VuexClass.name );
120+
const { state, mutations, actions, getters, modules } = extractVuexModule( VuexClass )[ classPath ];
118121

119122
createGettersAndMutationProxyFromState({ cls: VuexClass, proxy, state, $store, namespacedPath, maxDepth: 7 });
120123
createExplicitMutationsProxy( VuexClass, proxy, $store, namespacedPath );
@@ -261,7 +264,8 @@ function createLocalWatchers( cls :VuexModuleConstructor, $store :Map, namespace
261264
function createSubModuleProxy( $store :Map, cls:VuexModuleConstructor, proxy :Map, modules :Map ) {
262265
const store = cls.prototype.__store_cache__ || $store;
263266
for( let field in modules ) {
264-
const subModuleClass = cls.prototype.__submodules_cache__[ field ];
267+
const subModuleClass = cls.prototype.__submodules_cache__[ field ] as VuexModuleConstructor;
268+
subModuleClass.prototype.__namespacedPath__ = cls.prototype.__namespacedPath__ + "/" + subModuleClass.prototype.__namespacedPath__;
265269
proxy[ field ] = createProxy( store, subModuleClass );
266270
}
267271

@@ -365,6 +369,8 @@ function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy,
365369
}
366370

367371
// The field has only a getter.
372+
if( proxy[ field ] ) continue;
373+
368374
Object.defineProperty( proxy, field, {
369375
get: () => {
370376
if( $store.getters ) return $store.getters[ namespacedPath + field ];
@@ -391,7 +397,6 @@ function runSetterCheck( cls :VuexModuleConstructor, getters :Map ) {
391397
// if there are setters defined that are not in getters.
392398
// throw an error.
393399
const setterMutations = cls.prototype.__mutations_cache__.__setter_mutations__;
394-
console.log( "Setter Mutations", cls.name, setterMutations );
395400
for( let field in setterMutations ) {
396401
const setterIsNotInGetters = Object.keys( getters ).indexOf( field ) < 0;
397402
if( setterIsNotInGetters ) {

src/submodule.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { VuexModule, SubModuleType, Map, VuexObject } from "./interfaces";
1+
import { VuexModule, SubModuleType, Map, VuexObject, VuexModuleConstructor } from "./interfaces";
22
import { extractVuexModule } from "./module";
3-
import { toCamelCase } from "./utils"
3+
import { toCamelCase, getClassPath } from "./utils"
44

55
export function isFieldASubModule( instance :VuexModule & Map, field :string ) {
66
return(
@@ -10,8 +10,10 @@ export function isFieldASubModule( instance :VuexModule & Map, field :string ) {
1010
}
1111

1212
export function extractVuexSubModule( instance :VuexModule & Map, field :string ) {
13-
const subModuleClass = instance[ field ][ "__submodule_class__" ];
14-
return extractVuexModule( subModuleClass )[ toCamelCase( subModuleClass.name ) ] as VuexObject
13+
const subModuleClass = instance[ field ][ "__submodule_class__" ] as VuexModule & VuexModuleConstructor;
14+
const extract = extractVuexModule( subModuleClass );
15+
const path = getClassPath( subModuleClass.prototype.__namespacedPath__ ) || toCamelCase( subModuleClass.name );
16+
return extract[ path ];
1517
}
1618

1719
export function createSubModule<T>( Cls :T ) {

src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
export function toCamelCase(str :string){
22
return str[ 0 ].toLocaleLowerCase() + str.substring( 1 );
3-
}
3+
}
4+
5+
export function getClassPath( path :string ) {
6+
const arr = path.split( "/" );
7+
return arr[ arr.length - 1 ];
8+
}

test-ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 7549bff4f3e48f5b58d1ebcfdc369dbdbc43eb9d
1+
Subproject commit 5343f33db3f06798e3bbf6a817e74193422cfa8d

0 commit comments

Comments
 (0)