aboutsummaryrefslogtreecommitdiff
path: root/src/server/session/utilities/ipc.ts
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2020-01-10 16:45:09 -0500
committerSam Wilkins <samwilkins333@gmail.com>2020-01-10 16:45:09 -0500
commit148a599d67b57b8a31697b4d54da1a652a71e441 (patch)
tree8eb95e676b44d59bd8601a111b407f6d9371ef81 /src/server/session/utilities/ipc.ts
parent12a54031b6b22b02c5fc08f80b3079d84785e457 (diff)
ipc
Diffstat (limited to 'src/server/session/utilities/ipc.ts')
-rw-r--r--src/server/session/utilities/ipc.ts48
1 files changed, 28 insertions, 20 deletions
diff --git a/src/server/session/utilities/ipc.ts b/src/server/session/utilities/ipc.ts
index b20f3d337..888377c93 100644
--- a/src/server/session/utilities/ipc.ts
+++ b/src/server/session/utilities/ipc.ts
@@ -1,50 +1,58 @@
import { isMaster } from "cluster";
import { Utils } from "../../../Utils";
-export namespace IPC {
+export type IPCTarget = NodeJS.EventEmitter & { send?: Function };
+export type Listener = (message: any) => void | Promise<void>;
- export const suffix = isMaster ? Utils.GenerateGuid() : process.env.ipc_suffix;
- const ipc_id = `ipc_id_${suffix}`;
- const response_expected = `response_expected_${suffix}`;
- const is_response = `is_response_${suffix}`;
+export const suffix = isMaster ? Utils.GenerateGuid() : process.env.ipc_suffix;
- export async function dispatchMessage(target: NodeJS.EventEmitter & { send?: Function }, message: any, expectResponse = false): Promise<Error | undefined> {
- if (!target.send) {
+export class PromisifiedIPCManager {
+ private readonly target: IPCTarget;
+ private readonly ipc_id = `ipc_id_${suffix}`;
+ private readonly response_expected = `response_expected_${suffix}`;
+ private readonly is_response = `is_response_${suffix}`;
+
+ constructor(target: IPCTarget) {
+ this.target = target;
+ }
+
+ public emit = async (message: any, expectResponse = false): Promise<Error | undefined> => {
+ if (!this.target.send) {
return new Error("Cannot dispatch when send is undefined.");
}
- message[response_expected] = expectResponse;
+ message[this.response_expected] = expectResponse;
if (expectResponse) {
return new Promise(resolve => {
const messageId = Utils.GenerateGuid();
- message[ipc_id] = messageId;
+ message[this.ipc_id] = messageId;
const responseHandler: (args: any) => void = response => {
const { error } = response;
- if (response[is_response] && response[ipc_id] === messageId) {
- target.removeListener("message", responseHandler);
+ if (response[this.is_response] && response[this.ipc_id] === messageId) {
+ this.target.removeListener("message", responseHandler);
resolve(error);
}
};
- target.addListener("message", responseHandler);
- target.send!(message);
+ this.target.addListener("message", responseHandler);
+ this.target.send!(message);
});
} else {
- target.send(message);
+ this.target.send(message);
}
}
- export function addMessagesHandler(target: NodeJS.EventEmitter & { send?: Function }, handler: (message: any) => void | Promise<void>): void {
- target.addListener("message", async incoming => {
+ public addMessagesHandler = (handler: Listener): void => {
+ this.target.addListener("message", async incoming => {
let error: Error | undefined;
try {
await handler(incoming);
} catch (e) {
error = e;
}
- if (incoming[response_expected] && target.send) {
+ if (incoming[this.response_expected] && this.target.send) {
const response: any = { error };
- response[ipc_id] = incoming[ipc_id];
- response[is_response] = true;
- target.send(response);
+ response[this.ipc_id] = incoming[this.ipc_id];
+ response[this.is_response] = true;
+ this.target.send(response);
}
});
}