import type { Contributions } from "./contracts.ts"; /** Contributions belong to installed package sessions. This registry never loads code. */ export class ContributionRegistry { private entries = new Map(); private listeners = new Set<(revision: number) => void>(); private revision = 0; get(key: K): Contributions[K] | undefined { return this.entries.get(key)?.value as Contributions[K] | undefined; } register(owner: string, key: K, value: Contributions[K]): () => void { if (key !== "terminal" && key !== "tableFormulas" && key !== "browser" && key !== "git") throw new Error("Unknown plugin contribution."); if (this.entries.has(key)) throw new Error(`Contribution already registered: ${key}`); const entry = { owner, value }; this.entries.set(key, entry); this.emit(); return () => { if (this.entries.get(key) !== entry) return; this.entries.delete(key); this.emit(); }; } subscribe = (listener: (revision: number) => void) => { this.listeners.add(listener); listener(this.revision); return () => { this.listeners.delete(listener); }; }; private emit() { this.revision++; for (const listener of this.listeners) listener(this.revision); } } export const pluginHost = new ContributionRegistry();