import { Store } from 'vuex' ; import { reduce } from '../../utils/object' ; import { Dictionary } from '../../utils/types' ; import { ExtractGetters , XModuleName } from '../../x-modules/x-modules.types' ; import { AnyXStoreModule } from '../store.types' ; type Getters = { [ ModuleName in XModuleName ] ? : ExtractGetters < ModuleName > ; } ; let cache = { } ; param getters param moduleName param storeModule returns A  GettersTree object with only the getters of the  XModule .
 *
 *  internal remarks This proxy will be used by the stateSelector, of the module. This is done to ensure that
 * a Vuex stateSelector can only access the getters of the  XModule where it is registered.
 * This function task can be done with  getGettersProxy , just without passing the
 * storeModule. But in that case every time we register emitters in a module, it will loop over all
 * the getters of all the store. This way, passing the moduleNeeded, it only loops over the getters
 * of that module. It is a performance question to have this two different implementations.
 *
  export function getGettersProxyFromModule < ModuleName extends XModuleName > ( getters , moduleName , storeModule ) { const cachedGetter = cache [ moduleName ] ; if ( isCacheGetterDefined < ModuleName > ( cachedGetter ) ) { return cachedGetter ; } const modulePath = `x/${ moduleName }/` ; const safeGetters = reduce ( storeModule . getters as Dictionary , ( safeGettersProxy , getterName ) => defineGetterProxy ( safeGettersProxy , getterName , `${ modulePath }${ getterName }` , getters ) , { } as ExtractGetters < ModuleName > ) ; cache [ moduleName ] = safeGetters as unknown as Getters [ ModuleName ] ; return safeGetters ; } param getters param moduleName returns A  GettersTree object with only the getters of the  XModule .
 *
 *  internal remarks export function getGettersProxy < ModuleName extends XModuleName > ( getters , moduleName ) { const cachedGetter = cache [ moduleName ] ; if ( isCacheGetterDefined < ModuleName > ( cachedGetter ) ) { return cachedGetter ; } const modulePath = `x/${ moduleName }/` ; const getterKeys = Object . keys ( getters ) . filter ( getterKey => getterKey . startsWith ( modulePath ) ) ; const safeGetters = getterKeys . reduce ( ( safeGettersProxy , fullPathGetterName ) => { const getterName = fullPathGetterName . replace ( modulePath , '' ) ; return defineGetterProxy ( safeGettersProxy , getterName , fullPathGetterName , getters ) ; } , { } as ExtractGetters < ModuleName > ) ; cache [ moduleName ] = safeGetters as unknown as Getters [ ModuleName ] ; return safeGetters ; } param safeGettersProxy param getterName param fullPathGetterName param getters returns internal function defineGetterProxy < ModuleName extends XModuleName > ( safeGettersProxy , getterName , fullPathGetterName , getters ) { return Object . defineProperty ( safeGettersProxy , getterName , { get ( ) { return getters [ fullPathGetterName ] ; } , enumerable : true } ) ; } internal export function cleanGettersProxyCache ( ) { cache = { } ; } param cachedGetter returns internal function isCacheGetterDefined < ModuleName extends XModuleName > ( cachedGetter ) { return cachedGetter !== undefined ; }