authorgravatar for git@paperclover.netclover caruso <git@paperclover.net> 2026-03-19 23:30:39-07:00
committergravatar for git@paperclover.netclover caruso <git@paperclover.net> 2026-03-20 01:17:10-07:00
log7cbbaedd32edafc10c7c0b128461dc123771c5e0
tree2be45fe9ecf34ae60fc250116b5f77731b3db5eb
parent9aff7edf050088cee3bc2d69cc5a6c4f4d512704
signaturebadge-check Signed by SSH key SHA256:xbd+BjjhyBfwk7GVoURf9Yx0gzDerHbvYv7SddNWmAs

fix: don't need to call `memo()` on every component


1 files changed, 4 insertions(+), 11 deletions(-)

src/Memoizer.ts+4-11
......@@ -1,5 +1,5 @@
11import { ASSERT, UNWRAP } from "@clo/lib/assert.ts";
2import { type JSX, type Key, memo, type ReactNode } from "react";
2import { type JSX, type Key, type ReactNode } from "react";
33import { type Components } from "rehype-react";
44import remarkRehype from "remark-rehype";
55import { Fragment, jsx } from "react/jsx-runtime";
......@@ -29,10 +29,9 @@ type AstProcessor = Processor<Parent, Parent, Parent, Parent, Parent>;
2929 */
3030export class Memoizer {
3131 // options incrementally updated via `reconfigure`
32 #baseProcessor: BaseProcessor = defaultProcessor as BaseProcessor;
32 #baseProcessor: BaseProcessor = defaultProcessor;
3333 #astProcessor: AstProcessor | null = null;
3434 #components: Partial<Components> = {};
35 #previousComponents: Partial<Components> = {};
3635
3736 // incremental parsing graph, structure of arrays
3837 #predict: Predict | null = null;
......@@ -50,7 +49,7 @@ export class Memoizer {
5049 if (
5150 this.#astProcessor !== null &&
5251 this.#baseProcessor === processor &&
53 componentsAreEqual(this.#previousComponents, components) &&
52 componentsAreEqual(this.#components, components) &&
5453 !!this.#predict === predict
5554 ) {
5655 return;
......@@ -64,13 +63,7 @@ export class Memoizer {
6463 }
6564
6665 this.#astProcessor = processor as unknown as AstProcessor;
67 // Memoize every component. We want to ensure that React doesn't re-render
68 // components for no reason. Just because the parent renders doesn't mean
69 // the children should.
70 this.#components = Object.fromEntries(
71 Object.entries(components).map(([k, v]) => [k, typeof v === "function" ? memo(v) : v]),
72 ) as Partial<Components>;
73 this.#previousComponents = components;
66 this.#components = components;
7467 this.#content = "";
7568 this.#positions = [];
7669 this.#parsed = [];