refactor(renderer): keep the fence-reply generation in an import-free module

A dynamic store import inside the refresh was the workaround for the
store -> hydration -> refresh -> store cycle. The counter itself has no
dependencies, so it lives alone and both writers import it statically.
This commit is contained in:
Jinwoo-H
2026-09-07 23:07:58 -04:00
parent dc6390b004
commit 69ea26a3aa
3 changed files with 22 additions and 9 deletions
@@ -0,0 +1,12 @@
// Why a separate module: hydration (inside the store) and the refresh (which writes the store)
// both bump it; keeping it import-free breaks the store → hydration → refresh → store cycle.
let generation = 0
/** A newer hydration or request makes every older in-flight fence reply stale. */
export function advanceLegacyWorkerResumeFenceGeneration(): number {
return ++generation
}
export function currentLegacyWorkerResumeFenceGeneration(): number {
return generation
}
@@ -1,14 +1,15 @@
let generation = 0
export function markLegacyWorkerResumeFencesHydrated(): void {
generation++
}
import { useAppStore } from '@/store'
import {
advanceLegacyWorkerResumeFenceGeneration,
currentLegacyWorkerResumeFenceGeneration
} from './legacy-worker-resume-fence-generation'
export async function refreshLegacyWorkerResumeFences(): Promise<void> {
const requestGeneration = ++generation
const requestGeneration = advanceLegacyWorkerResumeFenceGeneration()
try {
const fences = await window.api.app.getLegacyWorkerResumeFences()
const { useAppStore } = await import('@/store')
if (requestGeneration !== generation) {
// A reply older than a later request or hydration describes state that has since been replaced.
if (requestGeneration !== currentLegacyWorkerResumeFenceGeneration()) {
return
}
useAppStore.setState({ legacyWorkerResumeFencesByPaneKey: fences })
@@ -1,4 +1,4 @@
import { markLegacyWorkerResumeFencesHydrated } from '@/lib/legacy-worker-resume-fence-refresh'
import { advanceLegacyWorkerResumeFenceGeneration } from '@/lib/legacy-worker-resume-fence-generation'
import { readWorkspaceSessionResumeFences } from '../../../../shared/workspace-session-resume-fences'
import type { WorkspaceKey } from '../../../../shared/folder-workspace-types'
import { FLOATING_TERMINAL_WORKTREE_ID } from '../../../../shared/constants'
@@ -42,7 +42,7 @@ export function createWorkspaceTerminalHydrationActions(
: null
const ownershipTransfersByTabId = new Map<string, TerminalLayoutPtyOwnershipTransfer[]>()
set((s) => {
markLegacyWorkerResumeFencesHydrated()
advanceLegacyWorkerResumeFenceGeneration()
const runtimeSessionPlaceholders = buildRuntimeSessionPlaceholders({
repos: s.repos,
runtimeHostIdByWorkspaceSessionKey: options?.runtimeHostIdByWorkspaceSessionKey ?? {},