From 673707cef75b355a4eb9bb93d00926e48e0f63d0 Mon Sep 17 00:00:00 2001 From: ymac <707337118@qq.com> Date: Sun, 27 Sep 2026 12:21:41 +0800 Subject: [PATCH] fix(native): stop rebuilding the files takeover on every state pulse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `sync()`'s cleanup loop compared the live registrations against the DESCRIPTOR list, which never contains the `files` takeover (its key is `FILES_KIND`, not a descriptor id). Every store/service notification — a Session switch is one — therefore disposed and immediately rebuilt it: [native] DISPOSE type dsh-better-sidebar:files [native] register type dsh-better-sidebar:files Disposal unmounts that kind's body, so the explorer's own component state (its scroll offset) was rebuilt on every pulse even when the plugin-side record survived. Its lifetime really belongs to the editor type's switch, which the loop below already handles; skip it in the cleanup pass. Pinned by "keeps the files takeover registered across a Session switch pulse": reverting the guard turns it red (`expected 2 to be 1` — 2 is the rebuilt registration count). Rebased onto #777 (`4099700`): the guard itself now lives on `main` — its cleanup pass already skips `FILES_KIND` and releases through `disposeSafely` — so what remains in this commit is the comment above that guard, recording the second reason it is load-bearing (a re-registration on an already inactive context orphaned the id), plus the regression spec below. --- src/client/native/index.ts | 9 ++++--- tests/native-surface.spec.ts | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/client/native/index.ts b/src/client/native/index.ts index 44b23edc..b4224045 100644 --- a/src/client/native/index.ts +++ b/src/client/native/index.ts @@ -379,10 +379,11 @@ export function registerNativeSurface(deps: NativeSurfaceDeps): () => void { wanted.set(descriptor.id, () => registerDescriptor(descriptor)) } for (const [descriptorId, registration] of live) { - // `files` is this module's OWN takeover, never a descriptor: it is - // owned by the editor-type switch below. Re-creating it here on every - // notification used to put its host-side type through a - // tear-down/re-register window on each store commit — and a + // `files` is this module's OWN takeover, never a descriptor (`wanted` + // only ever holds descriptor ids): its lifetime belongs to the + // editor-type switch below. Disposing it here on EVERY notification + // (any prefs/state pulse, a Session switch included) re-mounted the + // explorer body and lost the tree's own component state — and a // re-registration attempted on an already-inactive context (a plugin // reload) left the type registered with nobody holding its disposer, // so the same id could never be registered again for the rest of the diff --git a/tests/native-surface.spec.ts b/tests/native-surface.spec.ts index 88ca574c..4ab7b427 100644 --- a/tests/native-surface.spec.ts +++ b/tests/native-surface.spec.ts @@ -213,6 +213,57 @@ describe('service routing into the native surface', () => { }) describe('registerNativeSurface lifecycle (service-driven registration)', () => { + it('keeps the files takeover registered across a Session switch pulse', () => { + // Regression: `sync()`'s cleanup loop compared the live registrations with + // the DESCRIPTOR list, which never contains the `files` takeover — so every + // store/service pulse (a Session switch is one) disposed and rebuilt it. + // That remounts the explorer body, and a body remount loses the tree's own + // component state (its scroll offset) even though the record survives. + const store = createSidebarStore() + store.setSession('s1') + const service = createBetterSidebarService(store) + service.registerTab({ id: 'editor', title: 'Files', component: () => null, icon: () => null }) + const records = createNativeTabRecords() + + const registered: string[] = [] + const disposed: string[] = [] + const registry = { + current: { + register: (definition: { id: string; kind: string }) => { + registered.push(definition.kind) + return () => { disposed.push(definition.kind) } + }, + }, + } + let runInjected: (() => void) | undefined + const ctx = { + inject: (_deps: readonly string[], callback: (injected: { get: (name: string) => unknown }) => void) => { + runInjected = () => { callback({ get: () => registry.current }) } + return { dispose: () => { runInjected = undefined } } + }, + get: () => registry.current, + slots: { + inject: (_key: string, callback: () => () => void) => callback(), + register: () => () => {}, + }, + } + const dispose = registerNativeSurface({ ctx: ctx as never, store, service, records }) + runInjected?.() + const filesCount = (): number => registered.filter(kind => kind === 'files').length + expect(filesCount(), 'the takeover registers once').toBe(1) + + // A Session switch is a store pulse: it must not tear the takeover down. + store.setSession('s2') + expect(filesCount(), 'a Session switch must not rebuild the takeover').toBe(1) + expect(disposed, 'and nothing was disposed on the way').toEqual([]) + + // Disabling the editor type DOES retire it — the lifetime it really owns. + store.setPrefs({ ...store.getPrefs(), tabsEnabled: { editor: false } }) + expect(disposed, 'the editor switch retired the takeover (with the editor type)').toContain('files') + expect(filesCount(), 'and it was not rebuilt while disabled').toBe(1) + dispose() + }) + it('registers the native tab types when the tab-type registry ARRIVES after the slot declaration', () => { // Regression: the native seat declares `sidebar.right.pane.tab` before it // provides `sidebarRightTabs`, so a registration driven by the slot