Skip to content

Commit

Permalink
refactor: cache rt based on facets
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Dec 22, 2023
1 parent 2933dda commit 392538d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions app/com/components/RichTextRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@ interface RichTextUiSegment {
to: Linking | undefined;
}

const cache = new WeakMap<Facet[], RichTextUiSegment[]>();

const RichTextRenderer = <T extends object>(props: RichTextRendererProps<T>) => {
const linking = useLinking();
const get = props.get;

const segments = createMemo(() => {
const ret = get(props.item);
const segments = segmentRichText(ret.t, ret.f);
const segments = createMemo((): RichTextUiSegment[] => {
const { t: text, f: facets } = get(props.item);

if (facets !== undefined) {
let rendered = cache.get(facets);
if (rendered === undefined) {
const segments = segmentRichText(text, facets);
cache.set(facets, (rendered = renderRichText(segments)));
}

return rendered;
}

return renderRichText(segments);
return [{ text: text, to: undefined }];
});

const navigateLink = (ev: MouseEvent | KeyboardEvent) => {
Expand Down

0 comments on commit 392538d

Please sign in to comment.