-
Notifications
You must be signed in to change notification settings - Fork 432
fix(ui): stop the default-model picker spinning on every switch #3828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liuxiaocs7
wants to merge
1
commit into
apache:main
Choose a base branch
from
liuxiaocs7:fix/default-model-picker-spinner
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
packages/ui/src/__tests__/use-optimistic-selection.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * The default-model row (Settings › 通用 › 默认模型) drives Astryx's Selector on | ||
| * the synchronous `onChange` path so the trigger never spins. On that path the | ||
| * Selector's own optimistic value never advances, so the row supplies the | ||
| * "reflect the pick immediately" half of the fix — this hook. | ||
| * | ||
| * The clear signal is a monotonic read GENERATION captured at the pick (the | ||
| * write's start), and these pin why that ordering point is the correct one: | ||
| * | ||
| * - a read already IN FLIGHT at pick time (generation ≤ floor) can only carry | ||
| * the pre-write value, so it must NOT clear the pick; | ||
| * - a read issued AFTER the pick (generation > floor) — the row's own refresh | ||
| * OR a concurrent external write's read that lands mid-write — reports a | ||
| * newer authority and MUST clear the pick, even if the row's own explicit | ||
| * refresh never lands (the production-order regression). | ||
| * | ||
| * Also covered: convergence to this pick / an external write / the prior value | ||
| * restored (A→B→A), a refresh that lands nothing keeping the pick, and cancel. | ||
| */ | ||
|
|
||
| import assert from 'node:assert/strict'; | ||
| import { afterEach, test } from 'node:test'; | ||
| import { act } from 'react'; | ||
| import { createRoot } from 'react-dom/client'; | ||
| import { parseHTML } from 'linkedom'; | ||
| import { useOptimisticSelection, type OptimisticSelection } from '../use-optimistic-selection.js'; | ||
|
|
||
| const originalGlobals = { | ||
| document: globalThis.document, | ||
| window: globalThis.window, | ||
| Element: globalThis.Element, | ||
| HTMLElement: globalThis.HTMLElement, | ||
| Node: globalThis.Node, | ||
| }; | ||
| const originalActEnvironment = (globalThis as typeof globalThis & { | ||
| IS_REACT_ACT_ENVIRONMENT?: boolean; | ||
| }).IS_REACT_ACT_ENVIRONMENT; | ||
|
|
||
| let mountedRoot: ReturnType<typeof createRoot> | undefined; | ||
|
|
||
| afterEach(async () => { | ||
| if (mountedRoot) await act(() => mountedRoot?.unmount()); | ||
| mountedRoot = undefined; | ||
| Object.assign(globalThis, { | ||
| ...originalGlobals, | ||
| IS_REACT_ACT_ENVIRONMENT: originalActEnvironment, | ||
| }); | ||
| }); | ||
|
|
||
| interface Harness { | ||
| render(authoritative: string, committedGeneration: number): Promise<void>; | ||
| begin(next: string, floor: number): Promise<void>; | ||
| cancel(): Promise<void>; | ||
| value(): string | null; | ||
| } | ||
|
|
||
| async function mount(): Promise<Harness> { | ||
| const { document, window } = parseHTML('<main id="root"></main>'); | ||
| const root = document.querySelector<HTMLElement>('#root'); | ||
| assert.ok(root); | ||
| Object.assign(globalThis, { | ||
| document, | ||
| window, | ||
| Element: window.Element, | ||
| HTMLElement: window.HTMLElement, | ||
| Node: window.Node, | ||
| IS_REACT_ACT_ENVIRONMENT: true, | ||
| }); | ||
|
|
||
| const api: { current: OptimisticSelection | null } = { current: null }; | ||
| function Probe({ authoritative, committedGeneration }: { authoritative: string; committedGeneration: number }) { | ||
| const selection = useOptimisticSelection(authoritative, committedGeneration); | ||
| api.current = selection; | ||
| return <span data-value={selection.value} />; | ||
| } | ||
|
|
||
| mountedRoot = createRoot(root); | ||
| const el = () => root.querySelector('span'); | ||
| return { | ||
| async render(authoritative, committedGeneration) { | ||
| await act(() => | ||
| mountedRoot?.render(<Probe authoritative={authoritative} committedGeneration={committedGeneration} />), | ||
| ); | ||
| }, | ||
| async begin(next, floor) { | ||
| await act(() => api.current?.begin(next, floor)); | ||
| }, | ||
| async cancel() { | ||
| await act(() => api.current?.cancel()); | ||
| }, | ||
| value: () => el()?.getAttribute('data-value') ?? null, | ||
| }; | ||
| } | ||
|
|
||
| test('a pick shows immediately', async () => { | ||
| const h = await mount(); | ||
| await h.render('A', 5); | ||
| assert.equal(h.value(), 'A'); | ||
|
|
||
| // Picked at floor 5 (the read generation issued as of the pick). | ||
| await h.begin('B', 5); | ||
| assert.equal(h.value(), 'B'); | ||
| }); | ||
|
|
||
| test('an in-flight read from before the pick (generation ≤ floor) does not clear', async () => { | ||
| const h = await mount(); | ||
| await h.render('A', 5); | ||
| await h.begin('B', 5); | ||
| // A read that was already in flight at pick time commits the pre-write value | ||
| // at its own generation (≤ floor). It must not clear the pick. | ||
| await h.render('A', 5); | ||
| assert.equal(h.value(), 'B'); | ||
| }); | ||
|
|
||
| test("the row's own refresh (generation past the floor) clears to this pick", async () => { | ||
| const h = await mount(); | ||
| await h.render('A', 5); | ||
| await h.begin('B', 5); | ||
| await h.render('B', 6); | ||
| assert.equal(h.value(), 'B'); | ||
| }); | ||
|
|
||
| test('an after-the-pick read clears to authority even with no explicit refresh (ordering regression)', async () => { | ||
| const h = await mount(); | ||
| await h.render('A', 5); | ||
| await h.begin('B', 5); | ||
| // A concurrent external write's read, issued after the pick, lands mid-write | ||
| // reporting C. It is > floor, so it clears the pick and settles on C — WITHOUT | ||
| // the row's own refresh ever running. Under the old "arm the floor after the | ||
| // refresh resolves" ordering this read was absorbed into the floor and, if the | ||
| // refresh then failed, C was masked by B forever. | ||
| await h.render('C', 6); | ||
| assert.equal(h.value(), 'C'); | ||
| }); | ||
|
|
||
| test('A→B→A: authority restored to the pre-pick value still clears the pick', async () => { | ||
| const h = await mount(); | ||
| await h.render('A', 5); | ||
| await h.begin('B', 5); | ||
| await h.render('A', 6); | ||
| assert.equal(h.value(), 'A'); | ||
| }); | ||
|
|
||
| test('a refresh that lands no after-the-pick read keeps the pick (does not revert to stale)', async () => { | ||
| const h = await mount(); | ||
| await h.render('A', 5); | ||
| await h.begin('B', 5); | ||
| // No read newer than the floor ever commits (refresh failed/invalidated). The | ||
| // write persisted B, so B must remain shown. | ||
| await h.render('A', 5); | ||
| assert.equal(h.value(), 'B'); | ||
| }); | ||
|
|
||
| test('cancel rolls back to the authoritative value (write threw)', async () => { | ||
| const h = await mount(); | ||
| await h.render('A', 5); | ||
| await h.begin('B', 5); | ||
| assert.equal(h.value(), 'B'); | ||
|
|
||
| await h.cancel(); | ||
| assert.equal(h.value(), 'A'); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] The synchronous
targetRevisionfence below already covers the pre-unmount window, so this remount is no longer what keeps a superseded save from acting. Its remaining job is narrower: dropping a pending optimistic pick stranded in@maka/uiwhen a fenced save returns early and no read is ever accepted. That is worth keeping, but it is not written down, so the next reader will see two guards for one hazard and delete this one. One line of comment here naming what the remount still owns would prevent that.