2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +00:00

incr: move all ipccalls to single file

This commit is contained in:
18alantom 2023-07-10 12:11:32 +05:30
parent 6cb2fed6d7
commit 62a920ced7
5 changed files with 67 additions and 41 deletions

View File

@ -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) {

View File

@ -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)
);

View File

@ -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) {

View File

@ -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<string, unknown>) => {
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;
}

View File

@ -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<typeof ipcRenderer.on>[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);
}