Skip to content

Commit 5aa6437

Browse files
authored
chore: Refactor adding extra nodes to snapshot (#1951)
Get all info about the TextSnapshot first, and only then modify it. Previously these steps were interleaved.
1 parent 80bee1e commit 5aa6437

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

src/TextSnapshot.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,12 @@ export class TextSnapshot {
231231
};
232232

233233
const findDescendantNodes = async (
234-
backendNodeId: number,
234+
backendNodeId?: number,
235235
): Promise<Set<number>> => {
236236
const descendantIds = new Set<number>();
237+
if (!backendNodeId) {
238+
return descendantIds;
239+
}
237240
try {
238241
// @ts-expect-error internal API
239242
const client = page.pptrPage._client();
@@ -297,20 +300,26 @@ export class TextSnapshot {
297300
if (extraHandles.length) {
298301
page.extraHandles = extraHandles;
299302
}
303+
const reorgInfo: Array<{
304+
extraNode: TextSnapshotNode;
305+
attachTarget: TextSnapshotNode;
306+
descendantIds: Set<number>;
307+
}> = [];
308+
300309
for (const handle of page.extraHandles) {
301310
const extraNode = await createExtraNode(handle);
302311
if (!extraNode) {
303312
continue;
304313
}
305314
idToNode.set(extraNode.id, extraNode);
306315
const attachTarget = (await findAncestorNode(handle)) || rootNodeWithId;
307-
if (extraNode.backendNodeId !== undefined) {
308-
const descendantIds = await findDescendantNodes(
309-
extraNode.backendNodeId,
310-
);
311-
const index = moveChildNodes(attachTarget, extraNode, descendantIds);
312-
attachTarget.children.splice(index, 0, extraNode);
313-
}
316+
const descendantIds = await findDescendantNodes(extraNode.backendNodeId);
317+
reorgInfo.push({extraNode, attachTarget, descendantIds});
318+
}
319+
320+
for (const {extraNode, attachTarget, descendantIds} of reorgInfo) {
321+
const index = moveChildNodes(attachTarget, extraNode, descendantIds);
322+
attachTarget.children.splice(index, 0, extraNode);
314323
}
315324
}
316325
}

0 commit comments

Comments
 (0)