分离store逻辑
parent
a4d46fd501
commit
c663cdbe0d
|
@ -0,0 +1,35 @@
|
|||
const Store = require('electron-store')
|
||||
const { dialog, app, shell } = require('electron')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const log = require('electron-log')
|
||||
|
||||
const stores = {}
|
||||
|
||||
/**
|
||||
* 获取 Store 对象
|
||||
* @param {*} name store 名
|
||||
* @param {*} isIgnoredError 是否忽略错误
|
||||
* @returns Store
|
||||
*/
|
||||
module.exports = (name, isIgnoredError = true) => {
|
||||
if (stores[name]) return stores[name]
|
||||
let store
|
||||
try {
|
||||
store = stores[name] = new Store({ name, clearInvalidConfig: false })
|
||||
} catch (error) {
|
||||
log.error(error)
|
||||
if (!isIgnoredError) throw error
|
||||
|
||||
const backPath = path.join(app.getPath('userData'), name + '.json.bak')
|
||||
fs.copyFileSync(path.join(app.getPath('userData'), name + '.json'), backPath)
|
||||
dialog.showMessageBoxSync({
|
||||
type: 'error',
|
||||
message: name + ' data load error',
|
||||
detail: `We have helped you back up the old ${name} file to: ${backPath}\nYou can try to repair and restore it manually\n\nError detail: ${error.message}`,
|
||||
})
|
||||
store = new Store({ name, clearInvalidConfig: true })
|
||||
shell.showItemInFolder(backPath)
|
||||
}
|
||||
return store
|
||||
}
|
|
@ -1,11 +1,8 @@
|
|||
const log = require('electron-log')
|
||||
const Store = require('electron-store')
|
||||
const { defaultSetting, overwriteSetting } = require('./defaultSetting')
|
||||
// const apiSource = require('../renderer/utils/music/api-source-info')
|
||||
const getStore = require('./store')
|
||||
const defaultHotKey = require('./defaultHotKey')
|
||||
const { dialog, app } = require('electron')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
|
||||
exports.isLinux = process.platform == 'linux'
|
||||
exports.isWin = process.platform == 'win32'
|
||||
|
@ -128,31 +125,10 @@ exports.mergeSetting = (setting, version) => {
|
|||
* @param {*} setting
|
||||
*/
|
||||
exports.initSetting = () => {
|
||||
let electronStore_list
|
||||
try {
|
||||
electronStore_list = new Store({
|
||||
name: 'playList',
|
||||
clearInvalidConfig: false,
|
||||
})
|
||||
} catch (error) {
|
||||
log.error(error)
|
||||
const backPath = path.join(app.getPath('userData'), 'playList.json.bak')
|
||||
fs.copyFileSync(path.join(app.getPath('userData'), 'playList.json'), backPath)
|
||||
dialog.showMessageBoxSync({
|
||||
type: 'error',
|
||||
message: 'Playlist data loading error',
|
||||
detail: `We have helped you back up the old list file to ${backPath}\nYou can try to repair and restore it manually\n\nError detail: ${error.message}`,
|
||||
})
|
||||
electronStore_list = new Store({
|
||||
name: 'playList',
|
||||
})
|
||||
}
|
||||
const electronStore_config = new Store({
|
||||
name: 'config',
|
||||
})
|
||||
const electronStore_downloadList = new Store({
|
||||
name: 'downloadList',
|
||||
})
|
||||
const electronStore_list = getStore('playList')
|
||||
const electronStore_config = getStore('config')
|
||||
const electronStore_downloadList = getStore('downloadList')
|
||||
|
||||
let setting = electronStore_config.get('setting')
|
||||
if (setting) {
|
||||
let version = electronStore_config.get('version')
|
||||
|
@ -210,9 +186,7 @@ exports.initSetting = () => {
|
|||
* 初始化快捷键设置
|
||||
*/
|
||||
exports.initHotKey = () => {
|
||||
const electronStore_hotKey = new Store({
|
||||
name: 'hotKey',
|
||||
})
|
||||
const electronStore_hotKey = getStore('hotKey')
|
||||
|
||||
let localConfig = electronStore_hotKey.get('local')
|
||||
if (!localConfig) {
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
const { userApis: defaultUserApis } = require('../config')
|
||||
const Store = require('electron-store')
|
||||
const getStore = require('@common/store')
|
||||
|
||||
let userApis
|
||||
const electronStore_userApi = new Store({
|
||||
name: 'userApi',
|
||||
})
|
||||
|
||||
exports.getUserApis = () => {
|
||||
const electronStore_userApi = getStore('userApi')
|
||||
if (userApis) return userApis
|
||||
userApis = electronStore_userApi.get('userApis')
|
||||
if (!userApis) {
|
||||
|
@ -31,7 +29,7 @@ exports.importApi = script => {
|
|||
script,
|
||||
}
|
||||
userApis.push(apiInfo)
|
||||
electronStore_userApi.set('userApis', userApis)
|
||||
getStore('userApi').set('userApis', userApis)
|
||||
return apiInfo
|
||||
}
|
||||
|
||||
|
@ -42,5 +40,5 @@ exports.removeApi = ids => {
|
|||
ids.splice(index, 1)
|
||||
}
|
||||
}
|
||||
electronStore_userApi.set('userApis', userApis)
|
||||
getStore('userApi').set('userApis', userApis)
|
||||
}
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
const Store = require('electron-store')
|
||||
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames }, mainHandle } = require('../../common/ipc')
|
||||
const getStore = require('@common/store')
|
||||
|
||||
|
||||
const electronStore_data = new Store({
|
||||
name: 'data',
|
||||
})
|
||||
|
||||
mainHandle(ipcMainWindowNames.get_data, async(event, path) => electronStore_data.get(path))
|
||||
mainHandle(ipcMainWindowNames.get_data, async(event, path) => getStore('data').get(path))
|
||||
|
||||
|
||||
mainOn(ipcMainWindowNames.save_data, (event, { path, data }) => electronStore_data.set(path, data))
|
||||
mainOn(ipcMainWindowNames.save_data, (event, { path, data }) => getStore('data').set(path, data))
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
const Store = require('electron-store')
|
||||
const { mainSend, NAMES: { mainWindow: ipcMainWindowNames }, mainOn, mainHandle } = require('../../common/ipc')
|
||||
const { mainWindow: MAIN_WINDOW_EVENT_NAME, hotKey: HOT_KEY_EVENT_NAME } = require('../events/_name')
|
||||
const getStore = require('@common/store')
|
||||
|
||||
|
||||
const electronStore_hotKey = new Store({
|
||||
name: 'hotKey',
|
||||
})
|
||||
// const { registerHotkey, unRegisterHotkey } = require('../modules/hotKey/utils')
|
||||
|
||||
// mainHandle(ipcMainWindowNames.set_hot_key_config, async(event, { action, data }) => {
|
||||
|
@ -19,10 +17,13 @@ const electronStore_hotKey = new Store({
|
|||
// }
|
||||
// })
|
||||
|
||||
mainHandle(ipcMainWindowNames.get_hot_key, async() => ({
|
||||
local: electronStore_hotKey.get('local'),
|
||||
global: electronStore_hotKey.get('global'),
|
||||
}))
|
||||
mainHandle(ipcMainWindowNames.get_hot_key, async() => {
|
||||
const electronStore_hotKey = getStore('hotKey')
|
||||
return {
|
||||
local: electronStore_hotKey.get('local'),
|
||||
global: electronStore_hotKey.get('global'),
|
||||
}
|
||||
})
|
||||
|
||||
mainOn(ipcMainWindowNames.quit, () => global.lx_event.mainWindow.quit())
|
||||
mainOn(ipcMainWindowNames.min_toggle, () => global.lx_event.mainWindow.toggleMinimize())
|
||||
|
|
|
@ -1,38 +1,24 @@
|
|||
const Store = require('electron-store')
|
||||
const { mainOn, NAMES: { mainWindow: ipcMainWindowNames }, mainHandle } = require('../../common/ipc')
|
||||
const getStore = require('@common/store')
|
||||
|
||||
|
||||
let electronStore_list
|
||||
let electronStore_downloadList
|
||||
|
||||
mainHandle(ipcMainWindowNames.get_playlist, async(event, isIgnoredError = false) => {
|
||||
if (!electronStore_list) {
|
||||
electronStore_list = new Store({
|
||||
name: 'playList',
|
||||
clearInvalidConfig: !isIgnoredError,
|
||||
})
|
||||
}
|
||||
if (!electronStore_downloadList) {
|
||||
electronStore_downloadList = new Store({
|
||||
name: 'downloadList',
|
||||
})
|
||||
}
|
||||
const electronStore_list = getStore('playList', isIgnoredError)
|
||||
|
||||
return {
|
||||
defaultList: electronStore_list.get('defaultList'),
|
||||
loveList: electronStore_list.get('loveList'),
|
||||
userList: electronStore_list.get('userList'),
|
||||
downloadList: electronStore_downloadList.get('list'),
|
||||
downloadList: getStore('downloadList').get('list'),
|
||||
}
|
||||
})
|
||||
|
||||
const handleSaveList = ({ defaultList, loveList, userList }) => {
|
||||
if (!electronStore_list) return
|
||||
let data = {}
|
||||
if (defaultList != null) data.defaultList = defaultList
|
||||
if (loveList != null) data.loveList = loveList
|
||||
if (userList != null) data.userList = userList
|
||||
electronStore_list.set(data)
|
||||
getStore('playList').set(data)
|
||||
}
|
||||
mainOn(ipcMainWindowNames.save_playlist, (event, { type, data }) => {
|
||||
switch (type) {
|
||||
|
@ -40,7 +26,7 @@ mainOn(ipcMainWindowNames.save_playlist, (event, { type, data }) => {
|
|||
handleSaveList(data)
|
||||
break
|
||||
case 'downloadList':
|
||||
electronStore_downloadList && electronStore_downloadList.set('list', data)
|
||||
getStore('downloadList').set('list', data)
|
||||
break
|
||||
}
|
||||
})
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
const Store = require('electron-store')
|
||||
const { windowSizeList } = require('../../common/config')
|
||||
const { objectDeepMerge, throttle, initSetting } = require('../../common/utils')
|
||||
const getStore = require('@common/store')
|
||||
|
||||
exports.getWindowSizeInfo = ({ windowSizeId = 1 } = {}) => {
|
||||
return windowSizeList.find(i => i.id === windowSizeId) || windowSizeList[0]
|
||||
}
|
||||
|
||||
const electronStore_config = new Store({
|
||||
name: 'config',
|
||||
})
|
||||
exports.getAppSetting = () => {
|
||||
return electronStore_config.get('setting')
|
||||
return getStore('config').get('setting')
|
||||
}
|
||||
|
||||
const electronStore_hotKey = new Store({
|
||||
name: 'hotKey',
|
||||
})
|
||||
exports.getAppHotKeyConfig = () => {
|
||||
const electronStore_hotKey = getStore('hotKey')
|
||||
|
||||
return {
|
||||
global: electronStore_hotKey.get('global'),
|
||||
local: electronStore_hotKey.get('local'),
|
||||
|
@ -25,7 +21,7 @@ exports.getAppHotKeyConfig = () => {
|
|||
const saveHotKeyConfig = throttle(config => {
|
||||
for (const key of Object.keys(config)) {
|
||||
global.appHotKey.config[key] = config[key]
|
||||
electronStore_hotKey.set(key, config[key])
|
||||
getStore('hotKey').set(key, config[key])
|
||||
}
|
||||
})
|
||||
exports.saveAppHotKeyConfig = config => {
|
||||
|
@ -37,7 +33,7 @@ exports.saveAppHotKeyConfig = config => {
|
|||
// })
|
||||
exports.updateSetting = (settings) => {
|
||||
objectDeepMerge(global.appSetting, settings)
|
||||
electronStore_config.set('setting', global.appSetting)
|
||||
getStore('config').set('setting', global.appSetting)
|
||||
exports.initSetting()
|
||||
}
|
||||
exports.initSetting = () => {
|
||||
|
|
Loading…
Reference in New Issue