Release RuneStone 0.1.4 with installable plugins and settings

This commit is contained in:
Knotty Wyvern
2026-10-04 15:05:51 -07:00
parent 0268c5c9f0
commit 6b7af0d527
82 changed files with 4150 additions and 212 deletions
+99
View File
@@ -0,0 +1,99 @@
import { pluginHost, type ContributionRegistry } from "./registry.ts";
import type { Dispose, PluginApi, PluginCommand, PluginModule, PluginView } from "./sdk.ts";
export function deadline<T>(work: Promise<T>, milliseconds = 5000): Promise<T> {
let timer: ReturnType<typeof setTimeout>;
return Promise.race([work, new Promise<never>((_, reject) => { timer = setTimeout(() => reject(new Error("Plugin cleanup or activation timed out. Restart RuneStone.")), milliseconds); })]).finally(() => clearTimeout(timer));
}
/** Revokes host access before cleanup, including when activation is still pending. */
export class ExternalSession {
readonly controller = new AbortController();
readonly commands = new Map<string, PluginCommand>();
readonly views = new Map<string, PluginView>();
private cleanups: Dispose[] = [];
private stopped = false;
private activation: Promise<void> = Promise.resolve();
private stopping?: Promise<void>;
private lateCleanup?: Promise<void>;
private mounted = new Set<Dispose>();
private contributions: Dispose[] = [];
private registry: ContributionRegistry;
readonly id: string;
private services: Pick<PluginApi, "workspace" | "settings" | "assets">;
private changed: () => void;
private show: (id: string) => void;
constructor(id: string, services: Pick<PluginApi, "workspace" | "settings" | "assets">, changed: () => void, show: (id: string) => void, registry: ContributionRegistry = pluginHost) {
this.registry = registry; this.id = id; this.services = services; this.changed = changed; this.show = show;
}
private check() { if (this.stopped) throw new Error("This plugin is disabled."); }
async start(module: PluginModule) {
const register = <T extends { id: string; title: string }>(map: Map<string, T>, item: T) => {
this.check();
if (!/^[a-z][a-z0-9.-]*$/.test(item.id) || !item.title?.trim() || map.has(item.id)) throw new Error("Invalid or duplicate plugin contribution.");
map.set(item.id, item); this.changed();
};
const api: PluginApi = {
id: this.id, signal: this.controller.signal,
contribute: (key, value) => { this.check(); this.contributions.push(this.registry.register(this.id, key, value)); },
registerCommand: item => { if (typeof item.run !== "function") throw new Error("A command needs a run function."); register(this.commands, item); },
registerView: item => { if (typeof item.mount !== "function") throw new Error("A view needs a mount function."); register(this.views, item); },
openView: id => { this.check(); if (!this.views.has(id)) throw new Error("Unknown plugin view."); this.show(id); },
onDispose: cleanup => { this.check(); if (typeof cleanup !== "function") throw new Error("Cleanup must be a function."); this.cleanups.push(cleanup); },
workspace: {
list: path => { this.check(); return this.services.workspace.list(path); },
read: path => { this.check(); return this.services.workspace.read(path); },
write: (path, value, expected) => { this.check(); if (typeof expected !== "string") throw new Error("Supply the original file text before writing."); return this.services.workspace.write(path, value, expected); },
},
settings: {
read: () => { this.check(); return this.services.settings.read(); },
write: value => { this.check(); return this.services.settings.write(value); },
},
assets: { read: path => { this.check(); return this.services.assets.read(path); } },
};
this.activation = Promise.resolve().then(() => {
if (typeof module.activate !== "function") throw new Error("The plugin must export activate(api).");
this.check(); return module.activate(api);
}).then(async cleanup => {
if (typeof cleanup === "function") {
if (this.stopped) { this.lateCleanup = Promise.resolve().then(cleanup); await this.lateCleanup; } else this.cleanups.push(cleanup);
}
});
await deadline(this.activation);
}
async run(id: string) { this.check(); const command = this.commands.get(id); if (!command) throw new Error("Unknown plugin command."); await command.run(); }
async mount(id: string, element: HTMLElement): Promise<Dispose> {
this.check(); const view = this.views.get(id); if (!view) throw new Error("Unknown plugin view.");
let cleaning: Promise<void> | undefined;
const pending = Promise.resolve().then(() => { this.check(); return view.mount(element); });
const cleanup = () => {
cleaning ??= (async () => {
const dispose = await pending;
if (typeof dispose === "function") await dispose();
element.replaceChildren(); this.mounted.delete(cleanup);
})();
return cleaning;
};
this.mounted.add(cleanup);
try { await deadline(pending); } catch (error) { await deadline(cleanup()).catch(() => {}); throw error; }
if (this.stopped) await cleanup();
return cleanup;
}
stop(): Promise<void> {
if (this.stopping) return this.stopping;
this.stopped = true; this.controller.abort(); this.commands.clear(); this.views.clear(); this.changed();
for (const remove of this.contributions.splice(0)) remove();
this.stopping = (async () => {
const errors: unknown[] = [];
// Activation rejection is reported by start. A timeout still blocks removal.
try { await deadline(this.activation.catch(() => {})); } catch (error) { errors.push(error); }
if (this.lateCleanup) { try { await deadline(this.lateCleanup); } catch (error) { errors.push(error); } }
for (const cleanup of [...this.mounted, ...this.cleanups.reverse()]) {
try { await deadline(Promise.resolve().then(cleanup)); } catch (error) { errors.push(error); }
}
this.cleanups = [];
if (errors.length) throw new Error(errors.map(String).join("\n"));
})();
return this.stopping;
}
}