diff --git a/src/record/observer.ts b/src/record/observer.ts index f0b58de8..0a7de203 100644 --- a/src/record/observer.ts +++ b/src/record/observer.ts @@ -159,15 +159,15 @@ function initMutationObserver(cb: mutationCallBack): MutationObserver { id: mirror.getId(text.node as INode), value: text.value, })) - // text mutation without ID means the target node has been removed - .filter(text => text.id), + // text mutation's id was not in the mirror map means the target node has been removed + .filter(text => mirror.has(text.id)), attributes: attributes .map(attribute => ({ id: mirror.getId(attribute.node as INode), attributes: attribute.attributes, })) - // attribute mutation without ID means the target node has been removed - .filter(attribute => attribute.id), + // attribute mutation's id was not in the mirror map means the target node has been removed + .filter(attribute => mirror.has(attribute.id)), removes, adds, }); diff --git a/src/replay/index.ts b/src/replay/index.ts index ff057447..10bcd8ed 100644 --- a/src/replay/index.ts +++ b/src/replay/index.ts @@ -175,6 +175,20 @@ export class Replayer { private applyIncremental(d: incrementalData, isSync: boolean) { switch (d.source) { case IncrementalSource.Mutation: { + d.removes.forEach(mutation => { + const target = mirror.getNode(mutation.id); + if (!target) { + return; + } + const parent = (mirror.getNode( + mutation.parentId!, + ) as Node) as Element; + // target may be removed with its parents before + mirror.removeNodeFromMap(target); + if (parent) { + parent.removeChild(target); + } + }); d.adds.forEach(mutation => { const target = buildNodeWithSN( mutation.node, @@ -184,12 +198,10 @@ export class Replayer { ) as Node; const parent = (mirror.getNode(mutation.parentId) as Node) as Element; if (mutation.nextId) { - const next = (mirror.getNode(mutation.nextId) as Node) as Element; + const next = mirror.getNode(mutation.nextId) as Node; parent.insertBefore(target, next); } else if (mutation.previousId) { - const previous = (mirror.getNode( - mutation.previousId, - ) as Node) as Element; + const previous = mirror.getNode(mutation.previousId) as Node; parent.insertBefore(target, previous.nextSibling); } else { parent.appendChild(target); @@ -212,17 +224,6 @@ export class Replayer { } } }); - d.removes.forEach(mutation => { - const target = (mirror.getNode(mutation.id) as Node) as Element; - const parent = (mirror.getNode( - mutation.parentId!, - ) as Node) as Element; - // target may be removed with its parents before - if (parent) { - parent.removeChild(target); - } - delete mirror.map[mutation.id]; - }); break; } case IncrementalSource.MouseMove: