perf(sidebar): 避免已定位会话列时重复查询 DOM

- 缓存会话列仍连接时跳过 #root 全树 querySelector
- 保留 detached/HMR 回退定位与 1.5s safety net
- 补充 locator 行为测试覆盖缓存复用与节点替换
This commit is contained in:
Kunping Wang
2026-08-29 15:31:05 +08:00
committed by Menghuan1918
parent 2088ef6891
commit a6c7cf63c1
3 changed files with 84 additions and 5 deletions
+22
View File
@@ -0,0 +1,22 @@
/** Stable selector for the DSH AppFrame conversation slot. */
const CENTER_COLUMN_SELECTOR = '#root [data-slot="conversation"]'
/**
* Resolve the AppFrame center column while keeping the hot path query-free.
*
* Streaming chat output mutates `#root` at token cadence, so Sidebar's
* MutationObserver can call this once per animation frame. Once the center
* column has been found, its DOM identity is stable for the conversation
* lifecycle; checking `isConnected` is enough to reuse it without scanning
* the whole app tree again. Boot/HMR replacement still falls through to the
* document query as soon as the cached node is detached.
*/
export function resolveCenterColumn(
current: HTMLElement | null,
query: () => Element | null = () => document.querySelector(CENTER_COLUMN_SELECTOR),
): HTMLElement | undefined {
if (current !== null && current.isConnected) return current
const col = query()?.parentElement
return col !== null && col !== undefined && col.isConnected ? col : undefined
}