Skip to content

Commit

Permalink
✨ Feature(custom): optimize get config speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed Jun 9, 2024
1 parent 19c12b6 commit 106290f
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 55 deletions.
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import db from '@/utils/db'
import { T } from '@/i18n/index'
import { store } from '@/store'
import { initTalkingData } from '@/utils/analytic'
import { getConfig, saveConfig, sendToMain, triggerRPC } from '@/utils/dataSender'
import { getConfig, saveConfig, triggerRPC } from '@/utils/dataSender'
import { mainMixin } from '@/utils/mainMixin'
import { dragMixin } from '@/utils/mixin'
import { sendToMain } from '@/utils/common'

webFrame.setVisualZoomLevelLimits(1, 1)

Expand Down
2 changes: 1 addition & 1 deletion src/main/events/ipcList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export default {
ipcMain.on(GET_PICBEDS, (evt: IpcMainEvent) => {
const picBeds = getPicBeds()
evt.sender.send(GET_PICBEDS, picBeds)
evt.returnValue = picBeds
// evt.returnValue = picBeds
})

ipcMain.on(TOGGLE_SHORTKEY_MODIFIED_MODE, (_: IpcMainEvent, val: boolean) => {
Expand Down
22 changes: 15 additions & 7 deletions src/main/events/picgoCoreIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
OPEN_WINDOW,
GET_LANGUAGE_LIST,
SET_CURRENT_LANGUAGE,
GET_CURRENT_LANGUAGE
GET_CURRENT_LANGUAGE,
PICGO_GET_CONFIG_SYNC
} from '#/events/constants'
import { configPaths } from '#/utils/configPaths'
import { IPasteStyle, IPicGoHelperType, IWindowList } from '#/types/enum'
Expand Down Expand Up @@ -211,7 +212,7 @@ const handlePluginUpdate = async (fullName: string | string[]) => {
}

const handleUpdateAllPlugin = () => {
ipcMain.on('updateAllPlugin', async (event: IpcMainEvent, list: string[]) => {
ipcMain.on('updateAllPlugin', async (_: IpcMainEvent, list: string[]) => {
handlePluginUpdate(list)
})
}
Expand Down Expand Up @@ -249,7 +250,7 @@ const handleGetPicBedConfig = () => {

// TODO: remove it
const handlePluginActions = () => {
ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => {
ipcMain.on('pluginActions', (_: IpcMainEvent, name: string, label: string) => {
const plugin = picgo.pluginLoader.getPlugin(name)
if (plugin?.guiMenu?.(picgo)?.length) {
const menu: GuiMenuItem[] = plugin.guiMenu(picgo)
Expand All @@ -263,23 +264,29 @@ const handlePluginActions = () => {
}

const handleRemoveFiles = () => {
ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => {
ipcMain.on('removeFiles', (_: IpcMainEvent, files: ImgInfo[]) => {
setTimeout(() => {
picgo.emit('remove', files, GuiApi.getInstance())
}, 500)
})
}

const handlePicGoSaveConfig = () => {
ipcMain.on(PICGO_SAVE_CONFIG, (event: IpcMainEvent, data: IObj) => {
ipcMain.on(PICGO_SAVE_CONFIG, (_: IpcMainEvent, data: IObj) => {
picgo.saveConfig(data)
})
}

const handlePicGoGetConfig = () => {
ipcMain.on(PICGO_GET_CONFIG, (event: IpcMainEvent, key: string | undefined, callbackId: string) => {
ipcMain.handle(PICGO_GET_CONFIG, (_, key: string | undefined) => {
return picgo.getConfig(key)
})
}

const handlePicGoGetConfigSync = () => {
ipcMain.on(PICGO_GET_CONFIG_SYNC, (event: IpcMainEvent, key: string | undefined) => {
const result = picgo.getConfig(key)
event.sender.send(PICGO_GET_CONFIG, result, callbackId)
event.returnValue = result
})
}

Expand Down Expand Up @@ -436,6 +443,7 @@ export default {
handleRemoveFiles()
handlePicGoSaveConfig()
handlePicGoGetConfig()
handlePicGoGetConfigSync()
handlePicGoGalleryDB()
handleImportLocalPlugin()
handleUpdateAllPlugin()
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/InputBoxDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { ref, reactive, onBeforeUnmount, onBeforeMount } from 'vue'
import { T as $T } from '@/i18n/index'
import $bus from '@/utils/bus'
import { sendToMain } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import {
SHOW_INPUT_BOX,
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/layouts/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ import { onBeforeRouteUpdate, useRouter } from 'vue-router'
import InputBoxDialog from '@/components/InputBoxDialog.vue'
import { T as $T } from '@/i18n/index'
import * as config from '@/router/config'
import { getConfig, saveConfig, sendToMain } from '@/utils/dataSender'
import { openURL } from '@/utils/common'
import { getConfig, saveConfig } from '@/utils/dataSender'
import { openURL, sendToMain } from '@/utils/common'
import {
MINIMIZE_WINDOW,
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/manage/pages/manageMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ import { ref, reactive, computed, onBeforeMount, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { supportedPicBedList } from '@/manage/utils/constants'
import { invokeToMain } from '@/manage/utils/dataSender'
import { useManageStore } from '@/manage/store/manageStore'
import { newBucketConfig } from '@/manage/utils/newBucketConfig'
import { T as $T } from '@/i18n'
import { invokeToMain } from '@/utils/common'
const manageStore = useManageStore() as any
const route = useRoute()
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/manage/pages/manageSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,12 @@ import { ref, onBeforeMount, watch } from 'vue'
import DynamicSwitch from '@/manage/components/DynamicSwitch.vue'
import { fileCacheDbInstance } from '@/manage/store/bucketFileDb'
import { formatFileSize, customRenameFormatTable } from '@/manage/utils/common'
import { getConfig, saveConfig, invokeToMain } from '@/manage/utils/dataSender'
import { getConfig, saveConfig } from '@/manage/utils/dataSender'
import { T as $T } from '@/i18n'
import { selectDownloadFolder } from '#/utils/static'
import { invokeToMain } from '@/utils/common'
const form = ref<IStringKeyMap>({
timestampRename: false,
Expand Down
10 changes: 0 additions & 10 deletions src/renderer/manage/utils/dataSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,3 @@ export function saveConfig (_config: IObj | string, value?: any) {
export function removeConfig (key: string, propName: string) {
ipcRenderer.send(PICLIST_MANAGE_REMOVE_CONFIG, key, propName)
}

export function sendToMain (channel: string, ...args: any[]) {
const data = getRawData(args)
ipcRenderer.send(channel, ...data)
}

export function invokeToMain (channel: string, ...args: any[]) {
const data = getRawData(args)
return ipcRenderer.invoke(channel, ...data)
}
3 changes: 2 additions & 1 deletion src/renderer/pages/Gallery.vue
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ import type { IResult } from '@picgo/store/dist/types'
import ALLApi from '@/apis/allApi'
import { T as $T } from '@/i18n/index'
import { customRenameFormatTable, customStrMatch, customStrReplace } from '@/manage/utils/common'
import { getConfig, saveConfig, sendToMain } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import { getConfig, saveConfig } from '@/utils/dataSender'
import $$db from '@/utils/db'
import { PASTE_TEXT, GET_PICBEDS } from '#/events/constants'
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/MiniPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import { IConfig } from 'piclist'
import { onBeforeUnmount, onBeforeMount, ref, watch } from 'vue'
import { T as $T } from '@/i18n/index'
import { invokeToMain } from '@/manage/utils/dataSender'
import { getConfig, sendToMain } from '@/utils/dataSender'
import { sendToMain, invokeToMain } from '@/utils/common'
import { getConfig } from '@/utils/dataSender'
import { SHOW_MINI_PAGE_MENU, SET_MINI_WINDOW_POS } from '#/events/constants'
import { isUrl } from '#/utils/common'
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/pages/PicGoSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1388,9 +1388,8 @@ import { useRouter } from 'vue-router'
import ImageProcessSetting from '@/components/ImageProcessSetting.vue'
import { i18nManager, T as $T } from '@/i18n/index'
import { buildInRenameFormatTable } from '@/manage/utils/common'
import { invokeToMain } from '@/manage/utils/dataSender'
import { SHORTKEY_PAGE } from '@/router/config'
import { getConfig, saveConfig, sendRPC, sendToMain } from '@/utils/dataSender'
import { getConfig, saveConfig, sendRPC } from '@/utils/dataSender'
import { PICGO_OPEN_FILE, PICGO_OPEN_DIRECTORY, OPEN_URL, GET_PICBEDS, HIDE_DOCK } from '#/events/constants'
import { II18nLanguage, IRPCActionType, ISartMode } from '#/types/enum'
Expand All @@ -1399,6 +1398,7 @@ import { configPaths, ISartModeValues } from '#/utils/configPaths'
import { getLatestVersion } from '#/utils/getLatestVersion'
import pkg from 'root/package.json'
import { invokeToMain, sendToMain } from '@/utils/common'
const $router = useRouter()
const activeName = ref<'system' | 'syncAndConfigure' | 'upload' | 'advanced' | 'upadte'>('system')
Expand All @@ -1422,7 +1422,7 @@ const languageList = i18nManager.languageList.map(item => ({
}))
const startModeList = Object.values(ISartMode).map(item => ({
label: $T(`SETTINGS_START_MODE_${item.toUpperCase().replace(/_/g, '')}` as any),
label: $T(`SETTINGS_START_MODE_${item.toUpperCase().replace(/-/g, '_')}` as any),
value: item
}))
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pages/Plugin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ import { computed, ref, onBeforeMount, onBeforeUnmount, watch, onMounted, reacti
import ConfigForm from '@/components/ConfigFormForPlugin.vue'
import { T as $T } from '@/i18n/index'
import { getConfig, saveConfig, sendRPC, sendToMain } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import { getConfig, saveConfig, sendRPC } from '@/utils/dataSender'
import {
OPEN_URL,
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/pages/RenamePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { onBeforeUnmount, onBeforeMount, ref, reactive } from 'vue'
import { useIPCOn } from '@/hooks/useIPC'
import { T as $T } from '@/i18n/index'
import { sendToMain } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import { GET_RENAME_FILE_NAME, RENAME_FILE_NAME } from '#/events/constants'
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pages/ShortKey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ import { ipcRenderer, IpcRendererEvent } from 'electron'
import { onBeforeUnmount, onBeforeMount, ref, watch } from 'vue'
import { T as $T } from '@/i18n'
import { getConfig, sendToMain } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import { getConfig } from '@/utils/dataSender'
import keyBinding from '@/utils/key-binding'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pages/TrayPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ import { reactive, ref, onBeforeUnmount, onBeforeMount } from 'vue'
import { IResult } from '@picgo/store/dist/types'
import { T as $T } from '@/i18n/index'
import { getConfig, sendToMain } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import { getConfig } from '@/utils/dataSender'
import $$db from '@/utils/db'
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pages/Upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ import ImageProcessSetting from '@/components/ImageProcessSetting.vue'
import { T as $T } from '@/i18n'
import { PICBEDS_PAGE } from '@/router/config'
import $bus from '@/utils/bus'
import { getConfig, saveConfig, sendToMain, triggerRPC } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import { getConfig, saveConfig, triggerRPC } from '@/utils/dataSender'
import {
SHOW_INPUT_BOX,
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pages/picbeds/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ import { useRoute, useRouter } from 'vue-router'
import ConfigForm from '@/components/ConfigForm.vue'
import { T as $T } from '@/i18n/index'
import { getConfig, sendToMain, triggerRPC } from '@/utils/dataSender'
import { sendToMain } from '@/utils/common'
import { getConfig, triggerRPC } from '@/utils/dataSender'
import { OPEN_URL } from '#/events/constants'
import { II18nLanguage, IRPCActionType } from '#/types/enum'
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OPEN_URL } from '#/events/constants'
import { ILogType } from '#/types/enum'

const isDevelopment = process.env.NODE_ENV !== 'production'

export const handleTalkingDataEvent = (data: ITalkingDataOptions) => {
const { EventId, Label = '', MapKv = {} } = data
MapKv.from = window.location.href
Expand All @@ -31,11 +32,16 @@ export const getRawData = (args: any): any => {
return args
}

function sendToMain (channel: string, ...args: any[]) {
export function sendToMain (channel: string, ...args: any[]) {
const data = getRawData(args)
ipcRenderer.send(channel, ...data)
}

export function invokeToMain (channel: string, ...args: any[]) {
const data = getRawData(args)
return ipcRenderer.invoke(channel, ...data)
}

export const openURL = (url: string) => {
sendToMain(OPEN_URL, url)
}
Expand Down
25 changes: 7 additions & 18 deletions src/renderer/utils/dataSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,20 @@ import { v4 as uuid } from 'uuid'

import { getRawData } from '@/utils/common'

import { PICGO_SAVE_CONFIG, PICGO_GET_CONFIG, RPC_ACTIONS } from '#/events/constants'
import { PICGO_SAVE_CONFIG, PICGO_GET_CONFIG, RPC_ACTIONS, PICGO_GET_CONFIG_SYNC } from '#/events/constants'
import { IRPCActionType } from '#/types/enum'

export function saveConfig (config: IObj | string, value?: any) {
const configObject = typeof config === 'string' ? { [config]: value } : getRawData(config)
ipcRenderer.send(PICGO_SAVE_CONFIG, configObject)
}

export function getConfig<T> (key?: string): Promise<T | undefined> {
return new Promise((resolve) => {
const callbackId = uuid()
const callback = (_event: IpcRendererEvent, config: T | undefined, returnCallbackId: string) => {
if (returnCallbackId === callbackId) {
resolve(config)
ipcRenderer.removeListener(PICGO_GET_CONFIG, callback)
}
}
ipcRenderer.on(PICGO_GET_CONFIG, callback)
ipcRenderer.send(PICGO_GET_CONFIG, key, callbackId)
})
export async function getConfig<T> (key?: string): Promise<T | undefined> {
return await ipcRenderer.invoke(PICGO_GET_CONFIG, key)
}

export async function getConfigSync<T> (key?: string): Promise<T | undefined> {
return await ipcRenderer.sendSync(PICGO_GET_CONFIG_SYNC, key)
}

/**
Expand Down Expand Up @@ -53,8 +47,3 @@ export function sendRPC (action: IRPCActionType, ...args: any[]): void {
const data = getRawData(args)
ipcRenderer.send(RPC_ACTIONS, action, data)
}

export function sendToMain (channel: string, ...args: any[]) {
const data = getRawData(args)
ipcRenderer.send(channel, ...data)
}
1 change: 1 addition & 0 deletions src/universal/events/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const TALKING_DATA_APPID = 'B743C16E2989419A9B02EDE9D1E6A530'
export const TALKING_DATA_EVENT = 'TALKING_DATA_EVENT'
export const PICGO_SAVE_CONFIG = 'PICGO_SAVE_CONFIG'
export const PICGO_GET_CONFIG = 'PICGO_GET_CONFIG'
export const PICGO_GET_CONFIG_SYNC = 'PICGO_GET_CONFIG_SYNC'
export const PICGO_GET_DB = 'PICGO_GET_DB'
export const PICGO_INSERT_DB = 'PICGO_INSERT_DB'
export const PICGO_INSERT_MANY_DB = 'PICGO_INSERT_MANY_DB'
Expand Down

0 comments on commit 106290f

Please sign in to comment.