IPC
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user