From 9d7a75de7df3a610a8695acb661777018aa99ed3 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] Add missingNodeRetryMap Use a global missing node retry map to handle missing node that has not been resolved in the same round. --- src/replay/index.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/replay/index.ts b/src/replay/index.ts index 73774d03..477308a3 100644 --- a/src/replay/index.ts +++ b/src/replay/index.ts @@ -47,6 +47,8 @@ export class Replayer { private timer: Timer; + private missingNodeRetryMap: missingNodeMap = {}; + constructor(events: eventWithTime[], config?: Partial) { if (events.length < 2) { throw new Error('Replayer need at least 2 events.'); @@ -197,6 +199,9 @@ export class Replayer { castFn(); } this.lastPlayedEvent = event; + if (event === this.events[this.events.length - 1]) { + this.emitter.emit('finish'); + } }; return wrappedCastFn; } @@ -204,6 +209,13 @@ export class Replayer { private rebuildFullSnapshot( event: fullSnapshotEvent & { timestamp: number }, ) { + if (Object.keys(this.missingNodeRetryMap).length) { + console.warn( + 'Found unresolved missing node map', + this.missingNodeRetryMap, + ); + } + this.missingNodeRetryMap = {}; mirror.map = rebuild(event.data.node, this.iframe.contentDocument!)[1]; const styleEl = document.createElement('style'); const { documentElement, head } = this.iframe.contentDocument!; @@ -273,7 +285,7 @@ export class Replayer { } }); - const missingNodeMap: missingNodeMap = {}; + const missingNodeMap: missingNodeMap = { ...this.missingNodeRetryMap }; d.adds.forEach(mutation => { const target = buildNodeWithSN( mutation.node, @@ -316,7 +328,7 @@ export class Replayer { } }); if (Object.keys(missingNodeMap).length) { - console.warn('Found unresolved missing node map', missingNodeMap); + Object.assign(this.missingNodeRetryMap, missingNodeMap); } d.texts.forEach(mutation => { @@ -459,6 +471,7 @@ export class Replayer { parent.insertBefore(node, target.nextSibling); } delete map[mutation.node.id]; + delete this.missingNodeRetryMap[mutation.node.id]; if (mutation.previousId || mutation.nextId) { this.resolveMissingNode(map, parent, node as Node, mutation); }