cluster-ipc refactoring

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-07-21 13:04:33 +03:00
1 parent c0c1c227ca
commit 70bbb9fa62
10 files changed
+76 -53

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
import { createIpcChannel } from "./ipc";
import { ClusterId, clusterStore } from "./cluster-store";
import { tracker } from "./tracker";
export const clusterIpc = {
refresh: createIpcChannel({
channel: "cluster:refresh",
handle: async (clusterId: ClusterId = clusterStore.activeClusterId) => {
const cluster = clusterStore.getById(clusterId);
if (cluster) {
await cluster.refreshStatus();
cluster.pushState();
}
},
}),
disconnect: createIpcChannel({
channel: "cluster:disconnect",
handle: (clusterId: ClusterId) => {
tracker.event("cluster", "stop");
clusterStore.getById(clusterId)?.disconnect();
},
}),
reconnect: createIpcChannel({
channel: "cluster:reconnect",
handle: (clusterId: ClusterId) => {
tracker.event("cluster", "reconnect");
clusterStore.getById(clusterId)?.reconnect();
},
}),
}
+20 -2
View File
@@ -43,9 +43,9 @@ export async function invokeIpc<R = any>(channel: IpcChannel, ...args: any[]): P
}
// todo: make isomorphic api
export function handleIpc<T extends any[]>(channel: IpcChannel, handler: IpcMessageHandler<T>, options: IpcHandleOpts = {}) {
export function handleIpc(channel: IpcChannel, handler: IpcMessageHandler, options: IpcHandleOpts = {}) {
const { timeout = 0 } = options;
ipcMain.handle(channel, async (event, ...args: T) => {
ipcMain.handle(channel, async (event, ...args) => {
logger.info(`[IPC]: handle "${channel}"`, { args });
return new Promise(async (resolve, reject) => {
let timerId;
@@ -65,3 +65,21 @@ export function handleIpc<T extends any[]>(channel: IpcChannel, handler: IpcMess
})
})
}
export interface IpcPairOptions {
channel: IpcChannel
handle?: IpcMessageHandler
options?: IpcHandleOpts
}
export function createIpcChannel({ channel, ...initOpts }: IpcPairOptions) {
return {
handleInMain: (opts: Partial<Omit<IpcPairOptions, "channel">> = {}) => {
const { handle = initOpts.handle, options = initOpts.options } = opts;
return handleIpc(channel, handle, options);
},
invokeFromRenderer: (...args: any[]) => {
return invokeIpc(channel, ...args);
},
}
}