Files
apes-sidebar/src/client/use-session-root.ts
T
Menghuan1918 43742be658 perf(client): 每 tab 轮询收敛为按会话共享,keepMounted 加上界
v0.25 起原生 tab 一律 keepMounted:每个访问过的 body 都是永久的,于是
useSessionRoot 的 5s 轮询、目录 watch 的 socket、侧聊推送都按「挂载过的页签数」
永久复制。真机实测(4 个访问过的 tab、15s 空闲):活着的 5s 表 5 条、
session.cwd 请求 15 次;宿主那个解析器每次未命中要 spawn git 并回扫 4000 条事件。

- use-session-root:重写为模块级按会话引用计数的 store——一条 5s 表,只在 ≥1 个
  可见订阅者时存在;页面隐藏停表;恢复可见/聚焦/取消 park 立刻补刷一次;focus 与
  refresh-files 监听全 store 一份;parked body 保留上次答案。
- 消费点传真实可见性:native body 用 nativeTab.visible,壳用 bottomOpen。
- FileTree/use-dir-watch:已有的 visible 接进目录 watch——parked 或展开集为空时不建
  socket、不重列,恢复时重连并强制补刷一次(宿主按连接持有 ≤64 个 fs.watch 句柄)。
- use-sidechat-push/SideChatView:订阅按可见性门控,lastThreadBySession 加 LRU 并
  合并 localStorage 写入。
- state.bySession 加 LRU 32(当前会话永不淘汰,淘汰前先落盘),getSessionStates()
  不再每次整份复制(syncExpanded 原先每次 store notify 都 O(会话数) 复制),
  新增定向 getSessionState(id);FileTree 的 appsCacheRef 加上界。
2026-10-15 23:05:26 +08:00

200 lines
7.6 KiB
TypeScript

/**
* The root the explorer and the git panel should use for one session.
*
* The client session list carries the session HEADER cwd, captured once when
* the session started. The HOST is the authority on where the session is
* actually working, because it can follow the agent into a linked git worktree
* including one belonging to a different repository. Ask the host, prefer
* its answer, and re-ask when the window regains focus, when the document
* becomes visible again, when a file refresh is requested, and on a slow timer
* so a mid-session worktree move is picked up.
*
* ONE store per `sessionId` (the `ui/git-status.ts` shape: a module-level Map
* of slots with `retain()` / `release()` and `useSyncExternalStore`). The host
* keeps every visited native tab body mounted, so a per-hook poller meant one
* 5 s `session.cwd` POST per live body — three visited tabs on one session
* were three requests per tick, and each of those lands in the host's
* `activeWorktreeRootOf` (up to four real `git` spawns on a cache miss, whose
* TTL is shorter than this cadence). The store therefore runs EXACTLY ONE
* timer per session, and only while at least one subscriber reports itself
* visible (`visible: false` is what the host hands a parked tab body, and the
* sidebar shell passes its own panel-open state). A parked body holds no timer
* and issues no request; `document.hidden` pauses the timers the same way.
*
* A parked body keeps its last answer — releasing the last visible subscriber
* stops the timer but does not drop the resolved root, so re-showing a tab
* paints the worktree the session was in instead of falling back to the frozen
* list cwd. Becoming visible again (a subscriber turning visible, the document
* turning visible, or the window regaining focus) refreshes immediately once
* instead of waiting for the next tick. Both listeners belong to the store:
* the old per-instance `window.focus` listener was added once per mounted hook
* and never removed for the life of the page.
*
* Shared by both client surfaces: the plugin's own sidebar shell
* (`Sidebar.tsx`) and the native DSH right-sidebar tab body
* (`native/tab-adapter.tsx`). They used to disagree — the native body read the
* frozen list summary directly, so a host-side worktree change never reached
* the file list drawn in the host's own right Sidebar.
*/
import { useCallback, useEffect, useMemo, useSyncExternalStore } from 'react'
import type { Context } from '../context-types.ts'
import { api } from './api.ts'
/** How often the host is re-asked while the document is visible. */
const SESSION_ROOT_POLL_MS = 5_000
interface Slot {
readonly sessionId: string
/** The last host answer; kept while parked (see the module comment). */
cwd: string | undefined
/** The `useSyncExternalStore` snapshot: bumped when `cwd` changes. */
version: number
/** Subscribers that report themselves visible. */
visible: number
timer: number | undefined
/** Bumped per request so a late answer from an older one is dropped. */
generation: number
listeners: Set<() => void>
}
const slots = new Map<string, Slot>()
/** Whether the store's one-shot window/document listeners are attached. */
let listening = false
/** The slot of one session, created on first use. */
function slotOf(sessionId: string): Slot {
const existing = slots.get(sessionId)
if (existing !== undefined) return existing
const slot: Slot = {
sessionId, cwd: undefined, version: 0, visible: 0,
timer: undefined, generation: 0, listeners: new Set(),
}
slots.set(sessionId, slot)
attachListeners()
return slot
}
function notify(slot: Slot): void {
slot.version += 1
for (const listener of slot.listeners) listener()
}
/**
* Ask the host where the session is working now. A hidden document issues
* nothing: the answer would only drive panels nobody can see.
*/
function fetchSlot(slot: Slot): void {
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return
const generation = ++slot.generation
api.sessionCwd({ sessionId: slot.sessionId })
.then((result) => {
if (generation !== slot.generation) return
if (slot.cwd === result.cwd) return
slot.cwd = result.cwd
notify(slot)
})
.catch(() => { /* the explorer/git rows surface their own errors */ })
}
function startTimer(slot: Slot): void {
if (slot.timer !== undefined) return
slot.timer = window.setInterval(() => { fetchSlot(slot) }, SESSION_ROOT_POLL_MS)
}
function stopTimer(slot: Slot): void {
if (slot.timer === undefined) return
window.clearInterval(slot.timer)
slot.timer = undefined
}
/** Re-read every session that has at least one visible subscriber. */
function refreshVisible(): void {
for (const slot of slots.values()) {
if (slot.visible > 0) fetchSlot(slot)
}
}
/**
* Attach the store's listeners once, on the first slot: `focus` and the file
* refresh are window events, so per-hook listeners accumulated for the life of
* the page (a body that remounts on every session switch added another).
*/
function attachListeners(): void {
if (listening) return
listening = true
window.addEventListener('focus', refreshVisible)
window.addEventListener('dsh-sidebar:refresh-files', refreshVisible)
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
// A hidden page polls nothing: pause every cadence rather than waking on
// each tick to decide it must not fetch.
for (const slot of slots.values()) stopTimer(slot)
return
}
for (const slot of slots.values()) {
if (slot.visible === 0) continue
startTimer(slot)
fetchSlot(slot)
}
})
}
/** One more visible subscriber: the first one owns the timer and re-reads now. */
function retain(slot: Slot): void {
slot.visible += 1
if (slot.visible > 1) return
startTimer(slot)
fetchSlot(slot)
}
/** One fewer visible subscriber: the last one takes the timer with it. */
function release(slot: Slot): void {
slot.visible = Math.max(0, slot.visible - 1)
if (slot.visible === 0) stopTimer(slot)
}
/** What the caller hands the hook. */
export interface SessionRootOptions {
/**
* Whether this caller is on screen. Defaults to true; a parked caller
* (`visible: false`) holds no timer and issues no request, and keeps the
* last resolved root until it comes back.
*/
visible?: boolean
}
/**
* Resolve the live workspace root for one session.
* @param ctx - client plugin context (owns the session list feed + API).
* @param sessionId - the session to resolve; `undefined` resolves to `undefined`.
* @param options - `visible: false` parks this caller (no polling).
* @returns the host's active root, falling back to the list summary cwd.
*/
export function useSessionRoot(
ctx: Context,
sessionId: string | undefined,
options: SessionRootOptions = {},
): string | undefined {
const { visible = true } = options
const summaryCwd = useSyncExternalStore(
useMemo(() => (listener: () => void) => ctx.sessions.list.subscribe(listener), [ctx]),
() => sessionId === undefined ? undefined : ctx.sessions.list.getSnapshot().byId[sessionId]?.cwd,
)
const slot = sessionId === undefined ? undefined : slotOf(sessionId)
const subscribe = useCallback((listener: () => void) => {
if (slot === undefined) return () => {}
slot.listeners.add(listener)
return () => { slot.listeners.delete(listener) }
}, [slot])
useSyncExternalStore(subscribe, () => slot?.version ?? 0, () => slot?.version ?? 0)
useEffect(() => {
if (slot === undefined || !visible) return undefined
retain(slot)
return () => { release(slot) }
}, [slot, visible])
return slot?.cwd ?? summaryCwd
}