Skip to content

Commit d6b1ae9

Browse files
committed
fixed store issues
1 parent e33a635 commit d6b1ae9

File tree

7 files changed

+51
-29
lines changed

7 files changed

+51
-29
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules/
1+
node_modules/
2+
test-ts/
3+
test-js/

src/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface VuexModuleInternals {
3131
export interface VuexModuleInternalsPrototype {
3232
__options__ :VuexModuleOptions | undefined;
3333
__namespacedPath__ :string;
34-
__vuex_module_cache__ :undefined | VuexObject;
34+
__vuex_module_cache__ :undefined | { [ path: string ]: VuexObject };
3535
__vuex_proxy_cache__ :{} | undefined;
3636
__vuex_local_proxy_cache__ :{} | undefined;
3737
__submodules_cache__: Map;
@@ -47,6 +47,7 @@ export interface VuexModuleInternalsPrototype {
4747
__type__ :ActionType,
4848
}[],
4949
__watch__ :Map;
50+
__store_cache__ :any;
5051
}
5152

5253
export interface SubModuleType<T> {

src/module.legacy.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ export interface LegacyVuexModule extends VuexModuleAddons {}
4040
export class LegacyVuexModule {
4141

4242
static ExtractVuexModule( cls :typeof VuexModule ) {
43-
const vxmodule = extractVuexModule( cls );
44-
//@ts-ignore
45-
return vxmodule[ cls.prototype.__namespacedPath__ ]
43+
44+
const VuexClass = cls as VuexModule & VuexModuleConstructor;
45+
46+
const vxmodule = extractVuexModule( VuexClass );
47+
48+
return vxmodule[ VuexClass.prototype.__namespacedPath__ ];
4649
}
4750

4851
static CreateProxy<T extends typeof VuexModule>( $store :Map, cls :T ) {
49-
return createProxy( $store, cls )
52+
return createProxy( $store, cls );
5053
}
5154

5255
static CreateSubModule( cls :typeof VuexModule ) {

src/module.ts

Lines changed: 7 additions & 7 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 );
@@ -63,12 +63,12 @@ export function extractVuexModule( cls :typeof VuexModule ) {
6363
};
6464

6565
// Cache the vuex module on the class.
66-
VuexClass.prototype.__vuex_module_cache__ = vuexModule;
6766
const className = getNamespacedPath( VuexClass );
67+
68+
const rtn = { [ className ]: vuexModule }
69+
VuexClass.prototype.__vuex_module_cache__ = rtn;
6870

69-
return {
70-
[ className ]: vuexModule
71-
} as any
71+
return rtn;
7272

7373
}
7474

src/proxy.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ export function createProxy<T extends typeof VuexModule>( $store :any, cls :T )
1111
const VuexClass = cls as VuexModuleConstructor;
1212

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

1818
const namespacedPath = VuexClass.prototype.__namespacedPath__ ? VuexClass.prototype.__namespacedPath__ + "/" : "";
1919

2020
// Create Proxy and cache
2121
const proxy = _createProxy( cls, $store, namespacedPath );
22-
VuexClass.prototype.__vuex_proxy_cache__ = proxy;
2322

2423
// Setup Local Watchers
2524
createLocalWatchers( VuexClass, $store, namespacedPath || "" );
@@ -86,6 +85,10 @@ export function createProxy<T extends typeof VuexModule>( $store :any, cls :T )
8685
})
8786
}
8887
}
88+
89+
if( VuexClass.prototype.__store_cache__ === undefined ) VuexClass.prototype.__store_cache__ = $store;
90+
91+
VuexClass.prototype.__vuex_proxy_cache__ = proxy;
8992

