mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-01 08:39:35 +08:00
🔨 Refactor: rebuild multi-config communication way
This commit is contained in:
@@ -45,8 +45,8 @@
|
||||
<script lang="ts">
|
||||
import { Component, Vue } from 'vue-property-decorator'
|
||||
import dayjs from 'dayjs'
|
||||
import { completeUploaderMetaConfig } from '../utils/uploader'
|
||||
import mixin from '@/utils/ConfirmButtonMixin'
|
||||
import { IRPCActionType } from '~/universal/types/enum'
|
||||
|
||||
@Component({
|
||||
name: 'UploaderConfigPage',
|
||||
@@ -58,9 +58,7 @@ export default class extends Vue {
|
||||
defaultConfigId = '';
|
||||
|
||||
async selectItem (id: string) {
|
||||
await this.saveConfig(`uploader.${this.type}.defaultId`, id)
|
||||
const activeConfig = this.curConfigList.find(i => i._id === id)
|
||||
await this.saveConfig(`picBed.${this.type}`, activeConfig)
|
||||
await this.triggerRPC<void>(IRPCActionType.SELECT_UPLOADER, this.type, id)
|
||||
this.defaultConfigId = id
|
||||
}
|
||||
|
||||
@@ -70,39 +68,9 @@ export default class extends Vue {
|
||||
}
|
||||
|
||||
async getCurrentConfigList () {
|
||||
const curUploaderConfig = await this.getConfig<IStringKeyMap>(`uploader.${this.type}`) ?? {}
|
||||
let curConfigList = curUploaderConfig?.configList
|
||||
this.defaultConfigId = curUploaderConfig?.defaultId
|
||||
|
||||
if (!curConfigList) {
|
||||
curConfigList = await this.fixUploaderConfig()
|
||||
}
|
||||
|
||||
this.curConfigList = curConfigList
|
||||
}
|
||||
|
||||
async fixUploaderConfig (): Promise<IStringKeyMap[]> {
|
||||
const curUploaderConfig = await this.getConfig<IStringKeyMap>(`picBed.${this.type}`) ?? {}
|
||||
|
||||
if (!curUploaderConfig._id) {
|
||||
Object.assign(
|
||||
curUploaderConfig,
|
||||
completeUploaderMetaConfig(curUploaderConfig)
|
||||
)
|
||||
}
|
||||
|
||||
const curUploaderConfigList = [curUploaderConfig]
|
||||
await this.saveConfig(`uploader.${this.type}`, {
|
||||
configList: curUploaderConfigList,
|
||||
defaultId: curUploaderConfig._id
|
||||
})
|
||||
|
||||
// fix exist config
|
||||
await this.saveConfig(`picBed.${this.type}`, curUploaderConfig)
|
||||
|
||||
this.defaultConfigId = curUploaderConfig._id
|
||||
|
||||
return curUploaderConfigList
|
||||
const configList = await this.triggerRPC<IUploaderConfigItem>(IRPCActionType.GET_PICBED_CONFIG_LIST, this.type)
|
||||
this.curConfigList = configList?.configList ?? []
|
||||
this.defaultConfigId = configList?.defaultId ?? ''
|
||||
}
|
||||
|
||||
openEditPage (configId: string) {
|
||||
@@ -123,15 +91,10 @@ export default class extends Vue {
|
||||
}
|
||||
|
||||
async deleteConfig (id: string) {
|
||||
if (this.curConfigList.length <= 1) return
|
||||
const updatedConfigList = this.curConfigList.filter(i => i._id !== id)
|
||||
|
||||
if (id === this.defaultConfigId) {
|
||||
await this.selectItem(updatedConfigList[0]._id)
|
||||
}
|
||||
|
||||
await this.saveConfig(`uploader.${this.type}.configList`, updatedConfigList)
|
||||
this.curConfigList = updatedConfigList
|
||||
const res = await this.triggerRPC<IUploaderConfigItem | undefined>(IRPCActionType.DELETE_PICBED_CONFIG, this.type, id)
|
||||
if (!res) return
|
||||
this.curConfigList = res.configList
|
||||
this.defaultConfigId = res.defaultId
|
||||
}
|
||||
|
||||
addNewConfig () {
|
||||
|
||||
@@ -31,8 +31,7 @@ import {
|
||||
ipcRenderer,
|
||||
IpcRendererEvent
|
||||
} from 'electron'
|
||||
import { completeUploaderMetaConfig } from '@/utils/uploader'
|
||||
import { trimValues } from '@/utils/common'
|
||||
import { IRPCActionType } from '~/universal/types/enum'
|
||||
|
||||
@Component({
|
||||
name: 'OtherPicBed',
|
||||
@@ -55,22 +54,7 @@ export default class extends Vue {
|
||||
// @ts-ignore
|
||||
const result = await this.$refs.configForm.validate()
|
||||
if (result !== false) {
|
||||
const configListConfigPath = `uploader.${this.type}.configList`
|
||||
const configList = await this.getConfig<IStringKeyMap[]>(configListConfigPath)
|
||||
// Finds the specified item from the config array and modifies it
|
||||
const existItem = configList?.find(item => item._id === result._id)
|
||||
// edit
|
||||
if (existItem) {
|
||||
Object.assign(existItem, trimValues(result), {
|
||||
_updatedAt: Date.now()
|
||||
})
|
||||
} else { // add new
|
||||
configList?.push(trimValues(completeUploaderMetaConfig(result)))
|
||||
}
|
||||
|
||||
await this.saveConfig(configListConfigPath, configList)
|
||||
existItem && await this.shouldUpdateDefaultConfig(existItem)
|
||||
|
||||
await this.triggerRPC<void>(IRPCActionType.UPDATE_UPLOADER_CONFIG, this.type, result?._id, result)
|
||||
const successNotification = new Notification(this.$T('SETTINGS_RESULT'), {
|
||||
body: this.$T('TIPS_SET_SUCCEED')
|
||||
})
|
||||
|
||||
@@ -37,11 +37,12 @@ export default class extends Vue {
|
||||
|
||||
/**
|
||||
* trigger RPC action
|
||||
* TODO: create an isolate rpc handler
|
||||
*/
|
||||
triggerRPC<T> (action: IRPCActionType, ...args: any[]): Promise<T | undefined> {
|
||||
triggerRPC<T> (action: IRPCActionType, ...args: any[]): Promise<T | null> {
|
||||
return new Promise((resolve) => {
|
||||
const callbackId = uuid()
|
||||
const callback = (event: IpcRendererEvent, data: T | undefined, returnActionType: IRPCActionType, returnCallbackId: string) => {
|
||||
const callback = (event: IpcRendererEvent, data: T | null, returnActionType: IRPCActionType, returnCallbackId: string) => {
|
||||
if (returnCallbackId === callbackId && returnActionType === action) {
|
||||
resolve(data)
|
||||
ipcRenderer.removeListener(RPC_ACTIONS, callback)
|
||||
|
||||
Reference in New Issue
Block a user