The best way to control Claude Code, Codex and more from any computer or phone
perf(web): composer serializes each draft doc once per keystroke (#26720)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Hieu Le committed
Nov 1, 2026 at 22:01 UTC
0da2bb625ca899320fc224a0736830211027ddb8
2 files changed
+20
-6
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+2
-2
@@ -1018,7 +1018,7 @@ function ComposerPromptEditorTiptapInner(
1018
1019
const handleEditorChange = (updated: TiptapEditor) => {
1020
const map = serializeEditorDoc(updated.state.doc);
1021
- const contextIds = literalText ? [] : map.contextIds;
1021
+ const contextIds = literalText ? [] : [...map.contextIds];
1022
const { from, to } = updated.state.selection;
1023
const fromFlat = pmToFlat(map, from);
1024
const toFlat = pmToFlat(map, to);
@@ -1473,7 +1473,7 @@ function ComposerPromptEditorTiptapInner(
1473
value: map.value,
1474
cursor: collapseExpandedComposerCursor(map.value, expandedCursor, literalText),
1475
expandedCursor,
1476
- contextIds: literalText ? [] : map.contextIds,
1476
+ contextIds: literalText ? [] : [...map.contextIds],
1477
};
1478
const toFlat = pmToFlat(map, to);
1479
selectionRangeRef.current = {
apps/web/src/composer-rich-text-doc.ts
+18
-4
@@ -879,11 +879,12 @@ export interface RichRun {
879
nodeName?: string;
880
}
881
882
+// Shared by every caller that serializes the same doc, so it must not be mutated.
883
export interface RichDocMap {
883
- value: string;
884
- runs: RichRun[];
885
- docLength: number;
886
- contextIds: string[];
884
+ readonly value: string;
885
+ readonly runs: ReadonlyArray<Readonly<RichRun>>;
886
+ readonly docLength: number;
887
+ readonly contextIds: ReadonlyArray<string>;
888
}
889
890
function readAtomSource(node: ProseMirrorNode): string {
@@ -1354,7 +1355,20 @@ export function serializeSelection(doc: ProseMirrorNode, from: number, to: numbe
1355
return serializeEditorDoc(doc.type.create(null, content)).value;
1356
}
1357
1358
+// Docs are immutable, and one keystroke serializes the same doc from the marker
1359
+// plugin, onUpdate, and onSelectionUpdate.
1360
+const serializedDocs = new WeakMap<ProseMirrorNode, RichDocMap>();
1361
+
1362
export function serializeEditorDoc(doc: ProseMirrorNode): RichDocMap {
1363
+ let map = serializedDocs.get(doc);
1364
+ if (!map) {
1365
+ map = serializeDoc(doc);
1366
+ serializedDocs.set(doc, map);
1367
+ }
1368
+ return map;
1369
+}
1370
+
1371
+function serializeDoc(doc: ProseMirrorNode): RichDocMap {
1372
const acc: RichAccumulator = { runs: [], value: "", flat: 0, md: 0 };
1373
const blocks: ProseMirrorNode[] = [];
1374
doc.content.forEach((node) => {