Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Commit

Permalink
refactor: Рефакторинг IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
cawa-93 committed May 20, 2021
1 parent 71dc1c0 commit e4155b0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
19 changes: 15 additions & 4 deletions packages/main/src/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ import {ipcMain} from 'electron';
import type {IpcNameHostsMap} from '/@shared/types/ipc';


function hasProperty<T>(host: T, prop: string): prop is string & keyof T {
return !!(host as never)[prop];
}


export function registerIpcHost<T extends keyof IpcNameHostsMap>(hostName: T, host: IpcNameHostsMap[T]): void {
ipcMain.handle(hostName, async (event, methodName: keyof IpcNameHostsMap[T], ...args: unknown[]) => {
ipcMain.handle(hostName, async (event, methodName: unknown, ...args) => {
if (typeof methodName !== 'string') {
throw new Error(`methodName must be a string (got ${JSON.stringify(methodName)}) when called host "${hostName}"`);
}

if (!hasProperty(host, methodName)) {
throw new Error(`"${methodName}" is undefined method on host "${hostName}"`);
}

const method = host[methodName];

// If requested method does not exist, reject.
if (typeof method !== 'function') {
throw new Error(`Invalid method name "${methodName}" on host "${hostName}"`);
throw new Error(`"${methodName}" in not a function on host "${hostName}"`);
}

return method(...args);
});

}
5 changes: 2 additions & 3 deletions packages/renderer/src/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ const {invoke} = useElectron();

export function createIpcClient<T extends keyof IpcNameHostsMap>(hostName: T): Promisified<IpcNameHostsMap[T]> {
return new Proxy({} as never, {
get: (obj, methodName: string) => {
get: (obj, methodName) => {

// Chrome runtime could try to call those method if the proxy object
// is passed in a resolve or reject Promise function
if (methodName === 'then' || methodName === 'catch')
if (methodName === 'then' || methodName === 'catch' || methodName === 'finally')
return undefined;

// If accessed field effectivly exist on proxied object, act as a noop
if (Object.prototype.hasOwnProperty.call(obj, methodName)) {
return obj[methodName];
}
Expand Down
3 changes: 2 additions & 1 deletion packages/renderer/src/utils/dialogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {createIpcClient} from '/@/ipc';


export function showErrorMessage({title = 'Ошибка', message = 'Неизвестная ошибка'}) {
export function showErrorMessage({title = 'Ошибка', message = 'Неизвестная ошибка'}): Promise<void> {
const dialog = createIpcClient('DialogsControllers');
return dialog.showError(title, message);
}

9 changes: 3 additions & 6 deletions packages/shared/types/ipc/DialogsControllers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type {HostBase, MethodOnlyHost} from './Host';
import type {IpcHost} from './Host';


interface DialogsControllersBase extends HostBase {
export type DialogsControllers = IpcHost<{
showError: (title: string, content: string) => void
}


export type DialogsControllers = MethodOnlyHost<DialogsControllersBase>
}>
3 changes: 1 addition & 2 deletions packages/shared/types/ipc/Host.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {ClearIndex, Fn, NonFunctionKeys} from '../utils';


export type HostBase = Record<string, Fn>
export type MethodOnlyHost<T extends HostBase> = Omit<ClearIndex<T>, NonFunctionKeys<ClearIndex<T>>>
export type IpcHost<T extends Record<string, Fn>> = Omit<ClearIndex<T>, NonFunctionKeys<ClearIndex<T>>>
9 changes: 3 additions & 6 deletions packages/shared/types/ipc/WindowControllers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type {HostBase, MethodOnlyHost} from './Host';
import type {IpcHost} from './Host';


interface WindowControllersBase extends HostBase {
export type WindowControllers = IpcHost<{
minimize: () => void
maximize: () => void
unmaximize: () => void
close: () => void
isMaximized: () => boolean
}


export type WindowControllers = MethodOnlyHost<WindowControllersBase>
}>
6 changes: 3 additions & 3 deletions packages/shared/types/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {WindowControllers} from './WindowControllers';
import type {DialogsControllers} from './DialogsControllers';
import type {HostBase} from './Host';
import type {ClearIndex} from '../utils';
import type {IpcHost} from './Host';
import type {ClearIndex, Fn} from '../utils';



interface IpcNameHostsMapBase extends Record<string, HostBase> {
interface IpcNameHostsMapBase extends Record<string, IpcHost<Record<string, Fn>>> {
'WindowControllers': WindowControllers
'DialogsControllers': DialogsControllers
}
Expand Down

0 comments on commit e4155b0

Please sign in to comment.