Skip to content

Commit feade49

Browse files
authored
feat: improve plugin loading (#1232)
* feat: imrpove plugin loading - load themes plugin on acode startup and load others after successfully starting acode * fix: add few comments and remove duplicate calls * fix: wrong typo * fix
1 parent 3b6d3f7 commit feade49

File tree

3 files changed

+101
-12
lines changed

3 files changed

+101
-12
lines changed

biome.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,28 @@
44
"organizeImports": { "enabled": true },
55
"linter": {
66
"enabled": true,
7-
"rules": { "recommended": false }
7+
"rules": {
8+
"recommended": false,
9+
"complexity": {
10+
"noForEach": "warn",
11+
"noStaticOnlyClass": "error",
12+
"noUselessSwitchCase": "error",
13+
"useFlatMap": "error"
14+
},
15+
"style": {
16+
"noNegationElse": "off",
17+
"useForOf": "error",
18+
"useNodejsImportProtocol": "error",
19+
"useNumberNamespace": "error"
20+
},
21+
"suspicious": {
22+
"noDoubleEquals": "error",
23+
"noThenProperty": "error",
24+
"useIsArray": "error"
25+
}
26+
}
827
},
28+
"javascript": { "globals": ["Global1"] },
929
"files": {
1030
"include": ["src/**/*", "utils/**/*.js", "www/**/*.js", "www/res/**/*.css"],
1131
"ignore": [

src/lib/loadPlugins.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,84 @@
11
import fsOperation from "../fileSystem";
22
import Url from "../utils/Url";
33
import loadPlugin from "./loadPlugin";
4+
import settings from "./settings";
45

5-
export default async function loadPlugins() {
6+
// theme-related keywords for determining theme plugins
7+
const THEME_IDENTIFIERS = new Set([
8+
"theme",
9+
"catppuccin",
10+
"pine",
11+
"githubdark",
12+
"radiant",
13+
"rdtheme",
14+
"ayumirage",
15+
"dust",
16+
"synthwave",
17+
"dragon",
18+
"mint",
19+
"monokai",
20+
"lumina_code",
21+
"sweet",
22+
"moonlight",
23+
"bluloco",
24+
]);
25+
26+
export default async function loadPlugins(loadOnlyTheme = false) {
627
const plugins = await fsOperation(PLUGIN_DIR).lsDir();
728
const results = [];
829
const failedPlugins = [];
30+
const loadedPlugins = new Set();
931

1032
if (plugins.length > 0) {
1133
toast(strings["loading plugins"]);
1234
}
1335

36+
let pluginsToLoad = [];
37+
const currentTheme = settings.value.appTheme;
38+
39+
if (loadOnlyTheme) {
40+
// Only load theme plugins matching current theme
41+
pluginsToLoad = plugins.filter((pluginDir) => {
42+
const pluginId = Url.basename(pluginDir.url);
43+
return isThemePlugin(pluginId) && !loadedPlugins.has(pluginId);
44+
});
45+
} else {
46+
// Load non-theme plugins that aren't loaded yet
47+
pluginsToLoad = plugins.filter((pluginDir) => {
48+
const pluginId = Url.basename(pluginDir.url);
49+
return !isThemePlugin(pluginId) && !loadedPlugins.has(pluginId);
50+
});
51+
}
52+
1453
// Load plugins concurrently
15-
const loadPromises = plugins.map(async (pluginDir) => {
54+
const loadPromises = pluginsToLoad.map(async (pluginDir) => {
1655
const pluginId = Url.basename(pluginDir.url);
56+
57+
if (loadOnlyTheme && currentTheme) {
58+
const pluginIdLower = pluginId.toLowerCase();
59+
const currentThemeLower = currentTheme.toLowerCase();
60+
const matchFound = pluginIdLower.includes(currentThemeLower);
61+
// Skip if:
62+
// 1. No match found with current theme AND
63+
// 2. It's not a theme plugin at all
64+
if (!matchFound && !isThemePlugin(pluginId)) {
65+
return;
66+
}
67+
}
68+
1769
try {
1870
await loadPlugin(pluginId);
71+
loadedPlugins.add(pluginId);
1972
results.push(true);
2073
} catch (error) {
21-
window.log("error", `Failed to load plugin: ${pluginId}`);
22-
window.log("error", error);
23-
toast(`Failed to load plugin: ${pluginId}`);
74+
console.error(`Error loading plugin ${pluginId}:`, error);
2475
failedPlugins.push(pluginId);
2576
results.push(false);
2677
}
2778
});
2879

2980
await Promise.allSettled(loadPromises);
81+
3082
if (failedPlugins.length > 0) {
3183
setTimeout(() => {
3284
cleanupFailedPlugins(failedPlugins).catch((error) => {
@@ -37,6 +89,13 @@ export default async function loadPlugins() {
3789
return results.filter(Boolean).length;
3890
}
3991

92+
function isThemePlugin(pluginId) {
93+
// Convert to lowercase for case-insensitive matching
94+
const id = pluginId.toLowerCase();
95+
// Check if any theme identifier is present in the plugin ID
96+
return Array.from(THEME_IDENTIFIERS).some((theme) => id.includes(theme));
97+
}
98+
4099
async function cleanupFailedPlugins(pluginIds) {
41100
for (const pluginId of pluginIds) {
42101
try {
@@ -45,7 +104,7 @@ async function cleanupFailedPlugins(pluginIds) {
45104
await fsOperation(pluginDir).delete();
46105
}
47106
} catch (error) {
48-
window.log("error", `Failed to cleanup plugin ${pluginId}:`, error);
107+
console.error(`Failed to cleanup plugin ${pluginId}:`, error);
49108
}
50109
}
51110
}

src/lib/main.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,22 @@ async function onDeviceReady() {
233233
window.log("error", error);
234234
toast(`Error: ${error.message}`);
235235
} finally {
236-
setTimeout(() => {
236+
setTimeout(async () => {
237237
document.body.removeAttribute("data-small-msg");
238238
app.classList.remove("loading", "splash");
239+
240+
// load plugins
241+
try {
242+
await loadPlugins();
243+
} catch (error) {
244+
window.log("error", "Failed to load plugins!");
245+
window.log("error", error);
246+
toast("Failed to load plugins!");
247+
}
239248
applySettings.afterRender();
240249
}, 500);
241250
}
251+
242252
// Check for app updates
243253
if (navigator.onLine) {
244254
cordova.plugin.http.sendRequest(
@@ -430,13 +440,13 @@ async function loadApp() {
430440

431441
new EditorFile();
432442

433-
//load plugins
443+
// load theme plugins
434444
try {
435-
await loadPlugins();
445+
await loadPlugins(true);
436446
} catch (error) {
437-
window.log("error", "Plugins loading failed!");
447+
window.log("error", "Failed to load theme plugins!");
438448
window.log("error", error);
439-
toast("Plugins loading failed!");
449+
toast("Failed to load theme plugins!");
440450
}
441451

442452
acode.setLoadingMessage("Loading folders...");

0 commit comments

Comments
 (0)