From 8e5e9ec59ab424d3fde669fb8e160adf4a623fc9 Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sat, 10 Jul 2021 00:25:34 +0800 Subject: [PATCH] :hammer: Refactor: move guiApi to singleton --- src/main/apis/app/shortKey/shortKeyHandler.ts | 3 +-- src/main/apis/gui/index.ts | 10 ++++++++++ src/main/events/picgoCoreIPC.ts | 6 ++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/apis/app/shortKey/shortKeyHandler.ts b/src/main/apis/app/shortKey/shortKeyHandler.ts index f3edd98d..49ae309d 100644 --- a/src/main/apis/app/shortKey/shortKeyHandler.ts +++ b/src/main/apis/app/shortKey/shortKeyHandler.ts @@ -130,8 +130,7 @@ class ShortKeyHandler { } else if (command.includes('picgo-plugin-')) { const handler = shortKeyService.getShortKeyHandler(command) if (handler) { - const guiApi = new GuiApi() - return handler(picgo, guiApi) + return handler(picgo, GuiApi.getInstance()) } } else { logger.warn(`can not find command: ${command}`) diff --git a/src/main/apis/gui/index.ts b/src/main/apis/gui/index.ts index af00f7e0..bca130ea 100644 --- a/src/main/apis/gui/index.ts +++ b/src/main/apis/gui/index.ts @@ -18,8 +18,18 @@ import { // Cross-process support may be required in the future class GuiApi implements IGuiApi { + private static instance: GuiApi private windowId: number = -1 private settingWindowId: number = -1 + private constructor () { + console.log('init guiapi') + } + public static getInstance (): GuiApi { + if (!GuiApi.instance) { + GuiApi.instance = new GuiApi() + } + return GuiApi.instance + } private async showSettingWindow () { this.settingWindowId = await getSettingWindowId() const settingWindow = BrowserWindow.fromId(this.settingWindowId) diff --git a/src/main/events/picgoCoreIPC.ts b/src/main/events/picgoCoreIPC.ts index 8d0335fe..b45f64a6 100644 --- a/src/main/events/picgoCoreIPC.ts +++ b/src/main/events/picgoCoreIPC.ts @@ -210,12 +210,11 @@ const handleGetPicBedConfig = () => { const handlePluginActions = () => { ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => { const plugin = picgo.pluginLoader.getPlugin(name) - const guiApi = new GuiApi() if (plugin?.guiMenu?.(picgo)?.length) { const menu: GuiMenuItem[] = plugin.guiMenu(picgo) menu.forEach(item => { if (item.label === label) { - item.handle(picgo, guiApi) + item.handle(picgo, GuiApi.getInstance()) } }) } @@ -224,9 +223,8 @@ const handlePluginActions = () => { const handleRemoveFiles = () => { ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => { - const guiApi = new GuiApi() setTimeout(() => { - picgo.emit('remove', files, guiApi) + picgo.emit('remove', files, GuiApi.getInstance()) }, 500) }) }