| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import type { IPluginManagementService } from "@zcode/services";
- import type { ZCodePluginScope } from "@zcode/shared";
- import { logger } from "@/logger.js";
- import {
- buildWorkspaceKey,
- loadInto,
- notifyPluginManagementChange,
- } from "@/store/pluginManagementStoreLoading.js";
- import type { PluginManagementState } from "@/store/pluginManagementStore.js";
- function toMessage(error: unknown): string {
- return error instanceof Error ? error.message : String(error);
- }
- let nextToggleRequestId = 0;
- const requestsByStore = new WeakMap<() => PluginManagementState, Map<string, number>>();
- function buildToggleRequestKey(input: {
- workspacePath: string;
- workspaceIdentity: string | null;
- authorityKey: string | null;
- configScope: ZCodePluginScope | null;
- pluginId: string;
- }): string {
- const workspaceKey = buildWorkspaceKey(
- input.workspacePath,
- input.workspaceIdentity,
- input.authorityKey,
- );
- return `${workspaceKey}\u0000${input.configScope ?? "effective"}\u0000${input.pluginId}`;
- }
- export async function setPluginEnabledOptimistically(
- set: (partial: Partial<PluginManagementState>) => void,
- get: () => PluginManagementState,
- pluginId: string,
- enabled: boolean,
- pluginService: IPluginManagementService,
- scope: ZCodePluginScope = "user",
- ): Promise<boolean> {
- const { workspacePath, workspaceIdentity, authorityKey, configScope } = get();
- if (!workspacePath) return false;
- let activeToggleRequests = requestsByStore.get(get);
- if (!activeToggleRequests) {
- activeToggleRequests = new Map();
- requestsByStore.set(get, activeToggleRequests);
- }
- const requestKey = buildToggleRequestKey({
- workspacePath,
- workspaceIdentity,
- authorityKey,
- configScope,
- pluginId,
- });
- const requestId = ++nextToggleRequestId;
- activeToggleRequests.set(requestKey, requestId);
- const isCurrentRequest = (): boolean => {
- const current = get();
- return (
- activeToggleRequests.get(requestKey) === requestId &&
- current.workspacePath === workspacePath &&
- current.workspaceIdentity === workspaceIdentity &&
- current.authorityKey === authorityKey &&
- current.configScope === configScope &&
- current.togglingPluginId === pluginId
- );
- };
- const previousPlugin = get().plugins.find((plugin) => plugin.id === pluginId);
- // 启停 RPC 和后续列表刷新存在可感知延迟。先投影最终状态,让开关立刻响应;失败时
- // 在 catch 中恢复快照,避免用户把正常的异步写入误判成“点了没反应”。
- set({
- togglingPluginId: pluginId,
- error: null,
- lastFailedPluginId: null,
- ...(previousPlugin
- ? {
- plugins: get().plugins.map((plugin) =>
- plugin.id === pluginId ? { ...plugin, enabled, enabledSource: scope } : plugin,
- ),
- }
- : {}),
- });
- try {
- const result = await pluginService.setPluginEnabled({
- workspacePath,
- ...(workspaceIdentity ? { workspaceIdentity } : {}),
- pluginId,
- enabled,
- scope,
- });
- if (!isCurrentRequest()) {
- notifyPluginManagementChange({
- owner: get,
- workspacePath,
- workspaceIdentity,
- authorityKey,
- scope,
- });
- return false;
- }
- set({
- plugins: get().plugins.map((plugin) =>
- plugin.id === pluginId ? { ...plugin, ...result.plugin, enabled: result.enabled } : plugin,
- ),
- });
- await loadInto(set, get, {
- workspacePath,
- workspaceIdentity,
- authorityKey,
- configScope,
- pluginService,
- fresh: true,
- });
- notifyPluginManagementChange({
- owner: get,
- workspacePath,
- workspaceIdentity,
- authorityKey,
- scope,
- });
- return isCurrentRequest();
- } catch (error) {
- // 启停失败时必须返回 false 并恢复 optimistic projection,避免
- // 调用方继续刷新其它能力,或让界面把失败的切换误报成成功。
- logger.error("[plugins] setEnabled failed", { pluginId, enabled, error: toMessage(error) });
- if (isCurrentRequest()) {
- set({
- error: toMessage(error),
- lastFailedPluginId: pluginId,
- ...(previousPlugin
- ? {
- plugins: get().plugins.map((plugin) =>
- plugin.id === pluginId ? previousPlugin : plugin,
- ),
- }
- : {}),
- });
- }
- return false;
- } finally {
- if (isCurrentRequest()) {
- set({ togglingPluginId: null });
- }
- if (activeToggleRequests.get(requestKey) === requestId) {
- activeToggleRequests.delete(requestKey);
- }
- }
- }
|