[feat] better store operation
This commit is contained in:
@@ -1,13 +1,7 @@
|
||||
import { store } from "tauri-plugin-valtio"
|
||||
import { useSnapshot } from "valtio"
|
||||
import { DEFAULT_STORE_CONFIG } from "."
|
||||
|
||||
// Usage:
|
||||
// import {appStore} from "@/store/app"
|
||||
// import { useSnapshot } from "valtio"
|
||||
// const app = useSnapshot(appStore.state)
|
||||
// { app.version }
|
||||
// () => appStore.setVersion("0.0.1")
|
||||
|
||||
const defaultValue = {
|
||||
version: "0.0.1",
|
||||
hasUpdate: false,
|
||||
@@ -18,26 +12,41 @@ const defaultValue = {
|
||||
|
||||
export const appStore = store("app", { ...defaultValue }, DEFAULT_STORE_CONFIG)
|
||||
|
||||
export const setVersion = (version: string) => {
|
||||
export const useAppStore = () => {
|
||||
void appStore.start
|
||||
const state = useSnapshot(appStore.state)
|
||||
|
||||
return {
|
||||
state,
|
||||
setVersion,
|
||||
setHasUpdate,
|
||||
setInited,
|
||||
setNotice,
|
||||
setUseMirror,
|
||||
resetAppStore,
|
||||
}
|
||||
}
|
||||
|
||||
const setVersion = (version: string) => {
|
||||
appStore.state.version = version
|
||||
}
|
||||
export const setHasUpdate = (hasUpdate: boolean) => {
|
||||
const setHasUpdate = (hasUpdate: boolean) => {
|
||||
appStore.state.hasUpdate = hasUpdate
|
||||
}
|
||||
export const setInited = (inited: boolean) => {
|
||||
const setInited = (inited: boolean) => {
|
||||
appStore.state.inited = inited
|
||||
}
|
||||
export const setNotice = (notice: string) => {
|
||||
const setNotice = (notice: string) => {
|
||||
appStore.state.notice = notice
|
||||
}
|
||||
export const setUseMirror = (useMirror: boolean) => {
|
||||
const setUseMirror = (useMirror: boolean) => {
|
||||
appStore.state.useMirror = useMirror
|
||||
}
|
||||
|
||||
export const resetAppStore = () => {
|
||||
const resetAppStore = () => {
|
||||
setVersion(defaultValue.version)
|
||||
setHasUpdate(defaultValue.hasUpdate)
|
||||
setInited(defaultValue.inited)
|
||||
setNotice(defaultValue.notice)
|
||||
setUseMirror(defaultValue.useMirror)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { SteamUser } from "@/types/steam"
|
||||
import { store } from "tauri-plugin-valtio"
|
||||
import { DEFAULT_STORE_CONFIG } from "."
|
||||
import { useSnapshot } from "valtio"
|
||||
|
||||
const defaultValue = {
|
||||
dir: "C:\\Program Files (x86)\\Steam",
|
||||
@@ -18,33 +19,50 @@ const defaultValue = {
|
||||
isDirValid: false,
|
||||
isCsDirValid: false,
|
||||
}
|
||||
|
||||
export const steamStore = store(
|
||||
"steam",
|
||||
{ ...defaultValue },
|
||||
DEFAULT_STORE_CONFIG,
|
||||
)
|
||||
|
||||
export const setDir = (dir: string) => {
|
||||
export const useSteamStore = () => {
|
||||
void steamStore.start
|
||||
const state = useSnapshot(steamStore.state)
|
||||
|
||||
return {
|
||||
state,
|
||||
setDir,
|
||||
setCsDir,
|
||||
setUsers,
|
||||
setIsDirValid,
|
||||
setIsCsDirValid,
|
||||
currentUser,
|
||||
resetSteamStore,
|
||||
}
|
||||
}
|
||||
|
||||
const setDir = (dir: string) => {
|
||||
steamStore.state.dir = dir
|
||||
}
|
||||
export const setCsDir = (dir: string) => {
|
||||
const setCsDir = (dir: string) => {
|
||||
steamStore.state.csDir = dir
|
||||
}
|
||||
export const setUsers = (users: SteamUser[]) => {
|
||||
const setUsers = (users: SteamUser[]) => {
|
||||
steamStore.state.users = users
|
||||
}
|
||||
export const setIsDirValid = (valid: boolean) => {
|
||||
const setIsDirValid = (valid: boolean) => {
|
||||
steamStore.state.isDirValid = valid
|
||||
}
|
||||
export const setIsCsDirValid = (valid: boolean) => {
|
||||
const setIsCsDirValid = (valid: boolean) => {
|
||||
steamStore.state.isCsDirValid = valid
|
||||
}
|
||||
|
||||
export const currentUser = () => {
|
||||
const currentUser = () => {
|
||||
return steamStore.state.users[0] || defaultValue.users[0]
|
||||
}
|
||||
|
||||
export const resetSteamStore = () => {
|
||||
const resetSteamStore = () => {
|
||||
setDir(defaultValue.dir)
|
||||
setCsDir(defaultValue.csDir)
|
||||
setUsers(defaultValue.users)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { store } from "tauri-plugin-valtio"
|
||||
import { useSnapshot } from "valtio"
|
||||
import { DEFAULT_STORE_CONFIG } from "."
|
||||
|
||||
|
||||
const defaultValue = {
|
||||
launchOptions: [
|
||||
"-novid -high -freq 144 -fullscreen",
|
||||
@@ -17,7 +19,22 @@ export const toolStore = store(
|
||||
DEFAULT_STORE_CONFIG,
|
||||
)
|
||||
|
||||
export const setLaunchOption = (option: string, index: number) => {
|
||||
export const useToolStore = () => {
|
||||
void toolStore.start
|
||||
const state = useSnapshot(toolStore.state)
|
||||
|
||||
return {
|
||||
state,
|
||||
setLaunchOption,
|
||||
setLaunchOptions,
|
||||
setLaunchIndex,
|
||||
setPowerPlan,
|
||||
addLaunchOption,
|
||||
resetToolStore,
|
||||
}
|
||||
}
|
||||
|
||||
const setLaunchOption = (option: string, index: number) => {
|
||||
toolStore.state.launchOptions = [
|
||||
...toolStore.state.launchOptions.slice(0, index),
|
||||
option,
|
||||
@@ -25,19 +42,19 @@ export const setLaunchOption = (option: string, index: number) => {
|
||||
]
|
||||
}
|
||||
|
||||
export const setLaunchOptions = (options: string[]) => {
|
||||
const setLaunchOptions = (options: string[]) => {
|
||||
toolStore.state.launchOptions = options
|
||||
}
|
||||
|
||||
export const setLaunchIndex = (index: number) => {
|
||||
const setLaunchIndex = (index: number) => {
|
||||
toolStore.state.launchIndex = index
|
||||
}
|
||||
|
||||
export const setPowerPlan = (plan: number) => {
|
||||
const setPowerPlan = (plan: number) => {
|
||||
toolStore.state.powerPlan = plan
|
||||
}
|
||||
|
||||
export const addLaunchOption = (option: string) => {
|
||||
const addLaunchOption = (option: string) => {
|
||||
// 限制最高10个
|
||||
if (toolStore.state.launchOptions.length >= 10) {
|
||||
return
|
||||
@@ -45,7 +62,7 @@ export const addLaunchOption = (option: string) => {
|
||||
toolStore.state.launchOptions = [...toolStore.state.launchOptions, option]
|
||||
}
|
||||
|
||||
export const resetToolStore = () => {
|
||||
const resetToolStore = () => {
|
||||
setLaunchOptions(defaultValue.launchOptions)
|
||||
setLaunchIndex(defaultValue.launchIndex)
|
||||
setPowerPlan(defaultValue.powerPlan)
|
||||
|
||||
Reference in New Issue
Block a user