Skip to content

Commit 2487662

Browse files
author
埃博拉酱
committed
fix: unmount old plugin before loading new version on reload
Call acode.unmountPlugin() before loading the new plugin script in loadPlugin.js. This ensures the OLD destroy() callback runs while it still exists, properly cleaning up sidebar apps, commands, event listeners, and filesystem handlers from the previous plugin instance. Without this, the new script overwrites the old unmount callback via acode.setPluginUnmount(), making the old destroy unreachable and causing resource leaks (duplicate sidebar icons, stale event handlers, etc.). Also removes the old <script> tag so the browser fetches the new source.
1 parent 59df8a0 commit 2487662

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/lib/loadPlugin.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
88
const baseUrl = await helpers.toInternalUri(Url.join(PLUGIN_DIR, pluginId));
99
const cacheFile = Url.join(CACHE_STORAGE, pluginId);
1010

11+
// Unmount the old version before loading the new one.
12+
// This MUST be done here by the framework, not by the new plugin code itself,
13+
// because once the new script loads, it calls acode.setPluginUnmount(id, newDestroy)
14+
// which overwrites the old version's destroy callback. At that point the old
15+
// destroy — which holds references to the old sidebar app, commands, event
16+
// listeners, etc. — is lost and can never be called. Letting the framework
17+
// invoke unmountPlugin() first ensures the OLD destroy() runs while it still
18+
// exists, so all old-version resources are properly cleaned up.
19+
try {
20+
acode.unmountPlugin(pluginId);
21+
} catch (e) {
22+
// Expected to throw on first install when no unmount callback is registered
23+
}
24+
25+
// Remove the old <script> tag so the browser fetches the new source.
26+
const oldScript = document.getElementById(`${pluginId}-mainScript`);
27+
if (oldScript) oldScript.remove();
28+
1129
const pluginJson = await fsOperation(
1230
Url.join(PLUGIN_DIR, pluginId, "plugin.json"),
1331
).readFile("json");

0 commit comments

Comments
 (0)