fix(tree): 改名/删除的调解覆盖后台会话的底部工作台页签(#915 后续)
#912 让调解同时走原生承载面的记录,但底部工作台那一路仍只读**当前会话**的 快照(`pathTabsOf` 吃 `store.getSnapshot()`),而底部工作台是**按会话**存的: 后台会话里开着的文件页(`target: 'bottom'` 的打开、或在原生承载面之前持久化 下来的布局)既不跟着改名、也不随删除关闭,切回去的下一次保存照样把改名掉 / 删掉的文件重新创建出来。 - 枚举改走 `store.getSessionStates()`:**每个已缓存会话**的底部工作台都在内, 每行带上**自己的会话**(原生那一路本来就有 seat 会话);`PathTab.sessionId` 随之必填。 - 写入带会话:`updateTab` / `closeTab` 遇到**不在屏的会话**改走定向通道 (`store.reduceFor`,与定向 `openTab(seed, scope)` 同一条路:写进那个会话 自己的布局,不发布、不通知);当前会话继续走发布通道,界面照常重渲染。 - 删除的草稿确认继续问在**内容会话**里(草稿注册表的键是 `sessionId::tabId`)。 测试:`tests/editor-unsaved-guard.spec.tsx` 的 tree rename/delete reconciliation 组 +2 例(后台会话的底部页面跟着改名、且当前会话既没被写也没被重新发布;删除时 按它自己的草稿先问、关在它自己的布局里)。判别性实测:枚举收回「只看当前会话」 → 2 条红;定向写入换回发布通道 → 2 条红;草稿确认的会话换成当前会话 → 删除 那条红。
This commit is contained in:
1 parent
77854af361
commit
26a1eb20db
3 files changed
+144
-37
No files matched your search
+43
-4
@@ -704,6 +704,11 @@ export interface BetterSidebarService {
|
||||
* strict no-op (no state churn, no callbacks). `scope` (v0.12.0+) rides
|
||||
* to the callback (its optional cwd included); absent, the callback gets
|
||||
* `{ sessionId }` of the active session.
|
||||
*
|
||||
* v0.26.0+: a scope naming a session that is **not the one on screen** closes
|
||||
* the row in THAT session's layout through the targeted channel (no publish,
|
||||
* no notify), which is how the file tree's delete reconciliation reaches a
|
||||
* background conversation's pages. Without a scope nothing changes.
|
||||
*/
|
||||
closeTab(tabId: string, scope?: SessionScope): void
|
||||
/** Subscribe to registry changes (register/dispose). */
|
||||
@@ -735,6 +740,13 @@ export interface BetterSidebarService {
|
||||
* carried (#826) is dropped with it, because that line belongs to the
|
||||
* navigation that delivered the file — an in-place switch or a rename that
|
||||
* kept it would send the next file to the previous file's line.
|
||||
*
|
||||
* v0.26.0+: a `sessionId` that is **not the session on screen** patches that
|
||||
* session's own layout through the targeted channel (no publish, no notify —
|
||||
* the same route a targeted `openTab(seed, scope)` takes), so the file tree's
|
||||
* rename reconciliation reaches a background conversation's pages instead of
|
||||
* resolving them against the active session. The active session keeps the
|
||||
* publishing path, and an omitted `sessionId` behaves exactly as before.
|
||||
*/
|
||||
updateTab(tabId: string, patch: { title?: string; path?: string; meta?: unknown }, sessionId?: string): void
|
||||
/**
|
||||
@@ -1290,6 +1302,23 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
|
||||
else if (activated !== undefined) safeCall(() => descriptor.onActivate?.(activated!, callbackScope))
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a store mutation in the session it names.
|
||||
*
|
||||
* A session that is not on screen takes the TARGETED channel (`reduceFor`),
|
||||
* the same one a targeted `openTab(seed, scope)` uses: it writes that
|
||||
* session's own state and persists it without touching the published
|
||||
* snapshot. The publishing channel cannot stand in for it — the row is not in
|
||||
* the ACTIVE session's tree, so the reduce is a silent no-op at best, and
|
||||
* patching a same-numbered row of the active session is the other possible
|
||||
* outcome. The active session keeps the publishing path so the shell
|
||||
* re-renders with the new title/path.
|
||||
*/
|
||||
const reduceInSession = (sessionId: string, reducer: (state: SidebarState) => SidebarState): void => {
|
||||
if (sessionId === store.getSnapshot().sessionId) store.reduce(reducer)
|
||||
else store.reduceFor(sessionId, reducer)
|
||||
}
|
||||
|
||||
const closeTab = (tabId: string, scope?: SessionScope): void => {
|
||||
const sessionId = scope?.sessionId ?? store.getSnapshot().sessionId
|
||||
if (surface !== undefined && sessionId !== undefined) {
|
||||
@@ -1311,7 +1340,7 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
|
||||
}
|
||||
}
|
||||
let closed: SidebarTab | undefined
|
||||
store.reduce((state) => {
|
||||
const closeOne = (state: SidebarState): SidebarState => {
|
||||
// Unknown tab ids are a strict no-op: no state churn, no notify, no
|
||||
// pointless localStorage rewrite (mirrors updateTab's short-circuit).
|
||||
if (!tabOpenIn(state, tabId)) return state
|
||||
@@ -1319,7 +1348,11 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
|
||||
const leaf = leafWithTab(state.bottomSplits, tabId)
|
||||
closed = leaf?.tabs.find(tab => tab.id === tabId)
|
||||
return closeTabReducer(state, paneId, tabId)
|
||||
})
|
||||
}
|
||||
// The row lives in ITS session's tree: an explicit scope that is not on
|
||||
// screen has to close it there (see `reduceInSession`).
|
||||
if (sessionId === undefined) store.reduce(closeOne)
|
||||
else reduceInSession(sessionId, closeOne)
|
||||
if (closed !== undefined) {
|
||||
const sessionId = scope?.sessionId ?? store.getSnapshot().sessionId
|
||||
if (sessionId !== undefined) {
|
||||
@@ -1343,11 +1376,17 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe
|
||||
sessionId?: string,
|
||||
): void => {
|
||||
if (surface?.update(tabId, patch, sessionId) === true) return
|
||||
store.reduce((state) => patchTab(state, tabId, {
|
||||
const patchOne = (state: SidebarState): SidebarState => patchTab(state, tabId, {
|
||||
...(patch.title !== undefined ? { title: patch.title } : {}),
|
||||
...(patch.path !== undefined ? { path: patch.path } : {}),
|
||||
...(patch.meta !== undefined ? { meta: patch.meta } : {}),
|
||||
}))
|
||||
})
|
||||
// A named session that is not on screen is patched in ITS own state (a
|
||||
// background conversation's file page under a rename — see
|
||||
// `reduceInSession`); without one the current session keeps the publishing
|
||||
// path it always had.
|
||||
if (sessionId === undefined) store.reduce(patchOne)
|
||||
else reduceInSession(sessionId, patchOne)
|
||||
}
|
||||
|
||||
/** Activate an open tab (the tab-bar activation path; fires onActivate). */
|
||||
|
||||
@@ -15,12 +15,18 @@
|
||||
* a delete must close every tab at or under the removed path. Both ride the
|
||||
* SERVICE paths (`updateTab`/`closeTab`) rather than direct state reducers so
|
||||
* the registered lifecycle callbacks fire like any other tab mutation.
|
||||
*
|
||||
* The walk covers EVERY session the store has cached, not just the one on
|
||||
* screen: the bottom workbench is per session, and a background conversation's
|
||||
* file page has the same next-save hazard. Each row therefore names its own
|
||||
* session, and the writes go to that session (`updateTab`/`closeTab` take the
|
||||
* targeted channel for a session that is not on screen).
|
||||
*/
|
||||
import type { Context } from '../context-types.ts'
|
||||
import type { NativePathTab } from './service.ts'
|
||||
import { baseName, isWithinWorkspace, relativeTo, resolveSidebarPath } from './paths.ts'
|
||||
import { confirmDiscardDraft } from './editor-dirty.ts'
|
||||
import { allLeaves, type SidebarSnapshot, type SidebarStore } from './state.ts'
|
||||
import { allLeaves, type SidebarStore } from './state.ts'
|
||||
|
||||
/** One open tab that carries a file path, and the space it lives in. */
|
||||
interface PathTab {
|
||||
@@ -36,13 +42,15 @@ interface PathTab {
|
||||
absolute: string
|
||||
/** The session this tab's EDITOR reads and writes in — the `scope.sessionId`
|
||||
* its host registers the unsaved draft under, so it is the one the close
|
||||
* guard has to name. Absent only for a bottom-workbench tab whose store has
|
||||
* no session yet (the guard then reads conservatively rather than missing
|
||||
* the record and closing a live draft without asking). */
|
||||
* guard has to name. Absent only for a native tab whose address named no
|
||||
* session (the guard then reads conservatively rather than missing the
|
||||
* record and closing a live draft without asking). */
|
||||
scopeSessionId: string | undefined
|
||||
/** The native tab's seat session; absent for a bottom-workbench tab, whose
|
||||
* updates resolve against the store's current session. */
|
||||
sessionId?: string
|
||||
/** The session the tab is WRITTEN through: the native record's seat, or the
|
||||
* bottom-workbench row's own session — every row names its session now, so a
|
||||
* background conversation's pages are patched in place instead of resolving
|
||||
* against whatever session happens to be on screen. */
|
||||
sessionId: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,28 +118,32 @@ export function clearRetargetedPath(key: string): void {
|
||||
retargetedPaths.delete(key)
|
||||
}
|
||||
|
||||
/** Every open tab that carries a file path, from BOTH tab spaces. */
|
||||
function pathTabsOf(snapshot: SidebarSnapshot, native: readonly NativePathTab[]): PathTab[] {
|
||||
/** Every open tab that carries a file path, from BOTH tab spaces — the bottom
|
||||
* workbench of EVERY cached session, and the native records. */
|
||||
function pathTabsOf(store: SidebarStore, native: readonly NativePathTab[]): PathTab[] {
|
||||
const tabs: PathTab[] = []
|
||||
const state = snapshot.state
|
||||
if (state !== undefined) {
|
||||
// A bottom-workbench row resolves its scope against the store's session, so
|
||||
// that same session is the one its editor registered its draft under — the
|
||||
// key's `?? ''` is only the marker's spelling for "no session".
|
||||
const session = snapshot.sessionId ?? ''
|
||||
// Every session the store has cached, not just the one on screen: a
|
||||
// conversation the reader is not looking at can hold file pages of its own
|
||||
// (a `target: 'bottom'` open, a layout persisted back when files landed in
|
||||
// the workbench), and a rename/delete has to reach them too — the first save
|
||||
// after switching back would otherwise recreate the renamed-away file, which
|
||||
// is the very data problem this module exists to prevent. Each row therefore
|
||||
// carries its OWN session: it is both the identity half of the rename marker
|
||||
// and the channel its write goes through.
|
||||
for (const [sessionId, state] of store.getSessionStates()) {
|
||||
for (const leaf of allLeaves(state.bottomSplits)) {
|
||||
for (const tab of leaf.tabs) {
|
||||
// A bottom-workbench row carries an absolute path already, and it is the
|
||||
// same string the tree hands this module — one spelling, both fields.
|
||||
if (tab.path !== undefined) {
|
||||
tabs.push({
|
||||
key: pathTabKey(session, tab.id),
|
||||
id: tab.id,
|
||||
path: tab.path,
|
||||
absolute: tab.path,
|
||||
scopeSessionId: snapshot.sessionId,
|
||||
})
|
||||
}
|
||||
if (tab.path === undefined) continue
|
||||
tabs.push({
|
||||
key: pathTabKey(sessionId, tab.id),
|
||||
id: tab.id,
|
||||
path: tab.path,
|
||||
absolute: tab.path,
|
||||
scopeSessionId: sessionId,
|
||||
sessionId,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -174,15 +186,13 @@ function movedPath(path: string, oldPath: string, newPath: string): string | und
|
||||
* base name, matching how openFile titles editor tabs).
|
||||
*
|
||||
* Files and anything under a renamed DIRECTORY are retargeted — the same
|
||||
* subtree rule the delete path uses. The move is announced through
|
||||
* {@link markRetargetedPath} so the editor keeps its document (and an unsaved
|
||||
* draft) instead of reloading the new name. */
|
||||
* subtree rule the delete path uses — in EVERY session the store has cached.
|
||||
* The move is announced through {@link markRetargetedPath} so the editor keeps
|
||||
* its document (and an unsaved draft) instead of reloading the new name. */
|
||||
export function retargetPathTabs(ctx: Context, store: SidebarStore, oldPath: string, newPath: string): void {
|
||||
const service = ctx.get('betterSidebar')
|
||||
if (service === undefined) return
|
||||
// The tabs live in the snapshot's session: native ids restart per session.
|
||||
const snapshot = store.getSnapshot()
|
||||
for (const tab of pathTabsOf(snapshot, service.nativePathTabs())) {
|
||||
for (const tab of pathTabsOf(store, service.nativePathTabs())) {
|
||||
const moved = movedPath(tab.absolute, oldPath, newPath)
|
||||
if (moved === undefined) continue
|
||||
// The announcement has to match what the editor will see: it compares the
|
||||
@@ -200,17 +210,19 @@ export function retargetPathTabs(ctx: Context, store: SidebarStore, oldPath: str
|
||||
*
|
||||
* The question is asked in the tab's OWN session: the draft registry is keyed
|
||||
* by `sessionId::tabId`, so a bare id read would look up a same-numbered tab in
|
||||
* some other session (or nothing at all) and close a live draft unprompted. */
|
||||
* some other session (or nothing at all) and close a live draft unprompted.
|
||||
* Every session the store has cached is walked (a background conversation's
|
||||
* page would otherwise come back with its next save). */
|
||||
export function closePathTabs(ctx: Context, store: SidebarStore, target: string, unsavedMessage: string): void {
|
||||
const service = ctx.get('betterSidebar')
|
||||
if (service === undefined) return
|
||||
for (const tab of pathTabsOf(store.getSnapshot(), service.nativePathTabs())) {
|
||||
for (const tab of pathTabsOf(store, service.nativePathTabs())) {
|
||||
// `isWithinWorkspace` is true for the target itself too, so one test covers
|
||||
// both "this exact file" and "anything under this directory".
|
||||
if (!isWithinWorkspace(target, tab.absolute)) continue
|
||||
if (!confirmDiscardDraft(tab.id, unsavedMessage, tab.scopeSessionId)) continue
|
||||
// The tab is going away: a move announced for it can never be claimed.
|
||||
clearRetargetedPath(tab.key)
|
||||
service.closeTab(tab.id, tab.sessionId === undefined ? undefined : { sessionId: tab.sessionId })
|
||||
service.closeTab(tab.id, { sessionId: tab.sessionId })
|
||||
}
|
||||
}
|
||||
@@ -464,6 +464,62 @@ describe('tree rename/delete reconciliation', () => {
|
||||
expect(records.has('native-scope', 'tab4')).toBe(false)
|
||||
expect(records.has('native-scope', 'tab5')).toBe(true)
|
||||
})
|
||||
|
||||
/** The file paths one session's bottom workbench has open (tree order). */
|
||||
const bottomPaths = (store: SidebarStore, sessionId: string): string[] => {
|
||||
const state = store.getSessionStates().get(sessionId)
|
||||
if (state === undefined) return []
|
||||
return allLeaves(state.bottomSplits)
|
||||
.flatMap(leaf => leaf.tabs)
|
||||
.flatMap(tab => (tab.path === undefined ? [] : [tab.path]))
|
||||
}
|
||||
|
||||
it('retargets a BACKGROUND session\'s bottom-workbench page (every cached session is walked)', () => {
|
||||
// The bottom workbench is per session, and a file can live there on purpose
|
||||
// (`target: 'bottom'`) or in a layout persisted back when files landed in
|
||||
// the workbench. The reconciliation used to read the ACTIVE session's state
|
||||
// alone, so a background conversation's page kept the old path and its next
|
||||
// save recreated the renamed-away file — the same data problem, one
|
||||
// conversation over.
|
||||
const { store, service, context } = mountNativeSession()
|
||||
service.openTab(
|
||||
{ type: 'editor', title: 'a.ts', path: '/ws/a.ts', target: 'bottom' },
|
||||
{ sessionId: 'other-scope' },
|
||||
)
|
||||
expect(bottomPaths(store, 'other-scope')).toEqual(['/ws/a.ts'])
|
||||
expect(store.getSnapshot().sessionId).toBe('native-scope')
|
||||
|
||||
retargetPathTabs(context, store, '/ws/a.ts', '/ws/b.ts')
|
||||
|
||||
// The page follows the file in ITS session…
|
||||
expect(bottomPaths(store, 'other-scope')).toEqual(['/ws/b.ts'])
|
||||
// …while the session on screen is neither written to nor re-published: a
|
||||
// targeted write never touches the snapshot the shell renders from.
|
||||
expect(store.getSnapshot().sessionId).toBe('native-scope')
|
||||
expect(bottomPaths(store, 'native-scope')).toEqual([])
|
||||
})
|
||||
|
||||
it('closes a BACKGROUND session\'s page on delete, asking about ITS draft', () => {
|
||||
const confirmSpy = vi.fn().mockReturnValue(true)
|
||||
vi.stubGlobal('confirm', confirmSpy)
|
||||
const { store, service, context } = mountNativeSession()
|
||||
service.openTab(
|
||||
{ type: 'editor', title: 'a.ts', path: '/ws/a.ts', target: 'bottom' },
|
||||
{ sessionId: 'other-scope' },
|
||||
)
|
||||
const row = allLeaves(store.getSessionStates().get('other-scope')!.bottomSplits)
|
||||
.flatMap(leaf => leaf.tabs).find(tab => tab.path === '/ws/a.ts')!
|
||||
// The editor behind that page registered its draft in ITS session, so the
|
||||
// question has to be asked there — read against the active session the
|
||||
// record would not be found and the tab would close unprompted.
|
||||
setEditorDirty(row.id, true, 'other-scope', '/ws/a.ts')
|
||||
|
||||
closePathTabs(context, store, '/ws/a.ts', 'discard?')
|
||||
|
||||
expect(confirmSpy).toHaveBeenCalledWith('discard?')
|
||||
expect(bottomPaths(store, 'other-scope')).toEqual([])
|
||||
expect(store.getSnapshot().sessionId).toBe('native-scope')
|
||||
})
|
||||
})
|
||||
|
||||
describe('EditorHost dirty registration', () => {
|
||||
|
||||
Reference in new issue
Block a user