From ad2ac811a34463ab6ebccde7fe27edad1dd76eb5 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] serialize newly added nodes and update id node map when childList changed --- src/record/observer.ts | 5 +++-- src/types.ts | 1 + src/utils.ts | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/record/observer.ts b/src/record/observer.ts index 02f64028..8c0dc3d9 100644 --- a/src/record/observer.ts +++ b/src/record/observer.ts @@ -1,4 +1,4 @@ -import { INode } from 'rrweb-snapshot'; +import { INode, serializeNodeWithId } from 'rrweb-snapshot'; import { mirror, throttle, on } from '../utils'; import { mutationCallBack, @@ -69,8 +69,8 @@ function initMutationObserver(cb: mutationCallBack): MutationObserver { parentId: id, id: mirror.getId(n as INode), }); + mirror.removeNodeFromMap(n as INode); }); - // TODO: init new nodes to be INode addedNodes.forEach(n => { adds.push({ parentId: id, @@ -82,6 +82,7 @@ function initMutationObserver(cb: mutationCallBack): MutationObserver { : mirror.getId(nextSibling as INode), id: mirror.getId(n as INode), }); + serializeNodeWithId(n as INode, document, mirror.map); }); break; } diff --git a/src/types.ts b/src/types.ts index 86da426c..f2b4f816 100644 --- a/src/types.ts +++ b/src/types.ts @@ -171,6 +171,7 @@ export type Mirror = { map: idNodeMap; getId: (n: INode) => number; getNode: (id: number) => INode; + removeNodeFromMap: (n: INode) => void; }; export type throttleOptions = { diff --git a/src/utils.ts b/src/utils.ts index fa8262b9..1aa235bc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -17,6 +17,10 @@ export const mirror: Mirror = { getNode(id) { return mirror.map[id]; }, + removeNodeFromMap(n) { + const id = n.__sn && n.__sn.id; + delete mirror.map[id]; + }, }; // copy from underscore