// A feature in the workbench of one window. Its views, commands, and document types come from the static catalog, so the // navigation buttons, the command palette, and the file tree show them without the feature code. The first mount, // restore, or action command loads the feature module and starts one instance for this window. // A launch rule (`launch.ts`) can also start the instance at once. import { tr } from "../i18n/index.ts"; import type { Dispose, PluginCommand, PluginDocumentContext, PluginDocumentType, PluginView } from "../plugins/sdk.ts"; import type { FeatureEntry } from "./catalog.ts"; import { loadFeature } from "./loader.ts"; import type { FeatureContext, FeatureInstance, FeatureModule } from "./types.ts"; export class FeatureSession { readonly controller = new AbortController(); readonly views = new Map(); readonly documentTypes = new Map(); readonly commands = new Map(); readonly entry: FeatureEntry; private context: FeatureContext; private load: () => Promise; private instance?: Promise; private ready?: FeatureInstance; constructor(entry: FeatureEntry, context: FeatureContext, load: () => Promise = () => loadFeature(entry.id)) { this.entry = entry; this.context = context; this.load = load; for (const info of entry.views) { this.views.set(info.id, { id: info.id, get title() { return info.title(); }, placement: info.placement, icon: info.icon, navigation: !!info.navigation, launcher: false, mount: element => this.mountView(info.id, element), saveTabState: () => this.ready?.saveTabState?.(info.id), restoreTabState: async state => { await (await this.running()).restoreTabState?.(info.id, state); }, }); } for (const info of entry.documentTypes) { this.documentTypes.set(info.id, { id: info.id, get title() { return info.title(); }, icon: info.icon, extensions: [...info.extensions], content: info.content, identify: (path, source) => info.identify(path, source), mount: (element, document) => this.mountDocument(info.id, element, document), }); } for (const info of entry.commands) { const view = info.view; this.commands.set(info.id, { id: info.id, get title() { return info.title(); }, icon: info.icon, run: view ? () => context.openView(view) : async () => { await (await this.running()).run?.(info.id); } }); } } /** The instance of this window. A failed load leaves no instance, so a later call tries again. */ private running(): Promise { if (this.controller.signal.aborted) return Promise.reject(new Error(tr("This feature is off."))); this.instance ??= this.load().then(module => { if (this.controller.signal.aborted) throw new Error(tr("This feature is off.")); return this.ready = module.start(this.context); }).catch(reason => { this.instance = undefined; throw reason; }); return this.instance; } private async mountView(id: string, element: HTMLElement): Promise { return (await this.running()).mount(id, element); } private async mountDocument(type: string, element: HTMLElement, document: PluginDocumentContext): Promise { const instance = await this.running(); if (!instance.mountDocument) throw new Error(tr("This view is unavailable.")); return instance.mountDocument(type, element, document); } /** Mount a view, or with `document`, a document of the document type `id`. */ async mount(id: string, element: HTMLElement, document?: PluginDocumentContext): Promise { const view = document ? this.documentTypes.get(id) : this.views.get(id); if (!view) throw new Error(tr("This view is unavailable.")); const dispose = document ? await (view as PluginDocumentType).mount(element, document) : await (view as PluginView).mount(element, { surface: "view" }); return async () => { if (typeof dispose === "function") await dispose(); element.replaceChildren(); }; } async launch(id: string) { this.context.openView(id); } /** Load the module and start the instance now, without a view. Later calls use the same instance. */ async start(): Promise { await this.running(); } /** The instance of this window, for a feature with a surface that is not a view, such as the terminal panel. */ loadInstance(): Promise { return this.running(); } /** True after the instance started. */ get started(): boolean { return this.instance !== undefined; } async run(id: string) { const command = this.commands.get(id); if (!command) throw new Error(tr("This command is unavailable.")); await command.run(); } async dropTab(_data: string): Promise { throw new Error(tr("This feature cannot receive this tab.")); } /** Stop the instance. The host closes the views of the feature first. */ async stop() { this.controller.abort(); const instance = this.ready ?? await this.instance?.catch(() => undefined); this.ready = undefined; this.instance = undefined; await instance?.dispose(); } }