切换到简易的 store 实现

This commit is contained in:
lyswhut
2023-12-04 12:34:12 +08:00
parent 182a3076fc
commit bee2790af3
4 changed files with 101 additions and 343 deletions
+1 -1
View File
@@ -121,7 +121,7 @@ export const updateSetting = (setting?: Partial<LX.AppSetting>, isInit: boolean
result.setting.version = defaultSetting.version
electronStore_config.set({ version: result.setting.version, setting: result.setting })
electronStore_config.override({ version: result.setting.version, setting: result.setting })
return result
}
+69 -8
View File
@@ -1,4 +1,4 @@
import Store from 'electron-store'
// import { writeFileSync } from 'atomically'
import { dialog, shell } from 'electron'
import path from 'node:path'
import fs from 'node:fs'
@@ -8,18 +8,79 @@ type Stores = Record<string, Store>
const stores: Stores = {}
class Store {
private readonly filePath: string
private readonly dirPath: string
private store: Record<string, any>
private writeFile() {
const tempPath = this.filePath + '.' + Math.random().toString().substring(2, 10) + '.temp'
try {
fs.writeFileSync(tempPath, JSON.stringify(this.store, null, '\t'), 'utf8')
} catch (err: any) {
if (err.code === 'ENOENT') {
fs.mkdirSync(this.dirPath, { recursive: true })
fs.writeFileSync(tempPath, JSON.stringify(this.store, null, '\t'), 'utf8')
} else throw err
}
fs.renameSync(tempPath, this.filePath)
}
constructor(filePath: string, clearInvalidConfig: boolean = false) {
this.filePath = filePath
this.dirPath = path.dirname(this.filePath)
let store: Record<string, any>
if (fs.existsSync(this.filePath)) {
if (clearInvalidConfig) {
try {
store = JSON.parse(fs.readFileSync(this.filePath, 'utf8'))
} catch {
store = {}
}
} else store = JSON.parse(fs.readFileSync(this.filePath, 'utf8'))
} else store = {}
if (typeof store != 'object') {
if (clearInvalidConfig) store = {}
else throw new Error('parse data error: ' + String(store))
}
this.store = store
}
get<Value>(key: string): Value {
return this.store[key]
}
has(key: string): boolean {
return key in this.store
}
set(key: string, value: any) {
this.store[key] = value
this.writeFile()
}
override(value: Record<string, any>) {
this.store = value
this.writeFile()
}
}
/**
* 获取 Store 对象
* @param {string} name store 名
* @param {boolean|undefined} isIgnoredError 是否忽略错误
* @param {boolean|undefined} isShowErrorAlert=true 是否显示错误弹窗
* @param name store 名
* @param isIgnoredError 是否忽略错误
* @param isShowErrorAlert=true 是否显示错误弹窗
* @returns Store
*/
export default (name: string, isIgnoredError = true, isShowErrorAlert = true): Store => {
if (stores[name]) return stores[name]
let store: Store
const storePath = path.join(global.lxDataPath, name + '.json')
try {
store = stores[name] = new Store({ name, clearInvalidConfig: false, cwd: global.lxDataPath })
store = stores[name] = new Store(storePath, false)
} catch (err: any) {
const error = err as Error
log.error(error)
@@ -27,8 +88,8 @@ export default (name: string, isIgnoredError = true, isShowErrorAlert = true): S
if (!isIgnoredError) throw error
const backPath = path.join(global.lxDataPath, name + '.json.bak')
fs.renameSync(path.join(global.lxDataPath, name + '.json'), backPath)
const backPath = storePath + '.bak'
fs.renameSync(storePath, backPath)
if (isShowErrorAlert) {
dialog.showMessageBoxSync({
type: 'error',
@@ -39,7 +100,7 @@ export default (name: string, isIgnoredError = true, isShowErrorAlert = true): S
}
store = new Store({ name, clearInvalidConfig: true })
store = new Store(storePath, true)
}
return store
}