fix: Ignore old requests in useSearch hook #1862
Merged
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.
Previously, search-as-you-type could result in a scenario where the results displayed didn't match the user's search. This occurred because of a bug in the
pending.current
logic that delayed subsequent searches and calledfetchMore()
after the pending query completed. The issue is thatfetchMore()
runs in the closure of the previous search, so it actually just repeated the old search instead of performing a new search with new variables.This PR addresses this issue by removing the
pending.current
check, firing all requests when they come in. When requests complete, they are ignored if they are not the most-recently triggered. Urql doesn't support AbortController, so we can't cancel the in-progress requests. If necessary, we can increase the debounce delay to reduce load.The infinite scroll controller tends to trigger multiple duplicate requests, so these are also being filtered out before sending using similar logic.