-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathipcNode.ts
More file actions
104 lines (91 loc) · 3.32 KB
/
ipcNode.ts
File metadata and controls
104 lines (91 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-disable @typescript-eslint/no-explicit-any */
import Electron, { BrowserWindow } from 'electron';
import { initialBelnetRpcDealer } from './belnetRpcCall';
import {
IPC_CHANNEL_KEY,
IPC_GLOBAL_ERROR,
IPC_LOG_LINE,
StatusErrorType
} from './sharedIpc';
const { ipcMain } = Electron;
import * as rpcCalls from './belnetRpcCall';
import * as belnetProcessManager from './belnetProcessManager';
import * as utilityIPCCalls from './utilityIPCCalls';
export const eventsByJobId = Object.create(null);
export const getEventByJobId = (jobId: string): any => {
const event = eventsByJobId[jobId];
if (!event) {
throw new Error(`Could not find the event for jobId ${jobId}`);
}
return event;
};
let getMainWindowLocal: () => BrowserWindow | null;
/**
* Returns the function to call for that RPC call, so with zeromq client, or undefined.
*/
function isRpcCall(fnName: string) {
return (rpcCalls as any)[fnName];
}
/**
* Returns the function to call for that Belnet Process Manager call, or undefined.
*/
function isBelnetProcessManagerCall(fnName: string) {
return (belnetProcessManager as any)[fnName];
}
/**
* Returns the function to call for that Utility IPC call.
*/
function isUtilityCall(fnName: string) {
return (utilityIPCCalls as any)[fnName];
}
export async function initializeIpcNodeSide(
getMainWindow: () => BrowserWindow | null
): Promise<void> {
await initialBelnetRpcDealer();
getMainWindowLocal = getMainWindow;
ipcMain.on(IPC_CHANNEL_KEY, async (event, jobId, callName, ...args) => {
try {
// Try to find a matching rpc call, or a matching belnetProcessManager call or a matching utility call
const rpcCall = isRpcCall(callName);
const belnetProcessManagerCall = isBelnetProcessManagerCall(callName);
const utilityCall = isUtilityCall(callName);
const fnToCall = rpcCall || belnetProcessManagerCall || utilityCall;
if (!fnToCall) {
// if that fn is not defined at all, there is not much we can do.
throw new Error(
`ipc channel: ${callName} is not an available function`
);
}
if (eventsByJobId[jobId]) {
throw new Error(`There is already a event for this jobId ${jobId}`);
}
eventsByJobId[jobId] = event;
// this call just trigger the RPC call. The reply will come from somewhere else
await fnToCall(jobId, ...args);
} catch (error: any) {
const errorForDisplay = error && error.msg ? error.msg : error;
console.log(
`ipc channel error with call ${callName}: ${errorForDisplay}`
);
delete eventsByJobId[jobId];
event.sender.send(`${IPC_CHANNEL_KEY}-done`, jobId, error?.msg || null);
}
});
}
export function logLineToAppSide(logLine: string): void {
const withTimestamp = `${logLine}`;
if (utilityIPCCalls.getRendererReady()) {
console.info(`logLine ready "${logLine}`);
getMainWindowLocal()?.webContents.send(IPC_LOG_LINE, withTimestamp);
} else {
console.info('logLineToAppSide : renderer is not ready');
}
}
export function sendGlobalErrorToAppSide(globalError: StatusErrorType): void {
if (utilityIPCCalls.getRendererReady()) {
console.info(`global error "${globalError}`);
getMainWindowLocal()?.webContents.send(IPC_GLOBAL_ERROR, globalError);
} else {
console.info('sendGlobalErrorToAppSide : renderer is not ready');
}
}