Make Git a native feature with an off switch
- Add the native feature runtime in src/lib/features/: the catalog, the switches, the first-start migration, the loader, and the feature sessions. - Add Settings → Features and the features settings section. - Add features_legacy. It reads the old plugin packages and changes nothing. - Move Git from the plugin into src/lib/features/git/, on host internals. - Remove api.git, the git contribution key, and the SDK Git types. - Add pnpm check:features. It fails when a feature module loads at start. - Update the tests, the menu inventory, the docs, and UI-055.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// A feature in the workbench of one window. Its views and commands come from the static catalog, so the
|
||||
// navigation buttons and the command palette show them without the feature code. The first mount,
|
||||
// restore, or command loads the feature module and starts one instance for this window.
|
||||
import { tr } from "../i18n/index.ts";
|
||||
import type { Dispose, PluginCommand, 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<string, PluginView>();
|
||||
readonly documentTypes = new Map<string, PluginDocumentType>();
|
||||
readonly commands = new Map<string, PluginCommand>();
|
||||
readonly entry: FeatureEntry;
|
||||
private context: FeatureContext;
|
||||
private load: () => Promise<FeatureModule>;
|
||||
private instance?: Promise<FeatureInstance>;
|
||||
private ready?: FeatureInstance;
|
||||
|
||||
constructor(entry: FeatureEntry, context: FeatureContext, load: () => Promise<FeatureModule> = () => 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.commands) {
|
||||
this.commands.set(info.id, { id: info.id, get title() { return info.title(); }, icon: info.icon, run: () => context.openView(info.view) });
|
||||
}
|
||||
}
|
||||
|
||||
/** The instance of this window. A failed load leaves no instance, so a later call tries again. */
|
||||
private running(): Promise<FeatureInstance> {
|
||||
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<Dispose> {
|
||||
return (await this.running()).mount(id, element);
|
||||
}
|
||||
|
||||
async mount(id: string, element: HTMLElement): Promise<Dispose> {
|
||||
const view = this.views.get(id);
|
||||
if (!view) throw new Error(tr("This view is unavailable."));
|
||||
const dispose = await view.mount(element);
|
||||
return async () => {
|
||||
if (typeof dispose === "function") await dispose();
|
||||
element.replaceChildren();
|
||||
};
|
||||
}
|
||||
|
||||
async launch(id: string) { this.context.openView(id); }
|
||||
|
||||
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<void> { 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user