added common/ipc-helpers

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-07 15:10:43 +03:00
1 parent 17cd886ce6
commit bde60b1625
8 files changed
+83 -51

No files matched your search

+5
View File
@@ -1,6 +1,7 @@
import path from "path"
import Config from "conf"
import { Options as ConfOptions } from "conf/dist/source/types"
import produce from "immer";
import { app, remote } from "electron"
import { observable, reaction, toJS, when } from "mobx";
import Singleton from "./utils/singleton";
@@ -115,6 +116,10 @@ export class BaseStore<T = any> extends Singleton {
Object.assign(this.data, data);
}
merge(updater: (modelDraft: T) => void) {
this.data = produce(this.data, updater);
}
toJSON(): T {
return toJS(this.data, {
recurseEverything: true,
+39
View File
@@ -0,0 +1,39 @@
// Inter-protocol communications (main <-> renderer)
// https://www.electronjs.org/docs/api/ipc-main
// https://www.electronjs.org/docs/api/ipc-renderer
import { ipcMain, ipcRenderer } from "electron"
import logger from "../main/logger";
export interface IpcOptions {
timeout?: number;
}
export async function sendMessage(channel: string, ...args: any[]) {
logger.debug(`[IPC]: invoke "${channel}" with arguments`, args);
return ipcRenderer.invoke(channel, ...args);
}
// todo: maybe spawn callback in separate thread/worker
export function onMessage<T = any>(channel: string, callback: (...args: any[]) => T, options: IpcOptions = {}) {
const { timeout = 0 } = options;
ipcMain.handle(channel, async (event, ...args: any[]) => {
logger.debug(`[IPC]: handle "${channel}"`, event, args);
return new Promise(async (resolve, reject) => {
let timerId;
if (timeout) {
timerId = setTimeout(() => {
const timeoutError = new Error("[IPC]: response timeout");
reject(timeoutError);
}, timeout);
}
try {
const result = await callback(...args);
clearTimeout(timerId);
return result;
} catch (err) {
logger.debug(`[IPC]: handling "${channel}" error`, err);
}
})
})
}
+2
View File
@@ -2,6 +2,8 @@ import { app, remote } from "electron"
import { ensureDirSync, writeFileSync } from "fs-extra"
import * as path from "path"
// todo: move to main/kubeconfig-manager.ts (?)
// Writes kubeconfigs to "embedded" store, i.e. .../Lens/kubeconfigs/
export function writeEmbeddedKubeConfig(clusterId: string, kubeConfig: string): string {
// This can be called from main & renderer