Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: Wout Stiens <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
// @effect-diagnostics globalTimers:off -- The child-process shutdown watchdog runs at a worker callback boundary outside any Effect fiber.
|
|
// @effect-diagnostics nodeBuiltinImport:off -- This desktop-only helper owns a Node child process for global shortcut capture.
|
|
|
|
import * as NodeChildProcess from "node:child_process";
|
|
|
|
import type { SnapShotModifier } from "@t3tools/contracts";
|
|
|
|
export function startGlobalShiftShortcutProcess(
|
|
workerPath: string,
|
|
modifier: SnapShotModifier,
|
|
onTrigger: () => void,
|
|
onFailure: (error: Error) => void,
|
|
): Promise<() => void> {
|
|
const worker = NodeChildProcess.fork(workerPath, [modifier], {
|
|
env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" },
|
|
execArgv: [],
|
|
stdio: ["ignore", "ignore", "inherit", "ipc"],
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
|
let settled = false;
|
|
let stopped = false;
|
|
const stop = () => {
|
|
if (stopped) return;
|
|
stopped = true;
|
|
worker.kill();
|
|
const forceKill = setTimeout(() => worker.kill("SIGKILL"), 1_000);
|
|
forceKill.unref?.();
|
|
worker.once("exit", () => clearTimeout(forceKill));
|
|
};
|
|
const fail = (error: Error) => {
|
|
if (stopped) return;
|
|
if (settled) {
|
|
stop();
|
|
onFailure(error);
|
|
return;
|
|
}
|
|
settled = true;
|
|
stop();
|
|
reject(error);
|
|
};
|
|
|
|
worker.on("message", (message) => {
|
|
if (message === "ready" && !settled) {
|
|
settled = true;
|
|
resolve(stop);
|
|
return;
|
|
}
|
|
if (message !== "trigger" || !settled || stopped) return;
|
|
try {
|
|
onTrigger();
|
|
} catch {}
|
|
});
|
|
worker.once("error", (error) => {
|
|
fail(error);
|
|
});
|
|
worker.once("exit", (code) => {
|
|
fail(new Error(`Snapshot shortcut helper exited with code ${code}`));
|
|
});
|
|
});
|
|
}
|