Skip to content

Commit

Permalink
Add cache for formatted hits
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Roes committed Oct 16, 2021
1 parent 22be128 commit 97c4984
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/plugins/discover/public/application/helpers/format_hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ import { formatFieldValue } from './format_value';

// TODO: Test coverage
// TODO: documentation
// TODO: Build cache

const formattedHitCache = new WeakMap<estypes.SearchHit, FormattedHit>();

type FormattedHit = Array<[string, JSX.Element]>;

export function formatHit(
hit: estypes.SearchHit,
dataView: DataView,
fieldsToShow: string[]
): Array<[string, JSX.Element]> {
): FormattedHit {
const cached = formattedHitCache.get(hit);
if (cached) {
return cached;
}

const highlights = hit.highlight ?? {};
// Keys are sorted in the hits object
const flattened = flattenHit(hit, dataView, { includeIgnoredValues: true, source: true });
Expand All @@ -40,5 +48,7 @@ export function formatHit(
}
});
const maxEntries = getServices().uiSettings.get<number>(MAX_DOC_FIELDS_DISPLAYED);
return [...highlightPairs, ...sourcePairs].slice(0, maxEntries);
const formatted = [...highlightPairs, ...sourcePairs].slice(0, maxEntries);
formattedHitCache.set(hit, formatted);
return formatted;
}

0 comments on commit 97c4984

Please sign in to comment.