fix(client): apply side-card prefs that arrive after the boot timeout (#1159)
This commit is contained in:
1 parent
62a9ceeed5
commit
c337adbbf7
3 files changed
+131
-14
No files matched your search
+29
-12
@@ -30,7 +30,7 @@ import { registerQuoteSource } from './selection-quote.ts'
|
||||
import { IconSideCardNav } from './icons.tsx'
|
||||
import { registerChatMarkdownMentions } from './chat-mentions.ts'
|
||||
import type { SettingsSectionIconOwnerProps } from '@deepseek-ai/dsh-client-ui-settings/client'
|
||||
import { loadBootDecision } from './prefs.ts'
|
||||
import { awaitBootDecision, type BootDecision } from './prefs.ts'
|
||||
import { SideCardSection } from './SideCardSection.tsx'
|
||||
import { api } from './api.ts'
|
||||
import { LOCALE_NS, attachLocale, attachBetterLocale, t, zh, en } from './locales.ts'
|
||||
@@ -252,6 +252,13 @@ export function apply(ctx: Context): void {
|
||||
void revalidateChunksOnReactivate()
|
||||
ctx.effect(() => {
|
||||
let disposed = false
|
||||
// Settings updates can start a sync while an earlier one is still in
|
||||
// flight. Every sync gets a newer generation, and an answer may only
|
||||
// apply when it is not older than the last applied one: the boot
|
||||
// read's late continuation must never overwrite a decision a later
|
||||
// settings update already adopted.
|
||||
let syncGeneration = 0
|
||||
let appliedGeneration = 0
|
||||
let root: Root | undefined
|
||||
let host: HTMLDivElement | undefined
|
||||
let mounted = false
|
||||
@@ -370,23 +377,33 @@ export function apply(ctx: Context): void {
|
||||
// from ONE settings fetch BEFORE the first session seeds, so a
|
||||
// brand-new conversation opens (or stays closed) at the chosen width
|
||||
// from first paint. A settings route failure falls back to the schema
|
||||
// defaults; the sidebar still mounts (a stalled wire gives up after
|
||||
// the timeout and mounts on the defaults — the external-disable check
|
||||
// rides the same fetch, so one round trip covers both decisions).
|
||||
const decision = await Promise.race([
|
||||
loadBootDecision(api),
|
||||
new Promise<null>(resolve => { window.setTimeout(() => resolve(null), 2000) }),
|
||||
])
|
||||
if (disposed) return
|
||||
if (decision !== null) {
|
||||
// defaults. A stalled wire gives up after the first-paint budget and
|
||||
// mounts on the defaults, but the SAME read's eventual answer is
|
||||
// still adopted — a late answer must not strand the store on its
|
||||
// defaults (#1159). The external-disable check rides the same fetch,
|
||||
// so one round trip covers both decisions.
|
||||
const generation = ++syncGeneration
|
||||
const apply = (decision: BootDecision): void => {
|
||||
if (disposed || generation < appliedGeneration) return
|
||||
appliedGeneration = generation
|
||||
sidebarStore.setPrefs(decision.prefs)
|
||||
// Mutual exclusion with the dsh-web-ui family right panel: while the
|
||||
// aionui-panel provider is selected, the sidebar must not mount at
|
||||
// all. Re-evaluated on every settings-document update (live switch).
|
||||
sidebarStore.setSuspended(decision.suspended)
|
||||
if (decision.suspended) unmount()
|
||||
else mount()
|
||||
}
|
||||
if (decision?.suspended) unmount()
|
||||
else mount()
|
||||
const decision = await awaitBootDecision(api, apply)
|
||||
if (disposed) return
|
||||
if (decision !== null) {
|
||||
apply(decision)
|
||||
return
|
||||
}
|
||||
// First paint on the defaults. A late answer may already have applied
|
||||
// (its continuation can win the microtask race), and a newer sync's
|
||||
// answer may have too — both already chose mounted / unmounted.
|
||||
if (generation > appliedGeneration) mount()
|
||||
}
|
||||
// setPrefs and mount() run inside sync(); a rejection there would leave
|
||||
// the sidebar unmounted with a dead toggle as its only symptom.
|
||||
|
||||
@@ -263,3 +263,46 @@ export async function loadBootDecision(settings: SidebarSettingsClient): Promise
|
||||
return { prefs: { ...SIDEBAR_PREFS_DEFAULTS }, suspended: false }
|
||||
}
|
||||
}
|
||||
|
||||
/** The first-paint budget for the boot settings read. A cold start or a slow
|
||||
* host / LAN can push the route past this; the sidebar still mounts on the
|
||||
* defaults, and {@link awaitBootDecision} adopts the eventual answer. */
|
||||
const BOOT_DECISION_TIMEOUT_MS = 2000
|
||||
|
||||
/**
|
||||
* Resolve the boot decision within `timeoutMs`, and when the settings route
|
||||
* misses that budget, keep watching the SAME read so its answer is still
|
||||
* applied late. A timed-out boot must not strand the store on its defaults
|
||||
* forever: the store's own `settings/document-updated` path fires on a
|
||||
* DOCUMENT change, not on a late read, so a disabled third-party tab type
|
||||
* would otherwise stay registered until the user toggled it (#1159).
|
||||
*
|
||||
* `loadBootDecision` never rejects (it falls back to the schema defaults), so
|
||||
* only the late-resolving case needs a continuation. `onLate` runs at most
|
||||
* once, and only when the timeout won the race — never after an in-time
|
||||
* answer. The caller still owns its liveness / supersession checks (a
|
||||
* disposed fiber, or a newer settings update that already applied).
|
||||
* @param settings - the settings wire face (the plugin api by default).
|
||||
* @param onLate - receives the answer that landed after the timeout.
|
||||
* @param timeoutMs - the first-paint budget in milliseconds.
|
||||
* @returns the in-time decision, or null when the timeout won the race.
|
||||
*/
|
||||
export async function awaitBootDecision(
|
||||
settings: SidebarSettingsClient,
|
||||
onLate: (decision: BootDecision) => void,
|
||||
timeoutMs: number = BOOT_DECISION_TIMEOUT_MS,
|
||||
): Promise<BootDecision | null> {
|
||||
const read = loadBootDecision(settings)
|
||||
let timer: ReturnType<typeof setTimeout> | undefined
|
||||
const timeout = new Promise<null>((resolve) => {
|
||||
timer = setTimeout(() => resolve(null), timeoutMs)
|
||||
})
|
||||
const decision = await Promise.race([read, timeout])
|
||||
clearTimeout(timer)
|
||||
if (decision !== null) return decision
|
||||
// The budget expired: the caller mounts on the defaults now, and this
|
||||
// continuation adopts the same read's answer when it lands. No catch —
|
||||
// loadBootDecision cannot reject.
|
||||
void read.then(onLate)
|
||||
return null
|
||||
}
|
||||
+59
-2
@@ -1,5 +1,12 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { loadBootDecision, loadExternalDisable, loadPrefs, type SidebarSettingsClient } from '../src/client/prefs.ts'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
awaitBootDecision,
|
||||
loadBootDecision,
|
||||
loadExternalDisable,
|
||||
loadPrefs,
|
||||
type BootDecision,
|
||||
type SidebarSettingsClient,
|
||||
} from '../src/client/prefs.ts'
|
||||
import { SIDEBAR_PREFS_DEFAULTS } from '../src/prefs-shared.ts'
|
||||
|
||||
/** A fake settings wire face whose settingsGet resolves to one raw value. */
|
||||
@@ -302,3 +309,53 @@ describe('boot decision (one fetch for prefs + external disable)', () => {
|
||||
expect(decision.prefs.customCss).toBe('html { }')
|
||||
})
|
||||
})
|
||||
|
||||
/** A promise plus its resolver, for a settings read that answers late. */
|
||||
function deferred<T>(): { promise: Promise<T>; resolve: (value: T) => void } {
|
||||
let resolve!: (value: T) => void
|
||||
const promise = new Promise<T>((settle) => { resolve = settle })
|
||||
return { promise, resolve }
|
||||
}
|
||||
|
||||
describe('boot decision timeout (slow settings route)', () => {
|
||||
afterEach(() => { vi.useRealTimers() })
|
||||
|
||||
it('adopts a prefs answer that lands after the first-paint budget', async () => {
|
||||
vi.useFakeTimers()
|
||||
const view = deferred<{ value?: unknown; revision?: number; externalDisable?: boolean }>()
|
||||
const slow: SidebarSettingsClient = {
|
||||
settingsGet: () => view.promise,
|
||||
settingsUpdate: async () => ({ value: {}, revision: 2 }),
|
||||
}
|
||||
const late: BootDecision[] = []
|
||||
const pending = awaitBootDecision(slow, (decision) => { late.push(decision) }, 2000)
|
||||
await vi.advanceTimersByTimeAsync(2000)
|
||||
expect(await pending, 'the boot path mounts on the defaults at the budget').toBeNull()
|
||||
expect(late, 'no answer exists yet, so nothing is applied').toEqual([])
|
||||
|
||||
// The SAME read answers after the budget. The store's own
|
||||
// settings/document-updated path only fires on a DOCUMENT change, so this
|
||||
// continuation is the only way a disabled third-party tab type (or the
|
||||
// external-disable flag) reaches the store on a slow boot (#1159).
|
||||
view.resolve({
|
||||
value: { tabsEnabled: { 'dsh-mnemon:memory': false }, customCss: 'html { }' },
|
||||
revision: 1,
|
||||
externalDisable: true,
|
||||
})
|
||||
await vi.advanceTimersByTimeAsync(0)
|
||||
expect(late).toHaveLength(1)
|
||||
expect(late[0]?.prefs.tabsEnabled).toEqual({ 'dsh-mnemon:memory': false })
|
||||
expect(late[0]?.prefs.customCss).toBe('html { }')
|
||||
expect(late[0]?.suspended).toBe(true)
|
||||
})
|
||||
|
||||
it('never runs the late continuation when the answer wins the race', async () => {
|
||||
vi.useFakeTimers()
|
||||
const late = vi.fn()
|
||||
const decision = await awaitBootDecision(wire({ customCss: 'html { }' }), late, 2000)
|
||||
expect(decision?.prefs.customCss).toBe('html { }')
|
||||
expect(decision?.suspended).toBe(false)
|
||||
await vi.advanceTimersByTimeAsync(2000)
|
||||
expect(late, 'an in-time answer must not be applied twice').not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user