Skip to content

Commit

Permalink
refactor: describe ipc api key in plain objects
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Oct 4, 2023
1 parent fd4c5b5 commit 477d1bb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions packages/app-common/src/apis/app/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { IpcRendererInvokerAPI } from '../types';

import type { AppDetails, WindowType } from './types';

export enum AppAPIChannel {
CREATE_WINDOW = 'App:CreateWindow',
GET_APP_DETAILS = 'App:GetAppDetails',
}
export const AppAPIKey = {
CREATE_WINDOW: 'App:CreateWindow',
GET_APP_DETAILS: 'App:GetAppDetails',
};

type CreateWindowAPI = IpcRendererInvokerAPI<(type: WindowType) => void>;
type GetAppDetails = IpcRendererInvokerAPI<() => AppDetails>;
Expand Down
16 changes: 8 additions & 8 deletions packages/app-common/src/apis/logger/apis.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { IpcRendererInvokerAPI } from '../types';

export enum LoggerAPIChannel {
LOG = 'Logger:Log',
ERROR = 'Logger:Error',
WARN = 'Logger:Warn',
INFO = 'Logger:Info',
VERBOSE = 'Logger:Verbose',
DEBUG = 'Logger:Debug',
}
export const LoggerAPIKey = {
LOG: 'Logger:Log',
ERROR: 'Logger:Error',
WARN: 'Logger:Warn',
INFO: 'Logger:Info',
VERBOSE: 'Logger:Verbose',
DEBUG: 'Logger:Debug',
};

type LogLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug';

Expand Down
6 changes: 3 additions & 3 deletions packages/app-main/src/main/apis/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AppMainAPI } from '@ter/app-common/apis/app';
import { AppAPIChannel } from '@ter/app-common/apis/app';
import { AppAPIKey } from '@ter/app-common/apis/app';
import { ipcMain } from 'electron';

import { getAppDetails } from 'src/main/app';
Expand All @@ -9,9 +9,9 @@ import { makeHandler } from '../utils';

export function registerAppGlobalHandlers() {
ipcMain.handle(
AppAPIChannel.CREATE_WINDOW,
AppAPIKey.CREATE_WINDOW,
makeHandler<AppMainAPI['handleCreateWindow']>((_, type) => WindowManager.INSTANCE.createWindow({ type })),
);

ipcMain.handle(AppAPIChannel.GET_APP_DETAILS, makeHandler<AppMainAPI['handleGetAppDetails']>(getAppDetails));
ipcMain.handle(AppAPIKey.GET_APP_DETAILS, makeHandler<AppMainAPI['handleGetAppDetails']>(getAppDetails));
}
14 changes: 7 additions & 7 deletions packages/app-main/src/main/apis/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LoggerMainAPI } from '@ter/app-common/apis/logger';
import { LoggerAPIChannel } from '@ter/app-common/apis/logger';
import { LoggerAPIKey } from '@ter/app-common/apis/logger';
import type { IpcMain } from 'electron';
import type winston from 'winston';

Expand All @@ -14,10 +14,10 @@ export function registerLoggerHandlers(ipc: IpcMain, logger: winston.Logger) {
handleVerbose: (_, message, ...meta) => void logger.verbose(message, ...meta),
};

ipc.handle(LoggerAPIChannel.LOG, handlers.handleLog);
ipc.handle(LoggerAPIChannel.DEBUG, handlers.handleDebug);
ipc.handle(LoggerAPIChannel.ERROR, handlers.handleError);
ipc.handle(LoggerAPIChannel.INFO, handlers.handleInfo);
ipc.handle(LoggerAPIChannel.WARN, handlers.handleWarn);
ipc.handle(LoggerAPIChannel.VERBOSE, handlers.handleVerbose);
ipc.handle(LoggerAPIKey.LOG, handlers.handleLog);
ipc.handle(LoggerAPIKey.DEBUG, handlers.handleDebug);
ipc.handle(LoggerAPIKey.ERROR, handlers.handleError);
ipc.handle(LoggerAPIKey.INFO, handlers.handleInfo);
ipc.handle(LoggerAPIKey.WARN, handlers.handleWarn);
ipc.handle(LoggerAPIKey.VERBOSE, handlers.handleVerbose);
}
6 changes: 3 additions & 3 deletions packages/app-main/src/preload/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { AppRendererAPI } from '@ter/app-common/apis/app';
import { AppAPIChannel } from '@ter/app-common/apis/app';
import { AppAPIKey } from '@ter/app-common/apis/app';
import { ipcRenderer } from 'electron';

import { webArgs } from './args';

export const appAPI: AppRendererAPI = {
windowType: webArgs.windowType,
createWindow: (...args) => ipcRenderer.invoke(AppAPIChannel.CREATE_WINDOW, ...args),
getAppDetails: (...args) => ipcRenderer.invoke(AppAPIChannel.GET_APP_DETAILS, ...args),
createWindow: (...args) => ipcRenderer.invoke(AppAPIKey.CREATE_WINDOW, ...args),
getAppDetails: (...args) => ipcRenderer.invoke(AppAPIKey.GET_APP_DETAILS, ...args),
};
14 changes: 7 additions & 7 deletions packages/app-main/src/preload/logger.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { LoggerRendererAPI } from '@ter/app-common/apis/logger';
import { LoggerAPIChannel } from '@ter/app-common/apis/logger';
import { LoggerAPIKey } from '@ter/app-common/apis/logger';
import { ipcRenderer } from 'electron';

export const loggerAPI: LoggerRendererAPI = {
debug: (...args) => ipcRenderer.invoke(LoggerAPIChannel.DEBUG, ...args),
error: (...args) => ipcRenderer.invoke(LoggerAPIChannel.ERROR, ...args),
info: (...args) => ipcRenderer.invoke(LoggerAPIChannel.INFO, ...args),
log: (...args) => ipcRenderer.invoke(LoggerAPIChannel.LOG, ...args),
verbose: (...args) => ipcRenderer.invoke(LoggerAPIChannel.VERBOSE, ...args),
warn: (...args) => ipcRenderer.invoke(LoggerAPIChannel.WARN, ...args),
debug: (...args) => ipcRenderer.invoke(LoggerAPIKey.DEBUG, ...args),
error: (...args) => ipcRenderer.invoke(LoggerAPIKey.ERROR, ...args),
info: (...args) => ipcRenderer.invoke(LoggerAPIKey.INFO, ...args),
log: (...args) => ipcRenderer.invoke(LoggerAPIKey.LOG, ...args),
verbose: (...args) => ipcRenderer.invoke(LoggerAPIKey.VERBOSE, ...args),
warn: (...args) => ipcRenderer.invoke(LoggerAPIKey.WARN, ...args),
};

0 comments on commit 477d1bb

Please sign in to comment.