Understanding How Plugins Work
This page is the practical mental model for writing Acode plugins: what Acode does, what your plugin must do, and what happens during load/unload.
The Plugin Contract
From Acode's perspective, your plugin is:
- A folder in
PLUGIN_DIR - A
plugin.json - An entry script (usually
main.js)
From your perspective, your script should register:
acode.setPluginInit(pluginId, initFn)acode.setPluginUnmount(pluginId, unmountFn)(strongly recommended)
If you skip setPluginInit, your script may load, but your plugin logic will not run through Acode's lifecycle.
Lifecycle In One View
- Acode discovers plugin folders.
- It decides which plugins to load (enabled, not broken, not already loaded).
- It loads your entry script.
- It calls your registered
initwith runtime context. - Later, on disable/uninstall/reload, it calls your registered
unmount.
What You Get In init
Your init function receives:
baseUrl: internal base URL to your plugin files$page: a plugin page object for UI screenscache: object with:cacheFileUrlcacheFilefirstInitctx
Use firstInit for one-time setup or migration.
Recommended main.js Shape
js
import plugin from "../plugin.json";
function init(baseUrl, $page, cache) {
const commands = acode.require("commands");
commands.addCommand({
name: "example.open",
description: "Open Example Panel",
exec: () => {
$page.innerHTML = "<h2>Example Plugin</h2>";
$page.show();
},
});
}
function unmount() {
const commands = acode.require("commands");
commands.removeCommand("example.open");
}
acode.setPluginInit(plugin.id, init);
acode.setPluginUnmount(plugin.id, unmount);What Happens On Disable / Enable / Uninstall
- Disable:
- Acode calls
acode.unmountPlugin(id)which triggers your unmount. - Plugin runtime state is cleared (including plugin cache file).
- Acode calls
- Enable:
- Acode loads the plugin again and runs init again.
- Uninstall:
- Plugin files are removed.
- Acode runs unmount cleanup for loaded resources.
Treat init as repeatable and unmount as mandatory cleanup.
Failure Behavior You Should Know
If your plugin throws during load/init:
- it is marked as broken for the session flow,
- Acode skips loading it again until user/action retries it.
For programmatic recovery:
js
acode.clearBrokenPluginMark("com.example.plugin");Author Guidelines
- Keep
initfast; do heavy work lazily. - Register commands through
acode.require("commands"). - Always remove listeners, commands, intervals, and UI hooks in
unmount. - Avoid storing important state only in memory; use cache/settings when needed.
