Skip to content

Commit

Permalink
✨ feat: add possibility to update search index from within the update…
Browse files Browse the repository at this point in the history
…Hits hook
  • Loading branch information
niketpathak committed Nov 18, 2024
1 parent 0b76c74 commit 5f7a191
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
13 changes: 9 additions & 4 deletions demo/assets/js/testable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,10 +1071,15 @@ const test20A = typeahead({
hooks: {
updateHits: async (resultSet, loader) => {
loader();
const response = await fetch(`https://restcountries.com/v2/name/${resultSet.query}`);
const text = await response.text();
resultSet.hits = text && JSON.parse(text);
loader(false);
try {
const response = await fetch(`https://restcountries.com/v2/name/${resultSet.query}`);
const text = await response.text();
resultSet.hits = text && JSON.parse(text);
} catch (e) {
console.error('typeahead-20A failed request', e);
} finally {
loader(false);
}

return resultSet;
},
Expand Down
4 changes: 2 additions & 2 deletions src/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ export interface typeaheadConfig<T extends Dictionary> {
/**
* The updateHits hook allows you to modify/filter/sort the search results before being rendered
* @param hits The found results
* @returns A promise containing nothing/void or with the ResultSet
* @returns A promise containing nothing/void or with the ResultSet. You can set the "updateSearchIndex" flag to true if you wish for the returned items to be added to the search index. By default, the search index is not updated
*/
updateHits: (
resultSet: Pick<ResultSet<T>, 'hits' | 'query' | 'count'>,
loader: (visible: boolean) => void
) => Promise<void> | Promise<Pick<ResultSet<T>, 'hits' | 'count'>>;
) => Promise<void> | Promise<Pick<ResultSet<T>, 'hits'> & { count?: number; updateSearchIndex?: boolean }>;
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/typeahead-standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
* Responsible for drawing/updating the view
*/
const update = async (): Promise<void> => {
// hook to update Hits before displaying results from tree
// hook to update Hits before displaying results from search index/trie
const results_mod = await hooks.updateHits(
{
hits: resultSet.hits,
Expand All @@ -329,7 +329,8 @@ const typeahead = <T extends Dictionary>(config: typeaheadConfig<T>): typeaheadR
);
if (results_mod?.hits?.length) {
resultSet.hits = results_mod.hits;
resultSet.count = results_mod.count ?? results_mod.hits.length;
results_mod.count && (resultSet.count = results_mod.count);
results_mod.updateSearchIndex && addToIndex(results_mod.hits);
}

// No Matches
Expand Down

0 comments on commit 5f7a191

Please sign in to comment.