Files
Netcatty/electron/plugins/shutdownCoordinator.cjs
T
ryan-wong-coder 7a47187e37 feat(plugins): add isolated plugin host runtime (#2303)
* feat(plugins): add isolated plugin host runtime

* test(plugins): build runtime dependencies before tests

* fix(plugins): harden runtime lifecycle and recovery

* fix(plugins): harden runtime extension boundaries

* fix(plugins): enforce runtime integrity boundaries

* fix(plugins): await utility process exit

* fix(plugins): harden utility process containment

* fix(plugins): close runtime recovery gaps

* fix(plugins): harden runtime lifecycle boundaries
2026-07-19 08:22:17 +08:00

50 lines
1.3 KiB
JavaScript

"use strict";
const {
PLUGIN_DEACTIVATION_TIMEOUT_MS,
PLUGIN_UTILITY_FORCE_EXIT_TIMEOUT_MS,
PLUGIN_UTILITY_TERMINATION_GRACE_MS,
} = require("./constants.cjs");
let shutdownHandler = null;
let shutdownPromise = null;
function registerPluginShutdown(handler) {
if (typeof handler !== "function") throw new TypeError("Plugin shutdown handler must be a function");
shutdownHandler = handler;
shutdownPromise = null;
return () => {
if (shutdownHandler === handler) shutdownHandler = null;
};
}
function runPluginShutdown(options = {}) {
if (shutdownPromise) return shutdownPromise;
if (!shutdownHandler) return Promise.resolve({ timedOut: false });
const timeoutMs = options.timeoutMs
?? PLUGIN_DEACTIVATION_TIMEOUT_MS
+ PLUGIN_UTILITY_TERMINATION_GRACE_MS
+ PLUGIN_UTILITY_FORCE_EXIT_TIMEOUT_MS
+ 500;
let timer;
const timeout = new Promise((resolve) => {
timer = setTimeout(() => resolve({ timedOut: true }), timeoutMs);
});
shutdownPromise = Promise.race([
Promise.resolve().then(shutdownHandler).then(() => ({ timedOut: false })),
timeout,
]).finally(() => clearTimeout(timer));
return shutdownPromise;
}
function resetPluginShutdownForTests() {
shutdownHandler = null;
shutdownPromise = null;
}
module.exports = {
registerPluginShutdown,
resetPluginShutdownForTests,
runPluginShutdown,
};