11import { extractVuexModule } from "./module" ;
22import { 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
103109export 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
181181function 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
366370function 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