import { Ref , UnwrapRef , ComputedRef , reactive , computed } from 'vue' ; import { SubStateFlagWrapper } from './createSubState' ; export type SubState = Omit < object , '__isSubState__' > & SubStateFlagWrapper ; export type State = { [ key ] : SubState ; } ; export type SelectorsBase < T extends State > = { [ key ] : ( state : T ) => any ; } ; export type Selectors < T extends State , U extends SelectorsBase < T > > = { [ K in keyof U ] : ( state : T ) => ReturnType < U [ K ] > ; } ; type ComputedSelectors < T extends State , U extends SelectorsBase < T > > = { [ K in keyof U ] : ComputedRef < ReturnType < U [ K ] > > ; } ; type ReactiveState < T > = T extends Ref ? T : UnwrapRef < T > ; export default class Store < T extends State , U extends SelectorsBase < T > > { private readonly reactiveState : T extends Ref ? T : UnwrapRef < T > ; private readonly reactiveSelectors : ComputedSelectors < T , U > ; constructor ( initialState , selectors ? ) { this . reactiveState = reactive ( initialState ) ; this . reactiveSelectors = { } as ComputedSelectors < T , U > ; if ( selectors ) { Object . keys ( selectors ) . forEach ( ( key ) => ( this . reactiveSelectors [ key ] = computed ( ( ) => selectors [ key ] ( this . reactiveState ) ) ) ) ; } } getState ( ) { return this . reactiveState ; } getSelectors ( ) { return this . reactiveSelectors ; } getStateAndSelectors ( ) { return [ this . reactiveState , this . reactiveSelectors ] ; } }