refactor: move plugin loading into typed registry
This commit is contained in:
@@ -19,6 +19,7 @@ await build({
|
||||
app: path.join(sourceDir, "app.js"),
|
||||
"ui-core": path.join(sourceDir, "ui-core.ts"),
|
||||
"map-core": path.join(sourceDir, "map-core.js"),
|
||||
"plugin-loader": path.join(sourceDir, "plugin-loader.ts"),
|
||||
screenshot: path.join(sourceDir, "screenshot.ts"),
|
||||
"webgl-renderer": path.join(sourceDir, "webgl-renderer.ts"),
|
||||
ais: path.join(sourceDir, "plugins", "ais.js"),
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
type PluginGroup = "digital-modes" | "map-data" | "map" | "statistics" | "bookmarks" | "recorder" | "settings";
|
||||
|
||||
const pluginGroups: Readonly<Record<PluginGroup, readonly string[]>> = {
|
||||
"digital-modes": ["/ft8.js", "/ft4.js", "/ft2.js", "/wspr.js", "/cw.js", "/background-decode.js", "/sat.js", "/wefax.js"],
|
||||
"map-data": ["/map-core.js", "/ais.js", "/vdes.js", "/aprs.js", "/hf-aprs.js"],
|
||||
map: ["/map-core.js", "/ais.js", "/vdes.js", "/aprs.js", "/hf-aprs.js", "/sat.js", "/sat-scheduler.js"],
|
||||
statistics: ["/map-core.js"],
|
||||
bookmarks: ["/bookmarks.js"],
|
||||
recorder: [],
|
||||
settings: ["/vchan.js", "/scheduler.js"],
|
||||
};
|
||||
|
||||
const loaded = new Set<string>();
|
||||
const loading = new Map<string, Promise<void>>();
|
||||
const modulePlugins = new Set(["/ft8.js", "/ft4.js", "/ft2.js", "/wspr.js", "/cw.js"]);
|
||||
|
||||
function loadLegacyScript(path: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const script = document.createElement("script");
|
||||
script.src = path;
|
||||
script.addEventListener("load", () => { resolve(); }, { once: true });
|
||||
script.addEventListener("error", () => { reject(new Error(`Failed to load plugin script: ${path}`)); }, { once: true });
|
||||
document.body.appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
async function loadPlugin(path: string): Promise<void> {
|
||||
if (loaded.has(path)) return;
|
||||
const pending = loading.get(path);
|
||||
if (pending) return pending;
|
||||
const request = (modulePlugins.has(path) ? import(path).then(() => undefined) : loadLegacyScript(path)).then(() => {
|
||||
loaded.add(path);
|
||||
loading.delete(path);
|
||||
}).catch((error: unknown) => {
|
||||
loading.delete(path);
|
||||
throw new Error(`Failed to load plugin module: ${path}`, { cause: error });
|
||||
});
|
||||
loading.set(path, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
async function loadPlugins(group: string): Promise<void> {
|
||||
if (!(group in pluginGroups)) return;
|
||||
for (const path of pluginGroups[group as PluginGroup]) await loadPlugin(path);
|
||||
}
|
||||
|
||||
function requestPlugins(group: string): void {
|
||||
void loadPlugins(group).catch((error: unknown) => { console.error(error); });
|
||||
}
|
||||
|
||||
const loaderWindow = window as typeof window & {
|
||||
loadEagerPlugins?: () => Promise<void>;
|
||||
loadPluginsForTab?: (tab: string) => Promise<void>;
|
||||
};
|
||||
loaderWindow.loadEagerPlugins = async () => {
|
||||
await Promise.all(["digital-modes", "map-data", "bookmarks", "settings"].map(loadPlugins));
|
||||
};
|
||||
loaderWindow.loadPluginsForTab = loadPlugins;
|
||||
|
||||
document.addEventListener("click", (event) => {
|
||||
if (!(event.target instanceof Element)) return;
|
||||
const tab = event.target.closest<HTMLElement>("[data-tab]")?.dataset.tab;
|
||||
if (tab) requestPlugins(tab);
|
||||
});
|
||||
@@ -11,6 +11,8 @@ const indexPath = new URL("../../assets/web/index.html", import.meta.url);
|
||||
test("index loads the shared UI before the application", async () => {
|
||||
const html = await readFile(indexPath, "utf8");
|
||||
assert.ok(html.indexOf("/ui-core.js") < html.indexOf("/app.js"));
|
||||
assert.ok(html.indexOf("/plugin-loader.js") < html.indexOf("/app.js"));
|
||||
assert.equal(html.includes("var pluginScripts"), false);
|
||||
});
|
||||
|
||||
test("startup has no remote script or stylesheet dependencies", async () => {
|
||||
|
||||
Reference in New Issue
Block a user