55 Performance ,
66 updateItem ,
77} from "@/tech/SpacedRepetition" ;
8- import { createDirectStore } from "direct-vuex" ;
98import Vue from "vue" ;
109import Vuex , { ActionContext } from "vuex" ;
10+ import { getStoreAccessors } from "vuex-typescript" ;
1111import {
1212 CharacterId ,
1313 GameAndCharacterId ,
@@ -32,7 +32,7 @@ export interface PracticeSet<T extends TechId> {
3232 timestamp : number ;
3333}
3434
35- interface State {
35+ interface MainState {
3636 version : 1 ;
3737 local : {
3838 loaded : boolean ;
@@ -44,7 +44,13 @@ interface State {
4444 } ;
4545}
4646
47- const defaultState : State = {
47+ interface RootState {
48+ modules : {
49+ main : MainState ;
50+ } ;
51+ }
52+
53+ const defaultState : MainState = {
4854 version : 1 ,
4955 local : {
5056 loaded : false ,
@@ -60,11 +66,24 @@ const defaultState: State = {
6066
6167Vue . use ( Vuex ) ;
6268
63- const { store } = createDirectStore ( {
69+ type MainContext = ActionContext < MainState , RootState > ;
70+
71+ const mainStore = {
72+ namespaced : true ,
6473 state : defaultState ,
6574 getters : {
75+ selectedCharacters (
76+ state : MainState ,
77+ ) : MainState [ "local" ] [ "selectedCharacters" ] {
78+ return state . local . selectedCharacters ;
79+ } ,
80+ recordedPracticeSets (
81+ state : MainState ,
82+ ) : MainState [ "remote" ] [ "recordedPracticeSets" ] {
83+ return state . remote . recordedPracticeSets ;
84+ } ,
6685 spacedRepetitionItems (
67- state : State ,
86+ state : MainState ,
6887 ) : Map < SerializedTechVariant , SpacedRepetitionItem > {
6988 const result = new Map < SerializedTechVariant , SpacedRepetitionItem > ( ) ;
7089 for ( const practiceSet of state . remote . recordedPracticeSets ) {
@@ -86,7 +105,7 @@ const { store } = createDirectStore({
86105 } ,
87106 mutations : {
88107 selectCharacter < T extends GameId > (
89- state : State ,
108+ state : MainState ,
90109 gameAndCharacterId : GameAndCharacterId < T > ,
91110 ) : void {
92111 const { gameId, characterId } = gameAndCharacterId ;
@@ -96,25 +115,25 @@ const { store } = createDirectStore({
96115 // with `characterId as CharacterId<typeof gameId>` doesn't work.
97116 selectedCharacters [ gameId ] = characterId ;
98117 } ,
99- unselectCharacter ( state : State , gameId : GameId ) : void {
118+ unselectCharacter ( state : MainState , gameId : GameId ) : void {
100119 state . local . selectedCharacters [ gameId ] = null ;
101120 } ,
102121 recordPracticeSet < T extends TechId > (
103- state : State ,
122+ state : MainState ,
104123 practiceSet : PracticeSet < T > ,
105124 ) : void {
106125 state . remote . recordedPracticeSets . push ( practiceSet ) ;
107126 } ,
108- restoreState ( state : State , newState : State ) {
127+ restoreState ( state : MainState , newState : MainState ) {
109128 Object . assign ( state , newState ) ;
110129 } ,
111130 } ,
112131 actions : {
113- async saveState ( context : ActionContext < State , { } > ) : Promise < void > {
132+ async saveState ( context : MainContext ) : Promise < void > {
114133 const state = { ...context . state , loaded : false } ;
115134 localStorage . setItem ( "state" , JSON . stringify ( state ) ) ;
116135 } ,
117- async restoreState ( context : ActionContext < State , { } > ) : Promise < void > {
136+ async restoreState ( context : MainContext ) : Promise < void > {
118137 if ( context . state . local . loaded ) {
119138 return ;
120139 }
@@ -123,7 +142,7 @@ const { store } = createDirectStore({
123142 if ( previousStateString === null ) {
124143 return ;
125144 }
126- const previousState : State = JSON . parse ( previousStateString ) ;
145+ const previousState : MainState = JSON . parse ( previousStateString ) ;
127146 const oldVersion : number = previousState . version ;
128147 const currentVersion = context . state . version ;
129148 if ( oldVersion !== currentVersion ) {
@@ -140,12 +159,41 @@ const { store } = createDirectStore({
140159 } ) ;
141160 } ,
142161 } ,
162+ } ;
163+
164+ export type MainStore = typeof mainStore ;
165+
166+ const rootStore = new Vuex . Store ( {
167+ modules : {
168+ main : mainStore ,
169+ } ,
143170} ) ;
144171
145- export default store ;
172+ export type RootStore = typeof rootStore ;
146173
147- export type Store = typeof store ;
174+ export default rootStore ;
148175
149- export function getStore ( ) : Store {
150- return store ;
151- }
176+ const { commit, read, dispatch } = getStoreAccessors < MainState , RootState > (
177+ "main" ,
178+ ) ;
179+
180+ export const readSpacedRepetitionItems = read (
181+ mainStore . getters . spacedRepetitionItems ,
182+ ) ;
183+ export const readRecordedPracticeSets = read (
184+ mainStore . getters . recordedPracticeSets ,
185+ ) ;
186+ export const readSelectedCharacters = read (
187+ mainStore . getters . selectedCharacters ,
188+ ) ;
189+ export const dispatchRestoreState = dispatch ( mainStore . actions . restoreState ) ;
190+ export const dispatchSaveState = dispatch ( mainStore . actions . saveState ) ;
191+ export const commitRecordPracticeSet = commit (
192+ mainStore . mutations . recordPracticeSet ,
193+ ) ;
194+ export const commitSelectCharacter = commit (
195+ mainStore . mutations . selectCharacter ,
196+ ) ;
197+ export const commitUnselectCharacter = commit (
198+ mainStore . mutations . unselectCharacter ,
199+ ) ;
0 commit comments