From 5d4428038b1872474c03879f205b10c1cc1d567e Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sun, 29 Nov 2020 22:47:23 +0900 Subject: [PATCH] refactor: Use invoke for ipc to change language --- src/main/index.ts | 19 ++++++++----------- src/renderer/store/Preferences/Language.ts | 12 ++++-------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 1df0e20c..616026d2 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -977,18 +977,15 @@ ipcMain.handle('update-proxy-config', async (_event: IpcMainInvokeEvent, proxy: }) // language -ipcMain.on('change-language', (event: IpcMainEvent, value: string) => { +ipcMain.handle('change-language', async (_: IpcMainInvokeEvent, value: string) => { const preferences = new Preferences(preferencesDBPath) - preferences - .update({ - language: { - language: value - } - }) - .then(conf => { - i18next.changeLanguage(conf.language.language) - event.sender.send('response-change-language', conf.language.language) - }) + const conf = await preferences.update({ + language: { + language: value + } + }) + i18next.changeLanguage(conf.language.language) + return conf.language.language }) // hashtag diff --git a/src/renderer/store/Preferences/Language.ts b/src/renderer/store/Preferences/Language.ts index e39e724a..e36f53a0 100644 --- a/src/renderer/store/Preferences/Language.ts +++ b/src/renderer/store/Preferences/Language.ts @@ -37,14 +37,10 @@ const actions: ActionTree = { commit(MUTATION_TYPES.UPDATE_LANGUAGE, conf.language as LanguageSet) return conf }, - changeLanguage: ({ commit }, key: string) => { - return new Promise(resolve => { - win.ipcRenderer.send('change-language', key) - win.ipcRenderer.once('response-change-language', (_, value: string) => { - commit(MUTATION_TYPES.CHANGE_LANGUAGE, value) - resolve(value) - }) - }) + changeLanguage: async ({ commit }, key: string) => { + const value: string = await win.ipcRenderer.invoke('change-language', key) + commit(MUTATION_TYPES.CHANGE_LANGUAGE, value) + return value }, relaunch: () => { win.ipcRenderer.send('relaunch')