9093
return proxy as InstanceType<T> & ProxyWatchers;
9194
}
@@ -114,9 +117,9 @@ export function _createProxy<T>(cls: T, $store: any, namespacedPath = "") {
114117
const { state, mutations, actions, getters, modules } = extractVuexModule( VuexClass )[ VuexClass.prototype.__namespacedPath__ ];
115118

116119
createGettersAndMutationProxyFromState({ cls: VuexClass, proxy, state, $store, namespacedPath, maxDepth: 7 });
117-
createExplicitMutationsProxy({ cls: VuexClass, proxy, $store, namespacedPath });
120+
createExplicitMutationsProxy( VuexClass, proxy, $store, namespacedPath );
118121
createGettersAndGetterMutationsProxy({ cls: VuexClass, mutations, getters, proxy, $store, namespacedPath });
119-
createActionProxy({ actions, proxy, $store, namespacedPath });
122+
createActionProxy({ cls: VuexClass, actions, proxy, $store, namespacedPath });
120123
createSubModuleProxy( $store, VuexClass, proxy, modules );
121124

122125
//@ts-ignore
@@ -256,10 +259,10 @@ function createLocalWatchers( cls :VuexModuleConstructor, $store :Map, namespace
256259
}
257260

258261
function createSubModuleProxy( $store :Map, cls:VuexModuleConstructor, proxy :Map, modules :Map ) {
259-
262+
const store = cls.prototype.__store_cache__ || $store;
260263
for( let field in modules ) {
261264
const subModuleClass = cls.prototype.__submodules_cache__[ field ];
262-
proxy[ field ] = createProxy( $store, subModuleClass );
265+
proxy[ field ] = createProxy( store, subModuleClass );
263266
}
264267

265268
}
@@ -322,11 +325,16 @@ function createGettersAndMutationProxyFromState({ cls, proxy, state, $store, nam
322325

323326
}
324327

325-
function createExplicitMutationsProxy({ cls, proxy, $store, namespacedPath } :MutationProxyCreator) {
328+
function createExplicitMutationsProxy( cls :VuexModuleConstructor, proxy :Map, $store :any, namespacedPath :string ) {
329+
326330
const mutations = cls.prototype.__mutations_cache__.__explicit_mutations__;
331+
const commit = cls.prototype.__store_cache__ ? cls.prototype.__store_cache__.commit : $store.commit;
332+
namespacedPath = cls.prototype.__namespacedPath__.length ? cls.prototype.__namespacedPath__ + "/" : namespacedPath;
333+
327334
for( let field in mutations ) {
328-
proxy[ field ] = ( payload :any ) => $store.commit( namespacedPath + field, payload )
335+
proxy[ field ] = ( payload :any ) => commit( namespacedPath + field, payload )
329336
}
337+
330338
}
331339

332340
function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy, $store, namespacedPath } :GetterProxyCreator) {
@@ -367,11 +375,14 @@ function createGettersAndGetterMutationsProxy({ cls, getters, mutations, proxy,
367375
}
368376
}
369377

370-
function createActionProxy({ actions, proxy, $store, namespacedPath } :ActionProxyCreator) {
378+
function createActionProxy({ cls, actions, proxy, $store, namespacedPath } :ActionProxyCreator) {
379+
380+
const dispatch = cls.prototype.__store_cache__ ? cls.prototype.__store_cache__.dispatch : $store.dispatch;
381+
namespacedPath = cls.prototype.__namespacedPath__.length ? cls.prototype.__namespacedPath__ + "/" : namespacedPath;
382+
371383
for( let field in actions ) {
372-
if( $store === undefined ) continue;
373384
proxy[ field ] = function( payload :any ) {
374-
return $store.dispatch( namespacedPath + field, payload );
385+
return dispatch( namespacedPath + field, payload );
375386
}
376387
}
377388
}
@@ -422,6 +433,6 @@ interface GetterProxyCreator extends MutationProxyCreator {
422433
mutations :Map;
423434
}
424435

425-
interface ActionProxyCreator extends ProxyCreator {
436+
interface ActionProxyCreator extends MutationProxyCreator {
426437
actions :Map;
427438
}

test/create-proxy.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class UserSettings extends VuexModule {
2222

2323
@Module({ namespacedPath: 'user/' })
2424
class UserStore extends VuexModule {
25-
// settings = UserSettings.CreateSubModule(UserSettings)
25+
26+
settings = UserSettings.CreateSubModule(UserSettings)
2627

2728
firstname = 'Michael'
2829
lastname = 'Olofinjana'
@@ -78,7 +79,7 @@ describe('CreateProxy', () => {
7879
expect(user.lastname).toEqual('Olofinjana')
7980
})
8081

81-
it.only('should proxy actions', async () => {
82+
it('should proxy actions', async () => {
8283

8384
const user = UserStore.CreateProxy(store, UserStore)
8485

tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@
4343

4444
/* Experimental Options */
4545
},
46+
"exclude": [
47+
"test-js/*",
48+
"test-ts/*"
49+
]
4650
}

0 commit comments

Comments
 (0)