Files
Netcatty/application/state/terminalCwdStore.ts
T
bincxz c239f30653 perf(terminal): cut TerminalLayer re-renders from history, cwd, and title churn
Decouple high-frequency UI state from TerminalLayer's memo boundary so
npm run dev is less dominated by full-layer reconciliation.

- Publish shell history through shellHistoryStore; History side panel
  subscribes directly instead of receiving shellHistory props.
- Track terminal CWD in terminalCwdStore so OSC 7 updates do not setState
  TerminalLayerInner; TabBridge updates side-panel live snapshots only.
- Move remote-history ownership into the History side-panel slot.
- Treat sessions as equal for pane/layer memo when only dynamicTitle or
  codingCliProviderId changes (TopTabs still sees full session updates).
- Stabilize AppView terminal handlers; thin AI vault catalog props when
  the AI panel is not visible; memo HostTreeView.
2026-07-30 16:10:08 +08:00

61 lines
1.6 KiB
TypeScript

type Listener = () => void;
/**
* Live terminal CWD map + version token.
* OSC 7 / cwd probes update this store without setState on TerminalLayer,
* so only subscribers (side-panel live snapshot bridge) re-render.
*/
class TerminalCwdStore {
private cwdBySession = new Map<string, string>();
private version = 0;
private listeners = new Set<Listener>();
getVersion = (): number => this.version;
getCwd = (sessionId: string | null | undefined): string | null => {
if (!sessionId) return null;
return this.cwdBySession.get(sessionId) ?? null;
};
subscribe = (listener: Listener): (() => void) => {
this.listeners.add(listener);
return () => {
this.listeners.delete(listener);
};
};
setCwd(sessionId: string, cwd: string | null): boolean {
const current = this.cwdBySession.get(sessionId) ?? null;
const next = cwd && cwd.trim().length > 0 ? cwd : null;
if (current === next) return false;
if (next) {
this.cwdBySession.set(sessionId, next);
} else {
this.cwdBySession.delete(sessionId);
}
this.version += 1;
for (const listener of this.listeners) {
listener();
}
return true;
}
prune(validSessionIds: ReadonlySet<string>): void {
let changed = false;
for (const sessionId of this.cwdBySession.keys()) {
if (!validSessionIds.has(sessionId)) {
this.cwdBySession.delete(sessionId);
changed = true;
}
}
if (!changed) return;
this.version += 1;
for (const listener of this.listeners) {
listener();
}
}
}
export const terminalCwdStore = new TerminalCwdStore();