分离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 log = require('electron-log')
|
||||||
const Store = require('electron-store')
|
|
||||||
const { defaultSetting, overwriteSetting } = require('./defaultSetting')
|
const { defaultSetting, overwriteSetting } = require('./defaultSetting')
|
||||||
// const apiSource = require('../renderer/utils/music/api-source-info')
|
// const apiSource = require('../renderer/utils/music/api-source-info')
|
||||||
|
const getStore = require('./store')
|
||||||
const defaultHotKey = require('./defaultHotKey')
|
const defaultHotKey = require('./defaultHotKey')
|
||||||
const { dialog, app } = require('electron')
|
|
||||||
const path = require('path')
|
|
||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
exports.isLinux = process.platform == 'linux'
|
exports.isLinux = process.platform == 'linux'
|
||||||
exports.isWin = process.platform == 'win32'
|
exports.isWin = process.platform == 'win32'
|
||||||
|
@ -128,31 +125,10 @@ exports.mergeSetting = (setting, version) => {
|
||||||
* @param {*} setting
|
* @param {*} setting
|
||||||
*/
|
*/
|
||||||
exports.initSetting = () => {
|
exports.initSetting = () => {
|
||||||
let electronStore_list
|
const electronStore_list = getStore('playList')
|
||||||
try {
|
const electronStore_config = getStore('config')
|
||||||
electronStore_list = new Store({
|
const electronStore_downloadList = getStore('downloadList')
|
||||||
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',
|
|
||||||
})
|
|
||||||
let setting = electronStore_config.get('setting')
|
let setting = electronStore_config.get('setting')
|
||||||
if (setting) {
|
if (setting) {
|
||||||
let version = electronStore_config.get('version')
|
let version = electronStore_config.get('version')
|
||||||
|
@ -210,9 +186,7 @@ exports.initSetting = () => {
|
||||||
* 初始化快捷键设置
|
* 初始化快捷键设置
|
||||||
*/
|
*/
|
||||||
exports.initHotKey = () => {
|
exports.initHotKey = () => {
|
||||||
const electronStore_hotKey = new Store({
|
const electronStore_hotKey = getStore('hotKey')
|
||||||
name: 'hotKey',
|
|
||||||
})
|
|
||||||
|
|
||||||
let localConfig = electronStore_hotKey.get('local')
|
let localConfig = electronStore_hotKey.get('local')
|
||||||
if (!localConfig) {
|
if (!localConfig) {
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
const { userApis: defaultUserApis } = require('../config')
|
const { userApis: defaultUserApis } = require('../config')
|
||||||
const Store = require('electron-store')
|
const getStore = require('@common/store')
|
||||||
|
|
||||||
let userApis
|
let userApis
|
||||||
const electronStore_userApi = new Store({
|
|
||||||
name: 'userApi',
|
|
||||||
})
|
|
||||||
|
|
||||||
exports.getUserApis = () => {
|
exports.getUserApis = () => {
|
||||||
|
const electronStore_userApi = getStore('userApi')
|
||||||
if (userApis) return userApis
|
if (userApis) return userApis
|
||||||
userApis = electronStore_userApi.get('userApis')
|
userApis = electronStore_userApi.get('userApis')
|
||||||
if (!userApis) {
|
if (!userApis) {
|
||||||
|
@ -31,7 +29,7 @@ exports.importApi = script => {
|
||||||
script,
|
script,
|
||||||
}
|
}
|
||||||
userApis.push(apiInfo)
|
userApis.push(apiInfo)
|
||||||
electronStore_userApi.set('userApis', userApis)
|
getStore('userApi').set('userApis', userApis)
|
||||||
return apiInfo
|
return apiInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,5 +40,5 @@ exports.removeApi = ids => {
|
||||||
ids.splice(index, 1)
|
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 { mainOn, NAMES: { mainWindow: ipcMainWindowNames }, mainHandle } = require('../../common/ipc')
|
||||||
|
const getStore = require('@common/store')
|
||||||
|
|
||||||
|
|
||||||
const electronStore_data = new Store({
|
mainHandle(ipcMainWindowNames.get_data, async(event, path) => getStore('data').get(path))
|
||||||
name: 'data',
|
|
||||||
})
|
|
||||||
|
|
||||||
mainHandle(ipcMainWindowNames.get_data, async(event, path) => electronStore_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 { mainSend, NAMES: { mainWindow: ipcMainWindowNames }, mainOn, mainHandle } = require('../../common/ipc')
|
||||||
const { mainWindow: MAIN_WINDOW_EVENT_NAME, hotKey: HOT_KEY_EVENT_NAME } = require('../events/_name')
|
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')
|
// const { registerHotkey, unRegisterHotkey } = require('../modules/hotKey/utils')
|
||||||
|
|
||||||
// mainHandle(ipcMainWindowNames.set_hot_key_config, async(event, { action, data }) => {
|
// 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() => ({
|
mainHandle(ipcMainWindowNames.get_hot_key, async() => {
|
||||||
local: electronStore_hotKey.get('local'),
|
const electronStore_hotKey = getStore('hotKey')
|
||||||
global: electronStore_hotKey.get('global'),
|
return {
|
||||||
}))
|
local: electronStore_hotKey.get('local'),
|
||||||
|
global: electronStore_hotKey.get('global'),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
mainOn(ipcMainWindowNames.quit, () => global.lx_event.mainWindow.quit())
|
mainOn(ipcMainWindowNames.quit, () => global.lx_event.mainWindow.quit())
|
||||||
mainOn(ipcMainWindowNames.min_toggle, () => global.lx_event.mainWindow.toggleMinimize())
|
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 { 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) => {
|
mainHandle(ipcMainWindowNames.get_playlist, async(event, isIgnoredError = false) => {
|
||||||
if (!electronStore_list) {
|
const electronStore_list = getStore('playList', isIgnoredError)
|
||||||
electronStore_list = new Store({
|
|
||||||
name: 'playList',
|
|
||||||
clearInvalidConfig: !isIgnoredError,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if (!electronStore_downloadList) {
|
|
||||||
electronStore_downloadList = new Store({
|
|
||||||
name: 'downloadList',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
defaultList: electronStore_list.get('defaultList'),
|
defaultList: electronStore_list.get('defaultList'),
|
||||||
loveList: electronStore_list.get('loveList'),
|
loveList: electronStore_list.get('loveList'),
|
||||||
userList: electronStore_list.get('userList'),
|
userList: electronStore_list.get('userList'),
|
||||||
downloadList: electronStore_downloadList.get('list'),
|
downloadList: getStore('downloadList').get('list'),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleSaveList = ({ defaultList, loveList, userList }) => {
|
const handleSaveList = ({ defaultList, loveList, userList }) => {
|
||||||
if (!electronStore_list) return
|
|
||||||
let data = {}
|
let data = {}
|
||||||
if (defaultList != null) data.defaultList = defaultList
|
if (defaultList != null) data.defaultList = defaultList
|
||||||
if (loveList != null) data.loveList = loveList
|
if (loveList != null) data.loveList = loveList
|
||||||
if (userList != null) data.userList = userList
|
if (userList != null) data.userList = userList
|
||||||
electronStore_list.set(data)
|
getStore('playList').set(data)
|
||||||
}
|
}
|
||||||
mainOn(ipcMainWindowNames.save_playlist, (event, { type, data }) => {
|
mainOn(ipcMainWindowNames.save_playlist, (event, { type, data }) => {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -40,7 +26,7 @@ mainOn(ipcMainWindowNames.save_playlist, (event, { type, data }) => {
|
||||||
handleSaveList(data)
|
handleSaveList(data)
|
||||||
break
|
break
|
||||||
case 'downloadList':
|
case 'downloadList':
|
||||||
electronStore_downloadList && electronStore_downloadList.set('list', data)
|
getStore('downloadList').set('list', data)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,22 +1,18 @@
|
||||||
const Store = require('electron-store')
|
|
||||||
const { windowSizeList } = require('../../common/config')
|
const { windowSizeList } = require('../../common/config')
|
||||||
const { objectDeepMerge, throttle, initSetting } = require('../../common/utils')
|
const { objectDeepMerge, throttle, initSetting } = require('../../common/utils')
|
||||||
|
const getStore = require('@common/store')
|
||||||
|
|
||||||
exports.getWindowSizeInfo = ({ windowSizeId = 1 } = {}) => {
|
exports.getWindowSizeInfo = ({ windowSizeId = 1 } = {}) => {
|
||||||
return windowSizeList.find(i => i.id === windowSizeId) || windowSizeList[0]
|
return windowSizeList.find(i => i.id === windowSizeId) || windowSizeList[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
const electronStore_config = new Store({
|
|
||||||
name: 'config',
|
|
||||||
})
|
|
||||||
exports.getAppSetting = () => {
|
exports.getAppSetting = () => {
|
||||||
return electronStore_config.get('setting')
|
return getStore('config').get('setting')
|
||||||
}
|
}
|
||||||
|
|
||||||
const electronStore_hotKey = new Store({
|
|
||||||
name: 'hotKey',
|
|
||||||
})
|
|
||||||
exports.getAppHotKeyConfig = () => {
|
exports.getAppHotKeyConfig = () => {
|
||||||
|
const electronStore_hotKey = getStore('hotKey')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
global: electronStore_hotKey.get('global'),
|
global: electronStore_hotKey.get('global'),
|
||||||
local: electronStore_hotKey.get('local'),
|
local: electronStore_hotKey.get('local'),
|
||||||
|
@ -25,7 +21,7 @@ exports.getAppHotKeyConfig = () => {
|
||||||
const saveHotKeyConfig = throttle(config => {
|
const saveHotKeyConfig = throttle(config => {
|
||||||
for (const key of Object.keys(config)) {
|
for (const key of Object.keys(config)) {
|
||||||
global.appHotKey.config[key] = config[key]
|
global.appHotKey.config[key] = config[key]
|
||||||
electronStore_hotKey.set(key, config[key])
|
getStore('hotKey').set(key, config[key])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
exports.saveAppHotKeyConfig = config => {
|
exports.saveAppHotKeyConfig = config => {
|
||||||
|
@ -37,7 +33,7 @@ exports.saveAppHotKeyConfig = config => {
|
||||||
// })
|
// })
|
||||||
exports.updateSetting = (settings) => {
|
exports.updateSetting = (settings) => {
|
||||||
objectDeepMerge(global.appSetting, settings)
|
objectDeepMerge(global.appSetting, settings)
|
||||||
electronStore_config.set('setting', global.appSetting)
|
getStore('config').set('setting', global.appSetting)
|
||||||
exports.initSetting()
|
exports.initSetting()
|
||||||
}
|
}
|
||||||
exports.initSetting = () => {
|
exports.initSetting = () => {
|
||||||
|
|
Loading…
Reference in New Issue