Skip to content

Commit

Permalink
feat: add ipc framework
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoDelnat committed Mar 31, 2023
1 parent 962e303 commit 449b3b7
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ module.exports = {

'arrow-body-style': 'off', // warn
'arrow-parens': 'off', // warn

'class-methods-use-this': 'off',
'comma-dangle': 'off',
'consistent-return': 'off', // warn. Look at api calls closely before enabling this. api.ts.

'default-param-last': 'off',
'dot-notation': 'off', // required for our env variables currently

'no-await-in-loop': 'off',
'import/no-named-as-default': 'off',
'import/no-cycle': 'off',
'import/no-extraneous-dependencies': 'off',
Expand Down
1 change: 1 addition & 0 deletions electron/app/services/cluster/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ping';
7 changes: 7 additions & 0 deletions electron/app/services/cluster/handlers/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type {PingParams, PingResult} from '@shared/ipc';

export async function ping({context, kubeconfig}: PingParams): Promise<PingResult> {
// eslint-disable-next-line no-console
console.log('fake ping for', context, kubeconfig);
return {ok: false};
}
4 changes: 4 additions & 0 deletions electron/app/services/cluster/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {handleIpc} from '../../utils/ipc';
import {ping} from './handlers/ping';

handleIpc('cluster:ping', ping);
15 changes: 15 additions & 0 deletions electron/app/utils/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {ipcMain} from 'electron';

import {IpcResult} from '@shared/ipc';

export async function handleIpc<Params, Result>(channel: string, handler: (params: Params) => Result) {
ipcMain.handle(channel, async (_, params: Params): Promise<IpcResult> => {
try {
const payload = await handler(params);
return {success: true, payload};
} catch (err) {
const msg = err instanceof Error ? err.message : 'error_unknown';
return {success: false, reason: msg};
}
});
}
16 changes: 16 additions & 0 deletions src/redux/cluster/service/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {invokeIpc} from '@utils/ipc';

import type {PingParams, PingResult} from '@shared/ipc';

/**
* Example usage:
*
* ```
* try {
* const { ok } = await ping({ context: "minikube" })
* } catch (err) {
* console.log(err);
* }
* ```
*/
export const ping = invokeIpc<PingParams, PingResult>('cluster:ping');
8 changes: 8 additions & 0 deletions src/shared/ipc/cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type PingParams = {
context: string;
kubeconfig?: string;
};

export type PingResult = {
ok: boolean;
};
3 changes: 3 additions & 0 deletions src/shared/ipc/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type IpcResult = IpcSuccess | IpcFailure;
export type IpcSuccess = {success: true; payload: any};
export type IpcFailure = {success: false; reason: string};
2 changes: 2 additions & 0 deletions src/shared/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './cluster';
export * from './core';
16 changes: 16 additions & 0 deletions src/utils/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import {ipcRenderer} from 'electron';

import type {IpcResult} from '@shared/ipc';
import {StartupFlags} from '@shared/models/startupFlag';

import {StartupFlag} from './startupFlag';
Expand All @@ -9,3 +12,16 @@ export function getChannelName(name: string, hasAutomationFlag = StartupFlag.get

return name;
}

export function invokeIpc<Params, Result>(channel: string) {
return async (params: Params): Promise<Result> => {
try {
const result: IpcResult = await ipcRenderer.invoke(channel, params);
if (!result.success) throw new Error(result.reason);
return result.payload;
} catch (err) {
const msg = err instanceof Error ? err.message : 'error_unknown';
throw new Error(msg);
}
};
}

0 comments on commit 449b3b7

Please sign in to comment.