pluginManagementStoreEnabled.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import type { IPluginManagementService } from "@zcode/services";
  2. import type { ZCodePluginScope } from "@zcode/shared";
  3. import { logger } from "@/logger.js";
  4. import {
  5. buildWorkspaceKey,
  6. loadInto,
  7. notifyPluginManagementChange,
  8. } from "@/store/pluginManagementStoreLoading.js";
  9. import type { PluginManagementState } from "@/store/pluginManagementStore.js";
  10. function toMessage(error: unknown): string {
  11. return error instanceof Error ? error.message : String(error);
  12. }
  13. let nextToggleRequestId = 0;
  14. const requestsByStore = new WeakMap<() => PluginManagementState, Map<string, number>>();
  15. function buildToggleRequestKey(input: {
  16. workspacePath: string;
  17. workspaceIdentity: string | null;
  18. authorityKey: string | null;
  19. configScope: ZCodePluginScope | null;
  20. pluginId: string;
  21. }): string {
  22. const workspaceKey = buildWorkspaceKey(
  23. input.workspacePath,
  24. input.workspaceIdentity,
  25. input.authorityKey,
  26. );
  27. return `${workspaceKey}\u0000${input.configScope ?? "effective"}\u0000${input.pluginId}`;
  28. }
  29. export async function setPluginEnabledOptimistically(
  30. set: (partial: Partial<PluginManagementState>) => void,
  31. get: () => PluginManagementState,
  32. pluginId: string,
  33. enabled: boolean,
  34. pluginService: IPluginManagementService,
  35. scope: ZCodePluginScope = "user",
  36. ): Promise<boolean> {
  37. const { workspacePath, workspaceIdentity, authorityKey, configScope } = get();
  38. if (!workspacePath) return false;
  39. let activeToggleRequests = requestsByStore.get(get);
  40. if (!activeToggleRequests) {
  41. activeToggleRequests = new Map();
  42. requestsByStore.set(get, activeToggleRequests);
  43. }
  44. const requestKey = buildToggleRequestKey({
  45. workspacePath,
  46. workspaceIdentity,
  47. authorityKey,
  48. configScope,
  49. pluginId,
  50. });
  51. const requestId = ++nextToggleRequestId;
  52. activeToggleRequests.set(requestKey, requestId);
  53. const isCurrentRequest = (): boolean => {
  54. const current = get();
  55. return (
  56. activeToggleRequests.get(requestKey) === requestId &&
  57. current.workspacePath === workspacePath &&
  58. current.workspaceIdentity === workspaceIdentity &&
  59. current.authorityKey === authorityKey &&
  60. current.configScope === configScope &&
  61. current.togglingPluginId === pluginId
  62. );
  63. };
  64. const previousPlugin = get().plugins.find((plugin) => plugin.id === pluginId);
  65. // 启停 RPC 和后续列表刷新存在可感知延迟。先投影最终状态,让开关立刻响应;失败时
  66. // 在 catch 中恢复快照,避免用户把正常的异步写入误判成“点了没反应”。
  67. set({
  68. togglingPluginId: pluginId,
  69. error: null,
  70. lastFailedPluginId: null,
  71. ...(previousPlugin
  72. ? {
  73. plugins: get().plugins.map((plugin) =>
  74. plugin.id === pluginId ? { ...plugin, enabled, enabledSource: scope } : plugin,
  75. ),
  76. }
  77. : {}),
  78. });
  79. try {
  80. const result = await pluginService.setPluginEnabled({
  81. workspacePath,
  82. ...(workspaceIdentity ? { workspaceIdentity } : {}),
  83. pluginId,
  84. enabled,
  85. scope,
  86. });
  87. if (!isCurrentRequest()) {
  88. notifyPluginManagementChange({
  89. owner: get,
  90. workspacePath,
  91. workspaceIdentity,
  92. authorityKey,
  93. scope,
  94. });
  95. return false;
  96. }
  97. set({
  98. plugins: get().plugins.map((plugin) =>
  99. plugin.id === pluginId ? { ...plugin, ...result.plugin, enabled: result.enabled } : plugin,
  100. ),
  101. });
  102. await loadInto(set, get, {
  103. workspacePath,
  104. workspaceIdentity,
  105. authorityKey,
  106. configScope,
  107. pluginService,
  108. fresh: true,
  109. });
  110. notifyPluginManagementChange({
  111. owner: get,
  112. workspacePath,
  113. workspaceIdentity,
  114. authorityKey,
  115. scope,
  116. });
  117. return isCurrentRequest();
  118. } catch (error) {
  119. // 启停失败时必须返回 false 并恢复 optimistic projection,避免
  120. // 调用方继续刷新其它能力,或让界面把失败的切换误报成成功。
  121. logger.error("[plugins] setEnabled failed", { pluginId, enabled, error: toMessage(error) });
  122. if (isCurrentRequest()) {
  123. set({
  124. error: toMessage(error),
  125. lastFailedPluginId: pluginId,
  126. ...(previousPlugin
  127. ? {
  128. plugins: get().plugins.map((plugin) =>
  129. plugin.id === pluginId ? previousPlugin : plugin,
  130. ),
  131. }
  132. : {}),
  133. });
  134. }
  135. return false;
  136. } finally {
  137. if (isCurrentRequest()) {
  138. set({ togglingPluginId: null });
  139. }
  140. if (activeToggleRequests.get(requestKey) === requestId) {
  141. activeToggleRequests.delete(requestKey);
  142. }
  143. }
  144. }