This commit is contained in:
Manav Rathi
2024-07-30 13:41:58 +05:30
parent 24bc175f1c
commit b28e8c2fb4

View File

@@ -1,10 +1,33 @@
console.log("in utility process");
import log from "../log";
import { ensure, wait } from "../utils/common";
log.debug(() => "Started ML worker process");
process.parentPort.once("message", (e) => {
console.log("got message in utility process", e);
const [port] = e.ports;
port?.on("message", (e2) => {
console.log("got message on port in utility process", e2);
const port = ensure(e.ports[0]);
port.on("message", (event) => {
void handleMessage(event.data).then((response) => {
if (response) port.postMessage(response);
});
});
});
/** Our hand-rolled IPC handler and router */
const handleMessage = async (m: unknown) => {
if (m && typeof m == "object" && "type" in m) {
switch (m.type) {
case "foo":
if ("a" in m && typeof m.a == "string") return await foo(m.a);
break;
}
}
log.info("Ignoring unexpected message", m);
return undefined;
};
const foo = async (a: string) => {
console.log("got message foo with argument", a);
await wait(0);
return a.length;
};