Skip to content

Commit

Permalink
update latestResponseRef => responseToLatestRequestRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Nov 18, 2021
1 parent 4a4d600 commit b9e7a9f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions sample-app/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export default function SearchBar({
const isLoading = useAnswersState(state => state.searchStatus.isLoading);
const [
autocompleteResponse,
latestAutocompleteResponseRef,
responseToLatestRequestRef,
executeAutocomplete
] = useAutocomplete(isVertical);

async function executeQuery () {
await latestAutocompleteResponseRef.current;
await responseToLatestRequestRef.current;
if (autocompleteResponse?.inputIntents.includes(SearchIntent.NearMe)) {
const position = await SearchHandler.getUserLocation(geolocationOptions);
answersActions.setUserLocation({
Expand Down
12 changes: 6 additions & 6 deletions sample-app/src/hooks/useAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ export function useAutocomplete(
const answersActions = useAnswersActions();
const autocompleteNetworkIds = useRef({ latestRequest: 0, responseInState: 0 });
const [ autocompleteResponse, setAutocompleteResponse ] = useState<AutocompleteResponse>();
const latestAutocompleteResponseRef = useRef<Promise<void>>(Promise.resolve());
const responseToLatestRequestRef = useRef<Promise<void>>(Promise.resolve());
async function executeAutocomplete () {
const requestId = ++autocompleteNetworkIds.current.latestRequest;
latestAutocompleteResponseRef.current = new Promise(async (resolve) => {
responseToLatestRequestRef.current = new Promise(async (resolve) => {
const response = isVertical
? await answersActions.executeVerticalAutocomplete()
: await answersActions.executeUniversalAutocomplete();
if (requestId === autocompleteNetworkIds.current.latestRequest) {
resolve();
}
if (requestId >= autocompleteNetworkIds.current.responseInState) {
setAutocompleteResponse(response);
autocompleteNetworkIds.current.responseInState = requestId;
if (requestId === autocompleteNetworkIds.current.latestRequest) {
resolve();
}
}
});
}
return [ autocompleteResponse, latestAutocompleteResponseRef, executeAutocomplete ]
return [ autocompleteResponse, responseToLatestRequestRef, executeAutocomplete ]
};

0 comments on commit b9e7a9f

Please sign in to comment.