From f42df70fb155b5751767290f756d3ead71ea74d6 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 3 Feb 2022 13:26:29 +0530 Subject: [PATCH] refactor: use channels for ipcMain to ipcRenderer --- src/background.js | 6 +++--- src/main.js | 16 ++++++++++++---- src/messages.js | 7 +++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/background.js b/src/background.js index 93967945..afd3c8bc 100644 --- a/src/background.js +++ b/src/background.js @@ -16,7 +16,7 @@ import fs from 'fs/promises'; import path from 'path'; import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'; import { sendError } from './contactMothership'; -import { IPC_ACTIONS, IPC_MESSAGES } from './messages'; +import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from './messages'; import saveHtmlAsPdf from './saveHtmlAsPdf'; const isDevelopment = process.env.NODE_ENV !== 'production'; @@ -104,7 +104,7 @@ function createWindow() { }); mainWindow.webContents.on('did-finish-load', () => { - mainWindow.webContents.send('store-on-window', { + mainWindow.webContents.send(IPC_CHANNELS.STORE_ON_WINDOW, { appVersion: app.getVersion(), }); }); @@ -201,7 +201,7 @@ ipcMain.handle(IPC_ACTIONS.SEND_ERROR, (event, bodyJson) => { }); ipcMain.handle(IPC_ACTIONS.CHECK_FOR_UPDATES, (event, force) => { - if (!isDevelopment && !checkedForUpdate) { + if (force || (!isDevelopment && !checkedForUpdate)) { autoUpdater.checkForUpdatesAndNotify(); checkedForUpdate = true; } diff --git a/src/main.js b/src/main.js index ec811d46..87432a48 100644 --- a/src/main.js +++ b/src/main.js @@ -5,11 +5,21 @@ import models from '../models'; import App from './App'; import FeatherIcon from './components/FeatherIcon'; import { getErrorHandled, handleError } from './errorHandling'; -import { IPC_ACTIONS, IPC_MESSAGES } from './messages'; +import { IPC_CHANNELS, IPC_MESSAGES } from './messages'; import router from './router'; import { outsideClickDirective } from './ui'; import { stringifyCircular } from './utils'; +function registerIpcRendererListeners() { + ipcRenderer.on(IPC_CHANNELS.STORE_ON_WINDOW, (event, message) => { + Object.assign(window.frappe.store, message); + }); + + ipcRenderer.on('wc-message', (event, message) => { + console.log(message); + }); +} + (async () => { frappe.isServer = true; frappe.isElectron = true; @@ -26,9 +36,7 @@ import { stringifyCircular } from './utils'; window.frappe = frappe; window.frappe.store = {}; - ipcRenderer.on('store-on-window', (event, message) => { - Object.assign(window.frappe.store, message); - }); + registerIpcRendererListeners(); Vue.config.productionTip = false; Vue.component('feather-icon', FeatherIcon); diff --git a/src/messages.js b/src/messages.js index 0a2bfe81..542231e2 100644 --- a/src/messages.js +++ b/src/messages.js @@ -1,3 +1,4 @@ +// ipcRenderer.send(...) export const IPC_MESSAGES = { OPEN_MENU: 'open-menu', OPEN_SETTINGS: 'open-settings', @@ -9,6 +10,7 @@ export const IPC_MESSAGES = { MINIMIZE_CURRENT_WINDOW: 'minimize-current-window', }; +// ipcRenderer.invoke(...) export const IPC_ACTIONS = { TOGGLE_MAXIMIZE_CURRENT_WINDOW: 'toggle-maximize-current-window', GET_OPEN_FILEPATH: 'open-dialog', @@ -22,6 +24,11 @@ export const IPC_ACTIONS = { CHECK_FOR_UPDATES: 'check-for-updates', }; +// ipcMain.send(...) +export const IPC_CHANNELS = { + STORE_ON_WINDOW: 'store-on-window', +}; + export const DB_CONN_FAILURE = { INVALID_FILE: 'invalid-file', CANT_OPEN: 'cant-open',