feat(plugins): improve connection lifecycle and session tabs

This commit is contained in:
Freedom authored and GitHub committed 2026-09-18 22:10:37 +08:00
1 parent 6be8d5544e
commit 1321dc9b9d
25 files changed
+849 -44

No files matched your search

@@ -38,8 +38,10 @@ export interface PluginHostBridgeApi {
notify(pluginId: string, method: string, params?: unknown): Promise<void>;
sendBinary(pluginId: string, channel: string, dataBase64: string): Promise<void>;
readAsset(pluginId: string, path: string): Promise<PluginUiAssetPayload>;
openWorkbench?(pluginId: string, contributionId: string, context?: PluginWorkbenchContext): Promise<void> | void;
openWorkbench?(pluginId: string, contributionId: string, context?: PluginWorkbenchContext, options?: { forceNew?: boolean }): Promise<void> | void;
openFilesystem?(pluginId: string, providerId: string, context?: PluginWorkbenchContext): Promise<void> | void;
/** Explicit user-triggered reconnect of an owned plugin connection (full flow, interactive password prompt allowed). */
reopenConnection?(pluginId: string, connectionId: string): Promise<void>;
closeTab?(): Promise<void> | void;
/** Persist plugin bytes through the host's native save dialog. Resolves null when the user cancels. */
saveFile?(pluginId: string, request: PluginSaveFileRequest, data: Uint8Array): Promise<PluginSaveFileResult | null>;
@@ -63,6 +65,9 @@ export class PluginHostBridge {
private locale: string;
private theme?: PluginBridgeTheme;
/** Invoked once before each iframe load generation sends its init message. */
onReinit?: () => Promise<void> | void;
constructor(
private readonly plugin: InstalledPlugin,
private readonly workbench: PluginWorkbenchContribution,
@@ -82,7 +87,7 @@ export class PluginHostBridge {
if (!target || event.source !== target || !isRecord(event.data)) return false;
if (event.data.source !== PLUGIN_MESSAGE_SOURCE || event.data.version !== BRIDGE_VERSION) return false;
if (event.data.type === "ready") {
this.sendInit();
void this.handleReady();
return true;
}
if (event.data.type === "shortcut" && event.data.shortcut === "closeTab") {
@@ -94,7 +99,52 @@ export class PluginHostBridge {
return true;
}
private handleReady(): void {
this.requestInit("ready");
}
sendInit(): void {
this.requestInit("load");
}
private requestInit(signal: "load" | "ready"): void {
if ((this.initSignals.load && this.initSignals.ready) || (signal === "load" && this.initSignals.load)) {
this.initGeneration += 1;
this.initSignals = { load: false, ready: false };
this.initStarted = false;
}
if (this.initSignals[signal]) return;
this.initSignals[signal] = true;
if (this.initStarted) return;
this.initStarted = true;
const generation = this.initGeneration;
const reinit = this.onReinit;
if (!reinit) {
if (!this.disposed && generation === this.initGeneration) this.postInit();
return;
}
void (async () => {
try {
await reinit();
} catch (error) {
console.warn("[DBX][plugin-bridge:reinit]", error);
}
// A newer load generation supersedes this one; never post a stale init.
if (!this.disposed && generation === this.initGeneration) this.postInit();
})();
}
/** Stop future posts (queued inits after an async reinit) for a torn-down bridge. */
dispose(): void {
this.disposed = true;
}
private disposed = false;
private initGeneration = 0;
private initSignals = { load: false, ready: false };
private initStarted = false;
private postInit(): void {
this.post({
source: HOST_MESSAGE_SOURCE,
version: BRIDGE_VERSION,
@@ -186,9 +236,15 @@ export class PluginHostBridge {
this.requirePermission("host.workbench");
if (!this.api.openWorkbench) throw new Error("Host workbench navigation is unavailable");
const input = requireRecord(params, "host.openWorkbench params");
await this.api.openWorkbench(this.plugin.manifest.id, requireProtocolName(input.contributionId, "workbench contribution"), isRecord(input.context) ? input.context : undefined);
await this.api.openWorkbench(this.plugin.manifest.id, requireProtocolName(input.contributionId, "workbench contribution"), isRecord(input.context) ? input.context : undefined, { forceNew: input.forceNew === true });
return null;
}
if (method === "host.reopenConnection") {
if (!this.api.reopenConnection) throw new Error("Connection reopen is unavailable");
const input = requireRecord(params, "host.reopenConnection params");
await this.api.reopenConnection(this.plugin.manifest.id, requireProtocolName(input.connectionId, "connectionId"));
return { ok: true };
}
if (method === "host.openFilesystem") {
this.requirePermission("host.filesystem");
if (!this.api.openFilesystem) throw new Error("Host filesystem navigation is unavailable");
@@ -498,8 +554,9 @@ export function pluginSdkSource(initialTheme?: PluginBridgeTheme): string {
const asset = await request('ui.readAsset', { path });
return URL.createObjectURL(new Blob([decode(asset.dataBase64)], { type: asset.contentType }));
},
openWorkbench: (contributionId, childContext) => request('host.openWorkbench', { contributionId, context: childContext }),
openWorkbench: (contributionId, childContext, options) => request('host.openWorkbench', { contributionId, context: childContext, forceNew: !!(options && options.forceNew) }),
openFilesystem: (providerId, childContext) => request('host.openFilesystem', { providerId, context: childContext }),
reopenConnection: (connectionId) => request('host.reopenConnection', { connectionId }),
saveFile: (options = {}, data) => {
if (data === undefined) return request('host.saveFile', options);
if (typeof data === 'string') return request('host.saveFile', { ...(options || {}), dataBase64: data });