refactor(client): replace the per-field settings preference controller with a namespace settings scope

bindSettingsScope mirrors the Host-side settings owner seam in the browser:
one scope per namespace publishes a snapshot store (status, section value,
revision, writability, host/memory mode), validates sections against the
namespace's serialized wire schema via dsh-client-schema-form, and keeps the
controller's listener-before-read, revisioned serialized writes, latest-wins
publication, conflict recovery, and disposal quiescence. Theme, locale, and
busy-Enter services now take the scope as a constructor collaborator, which
removes the bindPersistence/syncPreference two-phase callback pair and the
defaulted no-op persist writers; hand-written wire guards fall away in favor
of the registered schema. test-runtime gains a stubSettingsScope double.
This commit is contained in:
Yichen Jiang
2026-08-07 23:25:42 +08:00
parent 6922a942a6
commit 638c9e4bd7
37 changed files with 926 additions and 617 deletions

View File

@@ -10,6 +10,7 @@ import {
apply, inject, SETTINGS_NS, THEME_SETTINGS_NAMESPACE,
} from '@deepseek-ai/dsh-client-ui-theme/client'
import type { AppearanceRowInjected, ThemeService } from '@deepseek-ai/dsh-client-ui-theme/client'
import { ThemeSettingsSchema } from '@deepseek-ai/dsh-client-ui-theme'
import { AppearanceRow } from '../src/client/AppearanceRow.tsx'
import type { createAppearanceRowStore } from '../src/client/settings-store.ts'
@@ -33,7 +34,7 @@ async function bench(isLoopback = true) {
let preference = 'system'
const namespace = () => ({
ns: THEME_SETTINGS_NAMESPACE,
schema: {},
schema: ThemeSettingsSchema.toJSON(),
value: { preference },
applies: 'live' as const,
secrets: [],

View File

@@ -1,19 +1,20 @@
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
import { Context } from 'cordis'
import type { ThemeSnapshot } from '@deepseek-ai/dsh-client-ui-theme/client'
import { stubSettingsScope, type StubSettingsScope } from '@deepseek-ai/dsh-client-test-runtime'
import type { ThemeSettings, ThemeSnapshot } from '@deepseek-ai/dsh-client-ui-theme/client'
import { ThemeService } from '@deepseek-ai/dsh-client-ui-theme/client'
const make = (persist = vi.fn()): {
const make = (host = stubSettingsScope<ThemeSettings>()): {
ctx: Context
theme: ThemeService
events: ThemeSnapshot[]
persist: typeof persist
host: StubSettingsScope<ThemeSettings>
} => {
const ctx = new Context()
const events: ThemeSnapshot[] = []
ctx.on('theme/change', (snapshot) => { events.push(snapshot) })
return { ctx, theme: new ThemeService(ctx, persist), events, persist }
return { ctx, theme: new ThemeService(ctx, host.scope), events, host }
}
describe('ThemeService', () => {
@@ -27,12 +28,12 @@ describe('ThemeService', () => {
expect(snapshot.themes.map(t => t.id)).toEqual(['light', 'dark'])
})
it('setTheme switches, requests persistence, republishes, and keeps DOM untouched', () => {
const { theme, events, persist } = make()
it('setTheme switches, writes through the scope, republishes, and keeps DOM untouched', () => {
const { theme, events, host } = make()
theme.setTheme('dark')
expect(theme.getTheme().preference).toBe('dark')
expect(theme.getTheme().active.colorScheme).toBe('dark')
expect(persist).toHaveBeenCalledWith('dark')
expect(host.set).toHaveBeenCalledWith('preference', 'dark')
expect(events).toHaveLength(1)
expect(events[0]).toBe(theme.getTheme())
// The service never touches presentation state.
@@ -40,19 +41,26 @@ describe('ThemeService', () => {
// Same-value set is a no-op (no extra event).
theme.setTheme('dark')
expect(events).toHaveLength(1)
expect(persist).toHaveBeenCalledOnce()
expect(host.set).toHaveBeenCalledOnce()
})
it('syncs a Host preference without writing it back', () => {
const { theme, events, persist } = make()
theme.syncPreference('dark')
it('adopts a published Host section without writing it back', () => {
const { theme, events, host } = make()
host.publish({ status: 'ready', value: { preference: 'dark' }, revision: 1, writable: true })
expect(theme.getTheme().preference).toBe('dark')
expect(events).toHaveLength(1)
expect(persist).not.toHaveBeenCalled()
theme.syncPreference('dark')
expect(host.set).not.toHaveBeenCalled()
host.publish({ value: { preference: 'dark' }, revision: 2 })
expect(events).toHaveLength(1)
})
it('adopts a section already standing at construction', () => {
const host = stubSettingsScope<ThemeSettings>()
host.publish({ status: 'ready', value: { preference: 'dark' }, revision: 1, writable: true })
const { theme } = make(host)
expect(theme.getTheme().preference).toBe('dark')
})
it('throws on unknown setTheme ids, duplicate registration, and the system id', () => {
const { theme } = make()
expect(() => { theme.setTheme('sepia') }).toThrow('not registered')
@@ -61,7 +69,7 @@ describe('ThemeService', () => {
})
it('registered themes join the snapshot; disposing the active one resets to default', () => {
const { theme, events, persist } = make()
const { theme, events, host } = make()
const dispose = theme.register({ id: 'sepia', colorScheme: 'light', tokens: { '--dsw-alias-bg-base': 'red' } })
expect(theme.getTheme().themes.map(t => t.id)).toEqual(['light', 'dark', 'sepia'])
theme.setTheme('sepia')
@@ -71,7 +79,7 @@ describe('ThemeService', () => {
expect(theme.getTheme().themes.map(t => t.id)).toEqual(['light', 'dark'])
// Custom ids are in-process extension themes; only the built-in product
// preferences cross the Host settings schema.
expect(persist).not.toHaveBeenCalled()
expect(host.set).not.toHaveBeenCalled()
// register + set + dispose = three publishes; disposer is idempotent.
expect(events.length).toBe(3)
dispose()
@@ -95,11 +103,11 @@ describe('ThemeService', () => {
expect(events.map(e => e.revision)).toEqual([1, 2, 3, 4])
})
it('uses a no-op persistence callback when constructed directly', () => {
const ctx = new Context()
const theme = new ThemeService(ctx)
theme.setTheme('dark')
expect(theme.getTheme().preference).toBe('dark')
it('context dispose releases the scope subscription', async () => {
const { ctx, host } = make()
expect(host.listenerCount()).toBe(1)
await ctx.fiber.dispose()
expect(host.listenerCount()).toBe(0)
})
describe('prefers-color-scheme resolution (stubbed matchMedia)', () => {