From 62a920ced7e28bfe06804be002f4b1496f15737c Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 10 Jul 2023 12:11:32 +0530 Subject: [PATCH] incr: move all ipccalls to single file --- src/errorHandling.ts | 20 +++++----- src/pages/DatabaseSelector.vue | 14 +++---- src/renderer.ts | 23 ++--------- src/renderer/registerIpcRendererListeners.ts | 11 +++--- src/utils/ipcCalls.ts | 40 +++++++++++++++++++- 5 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/errorHandling.ts b/src/errorHandling.ts index ee01e49f..0e851521 100644 --- a/src/errorHandling.ts +++ b/src/errorHandling.ts @@ -1,15 +1,18 @@ import { t } from 'fyo'; -import { Doc } from 'fyo/model/doc'; +import type { Doc } from 'fyo/model/doc'; import { BaseError } from 'fyo/utils/errors'; import { ErrorLog } from 'fyo/utils/types'; import { truncate } from 'lodash'; import { showDialog } from 'src/utils/interactive'; -import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages'; import { fyo } from './initFyo'; import router from './router'; import { getErrorMessage, stringifyCircular } from './utils'; -import { DialogOptions, ToastOptions } from './utils/types'; -const { ipcRenderer } = require('electron'); +import { + sendError as ipcSendError, + openExternalUrl, + showError, +} from './utils/ipcCalls'; +import type { DialogOptions, ToastOptions } from './utils/types'; function shouldNotStore(error: Error) { const shouldLog = (error as BaseError).shouldStore ?? true; @@ -43,7 +46,7 @@ export async function sendError(errorLogObj: ErrorLog) { console.log('sendError', body); } - await ipcRenderer.invoke(IPC_ACTIONS.SEND_ERROR, JSON.stringify(body)); + await ipcSendError(JSON.stringify(body)); } function getToastProps(errorLogObj: ErrorLog) { @@ -119,7 +122,7 @@ export async function handleErrorWithDialog( }; if (reportError) { - options.detail = truncate(options.detail, { length: 128 }); + options.detail = truncate(String(options.detail), { length: 128 }); options.buttons = [ { label: t`Report`, @@ -154,8 +157,7 @@ export async function showErrorDialog(title?: string, content?: string) { // To be used for show stopper errors title ??= t`Error`; content ??= t`Something has gone terribly wrong. Please check the console and raise an issue.`; - - await ipcRenderer.invoke(IPC_ACTIONS.SHOW_ERROR, { title, content }); + await showError(title, content); } // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -232,7 +234,7 @@ function getIssueUrlQuery(errorLogObj?: ErrorLog): string { export function reportIssue(errorLogObj?: ErrorLog) { const urlQuery = getIssueUrlQuery(errorLogObj); - ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, urlQuery); + openExternalUrl(urlQuery); } function getErrorLabel(error: Error) { diff --git a/src/pages/DatabaseSelector.vue b/src/pages/DatabaseSelector.vue index c24cae17..f0687421 100644 --- a/src/pages/DatabaseSelector.vue +++ b/src/pages/DatabaseSelector.vue @@ -246,12 +246,15 @@ import Loading from 'src/components/Loading.vue'; import Modal from 'src/components/Modal.vue'; import { fyo } from 'src/initFyo'; import { showDialog } from 'src/utils/interactive'; -import { deleteDb, getSavePath, getSelectedFilePath } from 'src/utils/ipcCalls'; +import { + deleteDb, + getDbList, + getSavePath, + getSelectedFilePath, +} from 'src/utils/ipcCalls'; import { updateConfigFiles } from 'src/utils/misc'; -import { IPC_ACTIONS } from 'utils/messages'; import type { ConfigFilesWithModified } from 'utils/types'; import { defineComponent } from 'vue'; -const { ipcRenderer } = require('electron'); export default defineComponent({ name: 'DatabaseSelector', @@ -360,10 +363,7 @@ export default defineComponent({ this.creatingDemo = false; }, async setFiles() { - const dbList: ConfigFilesWithModified[] = await ipcRenderer.invoke( - IPC_ACTIONS.GET_DB_LIST - ); - + const dbList = await getDbList(); this.files = dbList?.sort( (a, b) => Date.parse(b.modified) - Date.parse(a.modified) ); diff --git a/src/renderer.ts b/src/renderer.ts index 956b7932..c369b733 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -1,22 +1,16 @@ -const { ipcRenderer } = require('electron'); -import { DateTime } from 'luxon'; -import { CUSTOM_EVENTS, IPC_ACTIONS } from 'utils/messages'; +import { CUSTOM_EVENTS } from 'utils/messages'; import { UnexpectedLogObject } from 'utils/types'; import { App as VueApp, createApp } from 'vue'; import App from './App.vue'; import Badge from './components/Badge.vue'; import FeatherIcon from './components/FeatherIcon.vue'; -import { - getErrorHandled, - getErrorHandledSync, - handleError, - sendError, -} from './errorHandling'; +import { handleError, sendError } from './errorHandling'; import { fyo } from './initFyo'; import { outsideClickDirective } from './renderer/helpers'; import registerIpcRendererListeners from './renderer/registerIpcRendererListeners'; import router from './router'; import { stringifyCircular } from './utils'; +import { getEnv } from './utils/ipcCalls'; import { setLanguageMap } from './utils/language'; // eslint-disable-next-line @typescript-eslint/no-floating-promises @@ -27,13 +21,8 @@ import { setLanguageMap } from './utils/language'; } fyo.store.language = language || 'English'; - ipcRenderer.send = getErrorHandledSync(ipcRenderer.send.bind(ipcRenderer)); - ipcRenderer.invoke = getErrorHandled(ipcRenderer.invoke.bind(ipcRenderer)); - registerIpcRendererListeners(); - const { isDevelopment, platform, version } = (await ipcRenderer.invoke( - IPC_ACTIONS.GET_ENV - )) as { isDevelopment: boolean; platform: string; version: string }; + const { isDevelopment, platform, version } = await getEnv(); fyo.store.isDevelopment = isDevelopment; fyo.store.appVersion = version; @@ -125,10 +114,6 @@ function setOnWindow(isDevelopment: boolean) { window.router = router; // @ts-ignore window.fyo = fyo; - // @ts-ignore - window.DateTime = DateTime; - // @ts-ignore - window.ipcRenderer = ipcRenderer; } function getPlatformName(platform: string) { diff --git a/src/renderer/registerIpcRendererListeners.ts b/src/renderer/registerIpcRendererListeners.ts index 0060a0e4..41f2c5fe 100644 --- a/src/renderer/registerIpcRendererListeners.ts +++ b/src/renderer/registerIpcRendererListeners.ts @@ -1,11 +1,12 @@ -const { ipcRenderer } = require('electron'); import { handleError } from 'src/errorHandling'; import { fyo } from 'src/initFyo'; -import { IPC_CHANNELS } from 'utils/messages'; +import { + registerConsoleLogListener, + registerMainProcessErrorListener, +} from 'src/utils/ipcCalls'; export default function registerIpcRendererListeners() { - ipcRenderer.on( - IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, + registerMainProcessErrorListener( (_, error: unknown, more?: Record) => { if (!(error instanceof Error)) { throw error; @@ -27,7 +28,7 @@ export default function registerIpcRendererListeners() { } ); - ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, (_, ...stuff: unknown[]) => { + registerConsoleLogListener((_, ...stuff: unknown[]) => { if (!fyo.store.isDevelopment) { return; } diff --git a/src/utils/ipcCalls.ts b/src/utils/ipcCalls.ts index 5932e531..842a18c0 100644 --- a/src/utils/ipcCalls.ts +++ b/src/utils/ipcCalls.ts @@ -5,8 +5,9 @@ const { ipcRenderer } = require('electron'); import { t } from 'fyo'; import { BaseError } from 'fyo/utils/errors'; import type { BackendResponse } from 'utils/ipc/types'; -import { IPC_ACTIONS, IPC_MESSAGES } from 'utils/messages'; +import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages'; import type { + ConfigFilesWithModified, LanguageMap, SelectFileOptions, SelectFileReturn, @@ -154,3 +155,40 @@ export async function getSavePath(name: string, extention: string) { return { canceled, filePath }; } + +export async function getDbList() { + return (await ipcRenderer.invoke( + IPC_ACTIONS.GET_DB_LIST + )) as ConfigFilesWithModified[]; +} + +export async function getEnv() { + return (await ipcRenderer.invoke(IPC_ACTIONS.GET_ENV)) as { + isDevelopment: boolean; + platform: string; + version: string; + }; +} + +export function openExternalUrl(url: string) { + ipcRenderer.send(IPC_MESSAGES.OPEN_EXTERNAL, url); +} + +export async function showError(title: string, content: string) { + await ipcRenderer.invoke(IPC_ACTIONS.SHOW_ERROR, { title, content }); +} + +export async function sendError(body: string) { + await ipcRenderer.invoke(IPC_ACTIONS.SEND_ERROR, body); +} + +type IPCRendererListener = Parameters[1]; +export function registerMainProcessErrorListener( + listener: IPCRendererListener +) { + ipcRenderer.on(IPC_CHANNELS.LOG_MAIN_PROCESS_ERROR, listener); +} + +export function registerConsoleLogListener(listener: IPCRendererListener) { + ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, listener); +}