import Vue from 'vue' ; import { Mutation } from 'vuex' ; import { Dictionary , LocalModule , LocalGetter , LocalAction } from './declarations' ; import { localKey } from './utils' ; export function mapLocalModule ( vm , modulePath , module ) { mapState ( vm , module . state , modulePath ) ; const moduleName = modulePath [ modulePath . length - 1 ] ; if ( module . getters ) { mapGetters ( vm , module . getters , moduleName ) ; } if ( module . actions ) { mapActions ( vm , module . actions , moduleName ) ; } if ( module . mutations ) { mapMutations ( vm , module . mutations , moduleName ) ; } } function getSubState ( state , path ) { return path . reduce ( ( sub , key ) => sub [ key ] , state ) ; } function mapState ( vm , state , path ) { Object . keys ( state ) . forEach ( key => { Object . defineProperty ( vm , key , { enumerable : true , configurable : true , get : ( ) => getSubState ( vm . $store . state , path ) [ key ] } ) ; } ) ; } function mapGetters ( vm , getters , moduleName ) { Object . keys ( getters ) . forEach ( key => { Object . defineProperty ( vm , key , { enumerable : true , configurable : true , get : ( ) => vm . $store . getters [ localKey ( key , moduleName ) ] } ) ; } ) ; } function mapActions ( vm , actions , moduleName ) { Object . keys ( actions ) . forEach ( key => { ( vm as any ) [ key ] = ( payload ) => { return vm . $store . dispatch ( localKey ( key , moduleName ) , payload ) ; } ; } ) ; } function mapMutations ( vm , mutations , moduleName ) { Object . keys ( mutations ) . forEach ( key => { ( vm as any ) [ key ] = ( payload ) => { vm . $store . commit ( localKey ( key , moduleName ) , payload ) ; } ; } ) ; }