Skip to content
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

Use a debounce timer for content search request #544

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/services/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function useFilteredTranscripts({
const [searchService, setSearchService] = useState();
const [allSearchResults, setAllSearchResults] = useState(null);
const abortControllerRef = useRef(null);
const debounceTimerRef = useRef(0);

const { matcher, itemsWithIds, itemsIndexed } = useMemo(() => {
const itemsWithIds = (transcripts || []).map((item, idx) => (
Expand Down Expand Up @@ -168,18 +169,23 @@ export function useFilteredTranscripts({
}, [matcher, query, enabled, sorter, matchesOnly, showMarkers, playerDispatch, selectedTranscript]);

const callSearchFactory = () => {
clearTimeout(debounceTimerRef.current);

const abortController = new AbortController();
abortControllerRef.current = abortController;

(Promise.resolve(matcher(query, abortControllerRef.current))
.then(({ matchedTranscriptLines, hitCounts, allSearchHits }) => {
if (abortController.signal.aborted) return;
markMatchedItems(matchedTranscriptLines, hitCounts, allSearchHits);
})
.catch(e => {
console.error('search failed', e, query, transcripts);
})
);
// Wait 5 milliseconds before submitting the search request, to avoid unnecessary UI updates
debounceTimerRef.current = setTimeout(() => {
(Promise.resolve(matcher(query, abortControllerRef.current))
.then(({ matchedTranscriptLines, hitCounts, allSearchHits }) => {
if (abortController.signal.aborted) return;
markMatchedItems(matchedTranscriptLines, hitCounts, allSearchHits);
})
.catch(e => {
console.error('search failed', e, query, transcripts);
})
);
}, 5);
};
/**
* Generic function to prepare a list of search hits to be displayed in the transcript
Expand Down