import { ContextPosition , getters as namespacedGetters , commit as namespacedCommit , dispatch as namespacedDispatch , Commit , Dispatch , createLazyContextPosition , } from './context' ; import { mapValues , get } from './utils' ; import { Module } from './module' ; import { BG , BM , BA , Payload } from './assets' ; import { ComponentPublicInstance } from 'vue' ; export type MappedFunction < Fn , R > = undefined extends Payload < Fn > ? ( payload ? : Payload < Fn > ) => R : ( payload : Payload < Fn > ) => R ; export type RestArgs < Fn > = Fn extends ( _ : any , ... args : infer R ) => any ? R : never ; export function createMapper < S , G extends BG < S > , M extends BM < S > , A extends BA < S , G , M > > ( module ) { return new ComponentMapper ( createLazyContextPosition ( module ) ) ; } export class ComponentMapper < S , G extends BG < S > , M extends BM < S > , A extends BA < S , G , M > > { constructor ( private pos ) { } mapState < K extends keyof S > ( map ) ; mapState < T extends Record < string , keyof S > > ( map ) ; mapState < T extends Record < string , ( state : S , getters : G ) => any > > ( map ) ; mapState ( map ) { const pos = this . pos ; return createMappedObject ( map , ( value ) => { return function mappedStateComputed ( this ) { const state = get ( pos . path , this . $store . state ) ; if ( typeof value === 'function' ) { const getters = namespacedGetters ( this . $store , pos . namespace ) ; return value . call ( this , state , getters ) ; } else { return state [ value ] ; } } ; } ) ; } mapGetters < K extends keyof G > ( map ) ; mapGetters < T extends Record < string , keyof G > > ( map ) ; mapGetters ( map ) { const pos = this . pos ; return createMappedObject ( map , ( value ) => { function mappedGetterComputed ( this ) { return this . $store . getters [ pos . namespace + value ] ; } mappedGetterComputed . vuex = true ; return mappedGetterComputed ; } ) ; } mapMutations < K extends keyof M > ( map ) ; mapMutations < T extends Record < string , keyof M > > ( map ) ; mapMutations < T extends Record < string , ( commit : Commit < M > , ... args : any [ ] ) => any > > ( map ) ; mapMutations ( map ) { const pos = this . pos ; return createMappedObject ( map , ( value ) => { return function mappedMutationMethod ( this , ... args ) { const commit = ( type , payload ) => { return namespacedCommit ( this . $store , pos . namespace , type , payload ) ; } ; return typeof value === 'function' ? value . apply ( this , [ commit ] . concat ( args ) ) : commit ( value , args [ 0 ] ) ; } ; } ) ; } mapActions < K extends keyof A > ( map ) ; mapActions < T extends Record < string , keyof A > > ( map ) ; mapActions < T extends Record < string , ( dispatch : Dispatch < A > , ... args : any [ ] ) => any > > ( map ) ; mapActions ( map ) { const pos = this . pos ; return createMappedObject ( map , ( value ) => { return function mappedActionMethod ( this , ... args ) { const dispatch = ( type , payload ) => { return namespacedDispatch ( this . $store , pos . namespace , type , payload ) ; } ; return typeof value === 'function' ? value . apply ( this , [ dispatch ] . concat ( args ) ) : dispatch ( value , args [ 0 ] ) ; } ; } ) ; } } function createMappedObject ( map , fn ) { const normalized = ! Array . isArray ( map ) ? map : map . reduce < Record < string , string > > ( ( acc , key ) => { acc [ key ] = key ; return acc ; } , { } ) ; return mapValues ( normalized , fn ) ; }