forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadPlugins.js
More file actions
243 lines (212 loc) · 6.48 KB
/
Copy pathloadPlugins.js
File metadata and controls
243 lines (212 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import fsOperation from "../fileSystem";
import Url from "../utils/Url";
import loadPlugin from "./loadPlugin";
import settings from "./settings";
// theme-related keywords for determining theme plugins
const THEME_IDENTIFIERS = new Set([
"theme",
"catppuccin",
"pine",
"githubdark",
"radiant",
"rdtheme",
"ayumirage",
"dust",
"synthwave",
"dragon",
"mint",
"monokai",
"lumina_code",
"sweet",
"moonlight",
"bluloco",
"acode.plugin.extra_syntax_highlights",
"documentsviewer",
]);
export const onPluginLoadCallback = Symbol("onPluginLoadCallback");
export const onPluginsLoadCompleteCallback = Symbol(
"onPluginsLoadCompleteCallback",
);
export const LOADED_PLUGINS = new Set();
export const BROKEN_PLUGINS = new Map();
const AUTO_DISABLED_PLUGINS = new Set();
const PLUGIN_LOAD_TIMEOUT = 15000;
const PLUGIN_DISABLE_TIMEOUT = 60000;
let pluginDisabledUpdateQueue = Promise.resolve();
let initialPluginLoadComplete = false;
class PluginLoadTimeoutError extends Error {
constructor() {
super("Plugin load timeout");
this.name = "PluginLoadTimeoutError";
}
}
export default async function loadPlugins(loadOnlyTheme = false) {
if (!loadOnlyTheme) {
initialPluginLoadComplete = false;
}
try {
const plugins = await fsOperation(PLUGIN_DIR).lsDir();
const results = [];
if (plugins.length > 0) {
toast(strings["loading plugins"]);
}
let pluginsToLoad = [];
const currentTheme = settings.value.appTheme;
const enabledMap = settings.value.pluginsDisabled || {};
if (loadOnlyTheme) {
// Only load theme plugins matching current theme
pluginsToLoad = plugins.filter((pluginDir) => {
const pluginId = Url.basename(pluginDir.url);
// Skip already loaded and plugins that were previously marked broken
return (
isThemePlugin(pluginId) &&
!LOADED_PLUGINS.has(pluginId) &&
enabledMap[pluginId] !== true &&
!BROKEN_PLUGINS.has(pluginId)
);
});
} else {
// Load non-theme plugins that aren't loaded yet and are enabled
pluginsToLoad = plugins.filter((pluginDir) => {
const pluginId = Url.basename(pluginDir.url);
// Skip theme plugins, already loaded, disabled or previously marked broken
return (
!isThemePlugin(pluginId) &&
!LOADED_PLUGINS.has(pluginId) &&
enabledMap[pluginId] !== true &&
!BROKEN_PLUGINS.has(pluginId)
);
});
}
const loadPromises = pluginsToLoad.map(async (pluginDir) => {
const pluginId = Url.basename(pluginDir.url);
if (loadOnlyTheme && currentTheme) {
const pluginIdLower = pluginId.toLowerCase();
const currentThemeLower = currentTheme.toLowerCase();
const matchFound = pluginIdLower.includes(currentThemeLower);
// Skip if:
// 1. No match found with current theme AND
// 2. It's not a theme plugin at all
if (!matchFound && !isThemePlugin(pluginId)) {
return;
}
}
try {
results.push(await loadPluginWithTimeout(pluginId));
} catch (error) {
console.error(`Error loading plugin ${pluginId}:`, error);
results.push(false);
}
});
await Promise.allSettled(loadPromises);
acode[onPluginsLoadCompleteCallback]();
return results.filter(Boolean).length;
} finally {
if (!loadOnlyTheme) {
initialPluginLoadComplete = true;
}
}
}
export function isInitialPluginLoadComplete() {
return initialPluginLoadComplete;
}
export async function loadPluginWithTimeout(pluginId, justInstalled = false) {
const pluginState = { settled: false };
const pluginLoadPromise = loadPlugin(pluginId, justInstalled)
.catch(async (error) => {
pluginState.settled = true;
await markPluginBroken(pluginId, error);
throw error;
})
.then(async () => {
pluginState.settled = true;
await markPluginLoaded(pluginId, justInstalled);
});
try {
// Let app startup continue if a plugin is slow, but keep loading it in
// the background so good plugins on slower devices can still recover.
await Promise.race([
pluginLoadPromise,
new Promise((_, rej) =>
setTimeout(
() => rej(new PluginLoadTimeoutError()),
PLUGIN_LOAD_TIMEOUT,
),
),
]);
return true;
} catch (error) {
if (error instanceof PluginLoadTimeoutError) {
markPluginTimedOut(pluginId, pluginState);
return false;
}
throw error;
}
}
async function markPluginLoaded(pluginId, justInstalled = false) {
LOADED_PLUGINS.add(pluginId);
acode[onPluginLoadCallback](pluginId);
// clear broken mark if present
if (BROKEN_PLUGINS.has(pluginId)) {
BROKEN_PLUGINS.delete(pluginId);
}
if (justInstalled || AUTO_DISABLED_PLUGINS.has(pluginId)) {
AUTO_DISABLED_PLUGINS.delete(pluginId);
await updatePluginDisabled(pluginId, false);
}
}
async function markPluginBroken(pluginId, error) {
// mark plugin as broken to avoid repeated attempts until user intervenes
BROKEN_PLUGINS.set(pluginId, {
error: String(error.message || error),
timestamp: Date.now(),
});
AUTO_DISABLED_PLUGINS.add(pluginId);
await updatePluginDisabled(pluginId, true);
}
function markPluginTimedOut(pluginId, pluginState) {
BROKEN_PLUGINS.set(pluginId, {
error: "Plugin load timeout",
timestamp: Date.now(),
});
setTimeout(async () => {
try {
if (pluginState.settled || LOADED_PLUGINS.has(pluginId)) return;
await markPluginBroken(pluginId, new Error("Plugin load timeout"));
} catch (error) {
console.error(`Failed to disable timed out plugin ${pluginId}:`, error);
window.log("error", `Failed to disable timed out plugin ${pluginId}:`);
window.log("error", error);
}
}, PLUGIN_DISABLE_TIMEOUT - PLUGIN_LOAD_TIMEOUT);
}
function updatePluginDisabled(pluginId, disabled) {
const updatePromise = pluginDisabledUpdateQueue
.catch(() => {})
.then(async () => {
const disabledMap = { ...(settings.value.pluginsDisabled || {}) };
if (!disabled && !(pluginId in disabledMap)) return;
if (disabledMap[pluginId] === disabled) return;
if (disabled) {
disabledMap[pluginId] = true;
} else {
delete disabledMap[pluginId];
}
await settings.update({ pluginsDisabled: disabledMap }, false);
});
pluginDisabledUpdateQueue = updatePromise.catch((error) => {
console.error("Failed to update plugin disabled state:", error);
window.log(
"error",
`Failed to update plugin (ID: ${pluginId}) disabled state:`,
);
window.log("error", error);
});
return updatePromise;
}
function isThemePlugin(pluginId) {
// Convert to lowercase for case-insensitive matching
const id = pluginId.toLowerCase();
// Check if any theme identifier is present in the plugin ID
return Array.from(THEME_IDENTIFIERS).some((theme) => id.includes(theme));
}