refactor: move plugin loading into typed registry
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
"use strict";
|
||||
const pluginGroups = {
|
||||
"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 = /* @__PURE__ */ new Set();
|
||||
const loading = /* @__PURE__ */ new Map();
|
||||
const modulePlugins = /* @__PURE__ */ new Set(["/ft8.js", "/ft4.js", "/ft2.js", "/wspr.js", "/cw.js"]);
|
||||
function loadLegacyScript(path) {
|
||||
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) {
|
||||
if (loaded.has(path)) return;
|
||||
const pending = loading.get(path);
|
||||
if (pending) return pending;
|
||||
const request = (modulePlugins.has(path) ? import(path).then(() => void 0) : loadLegacyScript(path)).then(() => {
|
||||
loaded.add(path);
|
||||
loading.delete(path);
|
||||
}).catch((error) => {
|
||||
loading.delete(path);
|
||||
throw new Error(`Failed to load plugin module: ${path}`, { cause: error });
|
||||
});
|
||||
loading.set(path, request);
|
||||
return request;
|
||||
}
|
||||
async function loadPlugins(group) {
|
||||
if (!(group in pluginGroups)) return;
|
||||
for (const path of pluginGroups[group]) await loadPlugin(path);
|
||||
}
|
||||
function requestPlugins(group) {
|
||||
void loadPlugins(group).catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
const loaderWindow = window;
|
||||
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("[data-tab]")?.dataset.tab;
|
||||
if (tab) requestPlugins(tab);
|
||||
});
|
||||
@@ -1640,74 +1640,8 @@ SPDX-License-Identifier: GPL-2.0-or-later
|
||||
<script defer src="/leaflet-ais-tracksymbol.js"></script>
|
||||
<script defer src="/webgl-renderer.js"></script>
|
||||
<script defer src="/ui-core.js"></script>
|
||||
<script defer src="/plugin-loader.js"></script>
|
||||
<script defer src="/app.js"></script>
|
||||
<script>
|
||||
// Lazy plugin loader: loads plugin scripts when their tab/feature is first activated
|
||||
(function() {
|
||||
var pluginScripts = {
|
||||
'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']
|
||||
};
|
||||
var loaded = new Set();
|
||||
var loading = new Map();
|
||||
|
||||
function loadScript(src) {
|
||||
if (loaded.has(src)) return Promise.resolve();
|
||||
if (loading.has(src)) return loading.get(src);
|
||||
|
||||
var request = new Promise(function(resolve, reject) {
|
||||
var s = document.createElement('script');
|
||||
s.src = src;
|
||||
s.onload = function() {
|
||||
loaded.add(src);
|
||||
loading.delete(src);
|
||||
resolve();
|
||||
};
|
||||
s.onerror = function() {
|
||||
loading.delete(src);
|
||||
reject(new Error('Failed to load plugin script: ' + src));
|
||||
};
|
||||
document.body.appendChild(s);
|
||||
});
|
||||
loading.set(src, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
function loadPlugins(tab) {
|
||||
var scripts = pluginScripts[tab];
|
||||
if (!scripts) return Promise.resolve();
|
||||
return scripts.reduce(function(sequence, src) {
|
||||
return sequence.then(function() { return loadScript(src); });
|
||||
}, Promise.resolve());
|
||||
}
|
||||
|
||||
function requestPlugins(tab) {
|
||||
return loadPlugins(tab).catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
// Eager plugin loading is triggered by app.js (after window.trx is set up)
|
||||
// via window.loadEagerPlugins(). Dynamic scripts are effectively async, so
|
||||
// loading them before app.js would cause map-core.js to crash when
|
||||
// window.trx is not yet defined.
|
||||
window.loadEagerPlugins = function() {
|
||||
return Promise.all(
|
||||
['digital-modes', 'map-data', 'bookmarks', 'settings'].map(requestPlugins)
|
||||
);
|
||||
};
|
||||
// Load others on tab switch
|
||||
document.addEventListener('click', function(e) {
|
||||
var tab = e.target.closest('[data-tab]');
|
||||
if (tab) requestPlugins(tab.dataset.tab);
|
||||
});
|
||||
window.loadPluginsForTab = loadPlugins;
|
||||
})();
|
||||
</script>
|
||||
<!-- Template cloning is handled by navigateToTab() in app.js -->
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user