An open source design system that's fully customizable and agent ready
TypeScript
fix(typeahead): claim a generation per search so stale responses are discarded (#3588)
Md Shahrier Islam Arhame18ed0f0577a
diff --git a/.changeset/typeahead-stale-results.md b/.changeset/typeahead-stale-results.mdnew file mode 100644index 0000000000..a119a011e9--- /dev/null+++ b/.changeset/typeahead-stale-results.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Typeahead/Tokenizer discard out-of-order async search results (#3587)++Each search now claims a new generation, so a slow response for an abandoned query can no longer overwrite the results of the current one (previously the last response to resolve always won, and Enter could select an item from a query the user had already replaced).+@arham766diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsxindex b663b2056a..982bda1df1 100644--- a/packages/core/src/Typeahead/BaseTypeahead.tsx+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx@@ -410,7 +410,10 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({const performSearch = useCallback(async (searchQuery: string) => {searchSource.cancel?.();- const gen = searchGenRef.current;+ // Claim a new generation so overlapping searches can't race: an+ // in-flight response for an older query fails the gen check below+ // instead of overwriting the newer results.+ const gen = ++searchGenRef.current;setIsLoading(true);setHasSearched(true);try {@@ -451,7 +454,7 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({// Perform bootstrapconst performBootstrap = useCallback(async () => {- const gen = searchGenRef.current;+ const gen = ++searchGenRef.current;setIsLoading(true);try {const bootstrapResults = await searchSource.bootstrap();diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsxindex 61e07494ed..7ae83c678b 100644--- a/packages/core/src/Typeahead/Typeahead.test.tsx+++ b/packages/core/src/Typeahead/Typeahead.test.tsx@@ -18,7 +18,7 @@ import {afterAll,beforeEach,} from 'vitest';-import {render, screen, fireEvent, waitFor} from '@testing-library/react';+import {render, screen, fireEvent, waitFor, act} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {Typeahead} from './Typeahead';import {BaseTypeahead} from './BaseTypeahead';@@ -314,6 +314,50 @@ describe('BaseTypeahead focus-out', () => {});describe('Typeahead', () => {+ describe('out-of-order async results', () => {+ it('discards a stale response that resolves after a newer query', async () => {+ const resolvers = new Map<string, (items: SearchableItem[]) => void>();+ const rawSource: SearchSource = {+ search: async (query: string) =>+ new Promise<SearchableItem[]>(resolve => {+ resolvers.set(query, resolve);+ }),+ bootstrap: () => [],+ };++ render(+ <Typeahead+ label="Fruit"+ searchSource={rawSource}+ value={null}+ onChange={() => {}}+ debounceMs={0}+ />,+ );++ const input = screen.getByRole('combobox');+ fireEvent.change(input, {target: {value: 'a'}});+ fireEvent.change(input, {target: {value: 'ap'}});++ // The newer query resolves first…+ await act(async () => {+ resolvers.get('ap')!([{id: 'apple', label: 'Apple'}]);+ });+ expect(screen.getByText('Apple')).toBeInTheDocument();++ // …then the abandoned query's slow response arrives and must be+ // discarded rather than overwriting the current results.+ await act(async () => {+ resolvers.get('a')!([+ {id: 'avocado', label: 'Avocado'},+ {id: 'apricot', label: 'Apricot'},+ ]);+ });+ expect(screen.getByText('Apple')).toBeInTheDocument();+ expect(screen.queryByText('Avocado')).not.toBeInTheDocument();+ });+ });+it('renders with label', () => {render(<Typeahead