Release RuneStone 0.1.4 with installable plugins and settings
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type { Contributions } from "./contracts.ts";
|
||||
|
||||
/** Contributions belong to installed package sessions. This registry never loads code. */
|
||||
export class ContributionRegistry {
|
||||
private entries = new Map<keyof Contributions, { owner: string; value: Contributions[keyof Contributions] }>();
|
||||
private listeners = new Set<(revision: number) => void>();
|
||||
private revision = 0;
|
||||
get<K extends keyof Contributions>(key: K): Contributions[K] | undefined {
|
||||
return this.entries.get(key)?.value as Contributions[K] | undefined;
|
||||
}
|
||||
register<K extends keyof Contributions>(owner: string, key: K, value: Contributions[K]): () => void {
|
||||
if (key !== "terminal" && key !== "tableFormulas") 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();
|
||||
Reference in New Issue
Block a user