import { ActionContext , createStore , Store } from "vuex" ; import { ActionsImpl , GettersImpl , ModuleOptions , ModulesImpl , MutationsImpl , StateOf , StoreOptions , StoreOrModuleOptions , WithOptionalState } from "../types" ; import { CreatedStore , ToDirectStore , VuexStore } from "../types/direct-types" ; export function createDirectStore < O extends WithOptionalState , S = StateOf < O > > ( options ) { const original = createStore ( options as any ) as VuexStore < O > ; const store = { get state ( ) { return original . state as any ; } , getters : toDirectGetters ( options , original . getters ) , commit : toDirectCommit ( options , original . commit ) , dispatch : toDirectDispatch ( options , original . dispatch ) , original } ; original . direct = store ; return { store , rootGetterContext : ( [ state , getters ] ) => getModuleGetterContext ( [ state , getters , state , getters ] , options , options ) , moduleGetterContext : ( args , moduleOptions ) => getModuleGetterContext ( args , moduleOptions , options ) , rootActionContext : ( originalContext ) => getModuleActionContext ( originalContext , options , options ) , moduleActionContext : ( originalContext , moduleOptions ) => getModuleActionContext ( originalContext , moduleOptions , options ) , } ; } export function localGetterContext < O extends StoreOrModuleOptions > ( [ state , getters ] , options ) { return getModuleGetterContext ( [ state , getters , undefined , undefined ] , options ) ; } export function localActionContext < O extends StoreOrModuleOptions > ( originalContext , options ) { return getModuleActionContext ( originalContext , options ) ; } export function defineModule < O extends WithOptionalState , S = StateOf < O > > ( options ) { return options ; } export function defineModules < S > ( ) { return modules => modules ; } export function defineGetters < S > ( ) { return getters => getters ; } export function defineMutations < S > ( ) { return mutations => mutations ; } export function defineActions < T > ( actions ) { return actions ; } export const createModule = obsolete ( defineModule , "createModule" , "defineModule" ) ; export const createModules = obsolete ( defineModules , "createModules" , "defineModules" ) ; export const createGetters = obsolete ( defineGetters , "createGetters" , "defineGetters" ) ; export const createMutations = obsolete ( defineMutations , "createMutations" , "defineMutations" ) ; export const createActions = obsolete ( defineActions , "createActions" , "defineActions" ) ; function obsolete < T extends ( ... args : any [ ] ) => any > ( fn , oldName , newName ) { return ( ( ... args ) => { console . warn ( `Function '${ oldName }' is obsolete, please use '${ newName }'.` ) ; return fn ( ... args ) ; } ) as T ; } export default { createDirectStore , defineModule , defineModules , defineGetters , defineMutations , defineActions , localGetterContext , localActionContext , createModule , createModules , createGetters , createMutations , createActions } ; const gettersCache = new WeakMap < Store < any > [ "getters" ] , any > ( ) ; function toDirectGetters ( options , originalGetters ) { let getters = gettersCache . get ( originalGetters ) ; if ( ! getters ) { getters = gettersFromOptions ( { } , options , originalGetters ) ; gettersCache . set ( originalGetters , getters ) ; } return getters ; } function gettersFromOptions ( result , options , originalGetters , hierarchy = [ ] ) { if ( options . getters ) createDirectGetters ( result , options . getters , originalGetters , hierarchy ) ; if ( options . modules ) { for ( const moduleName of Object . keys ( options . modules ) ) { const moduleOptions = options . modules [ moduleName ] ; if ( moduleOptions . namespaced ) result [ moduleName ] = gettersFromOptions ( { } , moduleOptions , originalGetters , [ ... hierarchy , moduleName ] ) ; else gettersFromOptions ( result , moduleOptions , originalGetters , hierarchy ) ; } } return result ; } function createDirectGetters ( result , gettersImpl , originalGetters , hierarchy ? ) { const prefix = ! hierarchy || hierarchy . length === 0 ? "" : `${ hierarchy . join ( "/" ) }/` ; for ( const name of Object . keys ( gettersImpl ) ) { Object . defineProperties ( result , { [ name ] : { get : ( ) => originalGetters [ `${ prefix }${ name }` ] } } ) ; } } const commitCache = new WeakMap < Store < any > [ "commit" ] , any > ( ) ; function toDirectCommit ( options , originalCommit ) { let commit = commitCache . get ( originalCommit ) ; if ( ! commit ) { commit = commitFromOptions ( { } , options , originalCommit ) ; commitCache . set ( originalCommit , commit ) ; } return commit ; } const rootCommitCache = new WeakMap < Store < any > [ "commit" ] , any > ( ) ; function toDirectRootCommit ( rootOptions , originalCommit ) { let commit = rootCommitCache . get ( originalCommit ) ; if ( ! commit ) { const origCall = ( mutation , payload ) => originalCommit ( mutation , payload , { root : true } ) ; commit = commitFromOptions ( { } , rootOptions , origCall ) ; rootCommitCache . set ( originalCommit , commit ) ; } return commit ; } function commitFromOptions ( result , options , originalCommitCall , hierarchy = [ ] ) { if ( options . mutations ) createDirectMutations ( result , options . mutations , originalCommitCall , hierarchy ) ; if ( options . modules ) { for ( const moduleName of Object . keys ( options . modules ) ) { const moduleOptions = options . modules [ moduleName ] ; if ( moduleOptions . namespaced ) result [ moduleName ] = commitFromOptions ( { } , moduleOptions , originalCommitCall , [ ... hierarchy , moduleName ] ) ; else commitFromOptions ( result , moduleOptions , originalCommitCall , hierarchy ) ; } } return result ; } function createDirectMutations ( result , mutationsImpl , originalCommitCall , hierarchy ? ) { const prefix = ! hierarchy || hierarchy . length === 0 ? "" : `${ hierarchy . join ( "/" ) }/` ; for ( const name of Object . keys ( mutationsImpl ) ) result [ name ] = ( payload ) => originalCommitCall ( `${ prefix }${ name }` , payload ) ; } const dispatchCache = new WeakMap < Store < any > [ "dispatch" ] , any > ( ) ; function toDirectDispatch ( options , originalDispatch ) { let dispatch = dispatchCache . get ( originalDispatch ) ; if ( ! dispatch ) { dispatch = dispatchFromOptions ( { } , options , originalDispatch ) ; dispatchCache . set ( originalDispatch , dispatch ) ; } return dispatch ; } const rootDispatchCache = new WeakMap < Store < any > [ "dispatch" ] , any > ( ) ; function toDirectRootDispatch ( rootOptions , originalDispatch ) { let dispatch = rootDispatchCache . get ( originalDispatch ) ; if ( ! dispatch ) { const origCall = ( mutation , payload ) => originalDispatch ( mutation , payload , { root : true } ) ; dispatch = dispatchFromOptions ( { } , rootOptions , origCall ) ; rootDispatchCache . set ( originalDispatch , dispatch ) ; } return dispatch ; } function dispatchFromOptions ( result , options , originalDispatchCall , hierarchy = [ ] ) { if ( options . actions ) createDirectActions ( result , options . actions , originalDispatchCall , hierarchy ) ; if ( options . modules ) { for ( const moduleName of Object . keys ( options . modules ) ) { const moduleOptions = options . modules [ moduleName ] ; if ( moduleOptions . namespaced ) result [ moduleName ] = dispatchFromOptions ( { } , moduleOptions , originalDispatchCall , [ ... hierarchy , moduleName ] ) ; else dispatchFromOptions ( result , moduleOptions , originalDispatchCall , hierarchy ) ; } } return result ; } function createDirectActions ( result , actionsImpl , originalDispatchCall , hierarchy ? ) { const prefix = ! hierarchy || hierarchy . length === 0 ? "" : `${ hierarchy . join ( "/" ) }/` ; for ( const name of Object . keys ( actionsImpl ) ) result [ name ] = ( payload ? ) => originalDispatchCall ( `${ prefix }${ name }` , payload ) ; } const getterContextCache = new WeakMap < any , any > ( ) ; function getModuleGetterContext ( args , options , rootOptions ? ) { const [ state , getters , rootState , rootGetters ] = args ; let context = actionContextCache . get ( state ) ; if ( ! context ) { if ( rootOptions ) { context = { get rootState ( ) { return rootState ; } , get rootGetters ( ) { return toDirectGetters ( rootOptions ! , rootGetters ) ; } , get state ( ) { return state ; } , get getters ( ) { return toDirectGetters ( options , getters ) ; } } ; } else { context = { get state ( ) { return state ; } , get getters ( ) { return toDirectGetters ( options , getters ) ; } } ; } if ( state ) getterContextCache . set ( state , context ) ; } return context ; } const actionContextCache = new WeakMap < any , any > ( ) ; function getModuleActionContext ( originalContext , options , rootOptions ? ) { let context = actionContextCache . get ( originalContext . state ) ; if ( ! context ) { if ( rootOptions ) { context = { get rootState ( ) { return originalContext . rootState ; } , get rootGetters ( ) { return toDirectGetters ( rootOptions ! , originalContext . rootGetters ) ; } , get rootCommit ( ) { return toDirectRootCommit ( rootOptions ! , originalContext . commit ) ; } , get rootDispatch ( ) { return toDirectRootDispatch ( rootOptions ! , originalContext . dispatch ) ; } , get state ( ) { return originalContext . state ; } , get getters ( ) { return toDirectGetters ( options , originalContext . getters ) ; } , get commit ( ) { return toDirectCommit ( options , originalContext . commit ) ; } , get dispatch ( ) { return toDirectDispatch ( options , originalContext . dispatch ) ; } } ; } else { context = { get state ( ) { return originalContext . state ; } , get getters ( ) { return toDirectGetters ( options , originalContext . getters ) ; } , get commit ( ) { return toDirectCommit ( options , originalContext . commit ) ; } , get dispatch ( ) { return toDirectDispatch ( options , originalContext . dispatch ) ; } } ; } if ( originalContext . state ) actionContextCache . set ( originalContext . state , context ) ; } return context ; }