workspaceSessionRestore.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type { PersistedWorkspaceSessionEntry } from "./protocol.js";
  2. function findLocalWorkspaceSessionIndex(
  3. persistedSessions: readonly PersistedWorkspaceSessionEntry[],
  4. startIndex: number,
  5. endIndexExclusive: number,
  6. ): number | null {
  7. for (let index = startIndex; index < endIndexExclusive; index += 1) {
  8. if (persistedSessions[index]?.kind === "local") {
  9. return index;
  10. }
  11. }
  12. return null;
  13. }
  14. export function resolveStartupLocalWorkspaceSessionIndex(
  15. persistedSessions: readonly PersistedWorkspaceSessionEntry[],
  16. lastActiveTabIndex: number | undefined,
  17. ): number | null {
  18. if (persistedSessions.length === 0) {
  19. return null;
  20. }
  21. const startIndex = Math.min(Math.max(lastActiveTabIndex ?? 0, 0), persistedSessions.length - 1);
  22. const nextLocalIndex = findLocalWorkspaceSessionIndex(
  23. persistedSessions,
  24. startIndex,
  25. persistedSessions.length,
  26. );
  27. if (nextLocalIndex != null) {
  28. return nextLocalIndex;
  29. }
  30. // 启动时远程 workspace 只恢复为断连 tab,本地 host 不能把它当作可预热目标。
  31. // 从上次 active 位置找不到本地项时回绕到前面的本地项,保持 renderer active tab 和 main 预热目标一致。
  32. return findLocalWorkspaceSessionIndex(persistedSessions, 0, startIndex);
  33. }