@@ -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
258261function 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
332340function 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}
0 commit comments