Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: virtual nodes are passed to plugin's onBuild function
  • Loading branch information
YunFeng0817 committed Feb 1, 2023
commit fb6db8cc254d2c0ff05d918297fa4be00c058ff6
21 changes: 15 additions & 6 deletions packages/rrdom/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export type ReplayerHandler = {
data: styleDeclarationData | styleSheetRuleData,
styleSheet: CSSStyleSheet,
) => void;
// Similar to the `afterAppend` callback in the `rrweb-snapshot` package. It's a postorder traversal of the newly appended nodes.
afterAppend?(node: Node, id: number): void;
};

/**
Expand All @@ -109,6 +111,7 @@ export function diff(
);
oldTree.parentNode?.replaceChild(calibratedOldTree, oldTree);
oldTree = calibratedOldTree;
replayer.afterAppend?.(oldTree, replayer.mirror.getId(oldTree));
}

let inputDataToApply = null,
Expand All @@ -128,6 +131,7 @@ export function diff(
(oldTree as Document).close();
(oldTree as Document).open();
replayer.mirror.add(oldTree, newMeta);
replayer.afterAppend?.(oldTree, replayer.mirror.getId(oldTree));
}
}
break;
Expand Down Expand Up @@ -303,7 +307,7 @@ function diffChildren(
newStartNode = newChildren[newStartIndex],
newEndNode = newChildren[newEndIndex];
let oldIdToIndex: Record<number, number> | undefined = undefined,
indexInOld;
indexInOld: number | undefined = undefined;
while (oldStartIndex <= oldEndIndex && newStartIndex <= newEndIndex) {
if (oldStartNode === undefined) {
oldStartNode = oldChildren[++oldStartIndex];
Expand Down Expand Up @@ -359,9 +363,12 @@ function diffChildren(
}
}
indexInOld = oldIdToIndex[rrnodeMirror.getId(newStartNode)];
if (indexInOld) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const nodeToMove = oldChildren[indexInOld]!;
const nodeToMove = oldChildren[indexInOld];
if (
indexInOld !== undefined &&
nodeToMove &&
nodeMatching(nodeToMove, newStartNode, replayer.mirror, rrnodeMirror)
) {
try {
parentNode.insertBefore(nodeToMove, oldStartNode);
} catch (e) {
Expand Down Expand Up @@ -399,10 +406,11 @@ function diffChildren(

try {
parentNode.insertBefore(newNode, oldStartNode || null);
diff(newNode, newStartNode, replayer, rrnodeMirror);
replayer.afterAppend?.(newNode, replayer.mirror.getId(newNode));
} catch (e) {
console.warn(e);
}
diff(newNode, newStartNode, replayer, rrnodeMirror);
}
newStartNode = newChildren[++newStartIndex];
}
Expand All @@ -422,10 +430,11 @@ function diffChildren(
);
try {
parentNode.insertBefore(newNode, referenceNode);
diff(newNode, newChildren[newStartIndex], replayer, rrnodeMirror);
replayer.afterAppend?.(newNode, replayer.mirror.getId(newNode));
} catch (e) {
console.warn(e);
}
diff(newNode, newChildren[newStartIndex], replayer, rrnodeMirror);
}
} else if (newStartIndex > newEndIndex) {
for (; oldStartIndex <= oldEndIndex; oldStartIndex++) {
Expand Down
10 changes: 9 additions & 1 deletion packages/rrdom/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,15 @@ describe('diff algorithm for rrdom', () => {
} as serializedNodeWithId;
mirror.add(unreliableChild, unreliableSN);
parentNode.appendChild(unreliableChild);
mirror.add(document.createElement('div'), { ...unreliableSN, id: 2 });
createTree(
{
tagName: 'div',
id: 2,
children: [],
},
undefined,
mirror,
);

const rrParentNode = createTree(
{
Expand Down
9 changes: 9 additions & 0 deletions packages/rrweb/src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ export class Replayer {
else if (data.source === IncrementalSource.StyleDeclaration)
this.applyStyleDeclaration(data, styleSheet);
},
afterAppend: (node: Node, id: number) => {
for (const plugin of this.config.plugins || []) {
if (plugin.onBuild) plugin.onBuild(node, { id, replayer: this });
}
},
};
if (this.iframe.contentDocument)
try {
Expand Down Expand Up @@ -863,6 +868,8 @@ export class Replayer {
);
}

// Skip the plugin onBuild callback in the virtual dom mode
if (this.usingVirtualDom) return;
for (const plugin of this.config.plugins || []) {
if (plugin.onBuild)
plugin.onBuild(builtNode, {
Expand Down Expand Up @@ -1480,6 +1487,8 @@ export class Replayer {
return;
}
const afterAppend = (node: Node | RRNode, id: number) => {
// Skip the plugin onBuild callback for virtual dom
if (this.usingVirtualDom) return;
for (const plugin of this.config.plugins || []) {
if (plugin.onBuild) plugin.onBuild(node, { id, replayer: this });
}
Expand Down