From 490a79c69308a4005dd6a1e1dfb2fd8a55dad42d Mon Sep 17 00:00:00 2001
From: Tianyi Cui <53024+tianyicui@users.noreply.github.com>
Date: Thu, 17 Sep 2026 11:57:25 +0800
Subject: [PATCH] fix(client): release replaced sidebar store adoptions
---
.../client/ui-sidebar-right/README.i18n.yaml | 4 +-
packages/client/ui-sidebar-right/README.md | 2 +-
packages/client/ui-sidebar-right/README.zh.md | 2 +-
.../ui-sidebar-right/src/client/index.ts | 17 ++++---
.../tests/apply.client.spec.ts | 45 +++++++++++++++++++
5 files changed, 59 insertions(+), 11 deletions(-)
diff --git a/packages/client/ui-sidebar-right/README.i18n.yaml b/packages/client/ui-sidebar-right/README.i18n.yaml
index 041ac5c789..00a415369a 100644
--- a/packages/client/ui-sidebar-right/README.i18n.yaml
+++ b/packages/client/ui-sidebar-right/README.i18n.yaml
@@ -2,5 +2,5 @@
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write packages/client/ui-sidebar-right/README.md
-README.md: a2ad8e17e58e04f10efb97236951cd172c654f95
-README.zh.md: 5ce03a0f712aa4ca8ef29404ff1a75467d2ec317
+README.md: 118fb24627143f077c36292d208086d1f61debe1
+README.zh.md: 73991c329583243d49a5d509b37a57665fd22c5d
diff --git a/packages/client/ui-sidebar-right/README.md b/packages/client/ui-sidebar-right/README.md
index a2ad8e17e5..118fb24627 100644
--- a/packages/client/ui-sidebar-right/README.md
+++ b/packages/client/ui-sidebar-right/README.md
@@ -93,7 +93,7 @@ Two more seats extend what is already there: `sidebar.right.tab.guide` (chain) r
## The Tab domain
-The Tab domain retains navigation, an abort signal, and bound actions per (Session, tab id). A private assembly callback adopts each Session's store, reconciles restored records immediately, and follows its commits. Only record removal or plugin unload aborts the signal; closing the sidebar and switching Sessions retain records, while undo restores a new occurrence. `useTabInfo()` composes framework-bound store and navigation hooks without manual component subscriptions or render-time record creation. `tab.actions` always target their own Session; `tab.visible` distinguishes bodies from titles, and floating tabs remain visible when the sidebar closes. `adopt` is absent from the public controller.
+The Tab domain retains navigation, an abort signal, and bound actions per (Session, tab id). A private assembly callback adopts each Session's store, reconciles restored records immediately, and follows its commits. Reconstructing a Session store replaces its adoption, releasing the old subscription and cleanup reference. Only record removal or plugin unload aborts the signal; closing the sidebar and switching Sessions retain records, while undo restores a new occurrence. `useTabInfo()` composes framework-bound store and navigation hooks without manual component subscriptions or render-time record creation. `tab.actions` always target their own Session; `tab.visible` distinguishes bodies from titles, and floating tabs remain visible when the sidebar closes. `adopt` is absent from the public controller.
Tab owners register `registerCloseHandler(kind, handler)` through an effect. A handler synchronously retains any background cleanup before allowing explicit close or replacement. The resource owner tracks completion and retry; the sidebar does not wait. A thrown handler preserves the tab. Collapse, presentation changes and plugin disposal do not invoke close handlers; the tab abort signal identifies occurrence disposal, not an explicit close.
diff --git a/packages/client/ui-sidebar-right/README.zh.md b/packages/client/ui-sidebar-right/README.zh.md
index 5ce03a0f71..73991c3295 100644
--- a/packages/client/ui-sidebar-right/README.zh.md
+++ b/packages/client/ui-sidebar-right/README.zh.md
@@ -93,7 +93,7 @@ tab 类型分两阶段注册,随包发布的引导类型走的正是别的包
## Tab 域
-Tab 域按(Session,Tab id)保留导航、中止信号与绑定动作。私有装配回调采用各 Session 的 store,立即对齐恢复的记录,再跟随其提交。记录消失或插件卸载才中止 signal,收起和切会话不销毁记录;undo 恢复的是新 occurrence。`useTabInfo()` 组合框架绑定的 store 与导航 hook,不在组件中手写订阅或在渲染时创建记录。`tab.actions` 始终作用于自己的会话;`tab.visible` 区分正文与标题,浮窗不受整栏收起影响。`adopt` 不在公开控制器上。
+Tab 域按(Session,Tab id)保留导航、中止信号与绑定动作。私有装配回调采用各 Session 的 store,立即对齐恢复的记录,再跟随其提交。同一 Session 的 store 重建会替换其 `adopt` 注册,同时释放旧订阅与清理引用。记录消失或插件卸载才中止 signal,收起和切会话不销毁记录;undo 恢复的是新 occurrence。`useTabInfo()` 组合框架绑定的 store 与导航 hook,不在组件中手写订阅或在渲染时创建记录。`tab.actions` 始终作用于自己的会话;`tab.visible` 区分正文与标题,浮窗不受整栏收起影响。`adopt` 不在公开控制器上。
标签页所有者通过 effect 注册 `registerCloseHandler(kind, handler)`。handler 在允许显式关闭或替换前同步保存后台清理任务。资源所有者跟踪完成和重试,侧栏不等待清理。handler 抛错时保留标签页。折叠、展示方式改变和插件卸载不调用关闭 handler;tab abort signal 标识 occurrence 卸载,不代表显式关闭。
diff --git a/packages/client/ui-sidebar-right/src/client/index.ts b/packages/client/ui-sidebar-right/src/client/index.ts
index f4131a7031..c50c757f23 100644
--- a/packages/client/ui-sidebar-right/src/client/index.ts
+++ b/packages/client/ui-sidebar-right/src/client/index.ts
@@ -122,16 +122,18 @@ export function apply(ctx: ClientContext): void {
ctx.effect(() => {
const handle = createSidebarRightStore(() => defaultSeed(tabs))
- // The runtime mints one instance of this handle per session (the scope key
- // is the session id) and caches it per key. Each is adopted as it is minted,
- // so a tab's own action reaches its session's store while another session
- // is on screen, and that store's commits sync the Tab domain themselves.
- const adoptions: Array<() => void> = []
+ // Each Session Context generation owns one Store. Background tab actions
+ // use the latest adoption for that Session.
+ const adoptions = new Map void>()
const store: typeof handle = {
...handle,
create: (scopeKey) => {
const instance = handle.create(scopeKey)
- if (scopeKey !== undefined) adoptions.push(adopt(scopeKey as SessionId, instance))
+ if (scopeKey !== undefined) {
+ const sessionId = scopeKey as SessionId
+ adoptions.get(sessionId)?.()
+ adoptions.set(sessionId, adopt(sessionId, instance))
+ }
return { ...instance, clearPersisted() {
instance.clearPersisted()
if (scopeKey !== undefined) forget(scopeKey as SessionId)
@@ -212,7 +214,8 @@ export function apply(ctx: ClientContext): void {
disposeExpand()
disposeSeat()
for (const dispose of disposeTypes.reverse()) dispose()
- for (const release of adoptions) release()
+ for (const release of adoptions.values()) release()
+ adoptions.clear()
}
}, 'ui-sidebar-right: seats and shipped tab type')
}
diff --git a/packages/client/ui-sidebar-right/tests/apply.client.spec.ts b/packages/client/ui-sidebar-right/tests/apply.client.spec.ts
index 6afbbd8235..530334eae2 100644
--- a/packages/client/ui-sidebar-right/tests/apply.client.spec.ts
+++ b/packages/client/ui-sidebar-right/tests/apply.client.spec.ts
@@ -18,6 +18,7 @@ import { apply as hostApply } from '../src/index.ts'
import { SidebarRightController } from '../src/client/service.ts'
import { SidebarRightTabRegistry } from '../src/client/tab-registry.ts'
import type { createSidebarRightStore } from '../src/client/stores.ts'
+import * as sidebarStores from '../src/client/stores.ts'
import { RightbarSeat } from '../src/client/shell/SidebarRight.tsx'
import { RightbarRoot } from '../src/client/shell/RightbarRoot.tsx'
import { ExpandButton } from '../src/client/shell/ExpandButton.tsx'
@@ -173,6 +174,50 @@ describe('ui-sidebar-right apply', () => {
expect(ctx.sidebarRight.openTabs.getSnapshot()).toEqual([])
})
+ it('releases replaced store adoptions once and keeps the latest store for each Session', async () => {
+ const originalFactory = sidebarStores.createSidebarRightStore
+ const stops: Array void>>> = []
+ const factory = vi.spyOn(sidebarStores, 'createSidebarRightStore').mockImplementation((seed) => {
+ const handle = originalFactory(seed)
+ return {
+ ...handle,
+ create(scopeKey) {
+ const instance = handle.create(scopeKey)
+ return {
+ ...instance,
+ subscribe(listener) {
+ const stop = vi.fn(instance.subscribe(listener))
+ stops.push(stop)
+ return stop
+ },
+ }
+ },
+ }
+ })
+ let b: Awaited> | undefined
+ try {
+ b = await boot()
+ const handle = b.seat('rightbar.session').store as ReturnType
+ const other = 's-other' as SessionId
+ handle.create(SESSION)
+ handle.create(other)
+ handle.create(SESSION)
+ handle.create(other)
+ const current = handle.create(SESSION)
+ const currentOther = handle.create(other)
+ expect(stops.map(stop => stop.mock.calls.length)).toEqual([1, 1, 1, 1, 0, 0])
+ current.actions.setExpanded(SESSION, true)
+ currentOther.actions.setExpanded(other, true)
+ expect(new Set(b.ctx.sidebarRight.openTabs.getSnapshot().map(tab => tab.sessionId)))
+ .toEqual(new Set([SESSION, other]))
+ await b.fiber.dispose()
+ expect(stops.map(stop => stop.mock.calls.length)).toEqual([1, 1, 1, 1, 1, 1])
+ } finally {
+ await b?.ctx.fiber.dispose()
+ factory.mockRestore()
+ }
+ })
+
it('hands the guide body the registry\'s entry boxes, observable', async () => {
const { ctx, seat, injectedOf } = await boot()
const { hooks: { guideEntries } } = injectedOf(seat('sidebar.right.pane.tab')) as GuideInjected