Skip to content

Commit

Permalink
reorder addList on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellanoce committed Jan 29, 2024
1 parent c3a8d22 commit 18d8ec8
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DoubleLinkedList {
public length = 0;
public head: DoubleLinkedListNode | null = null;
public tail: DoubleLinkedListNode | null = null;
public reordered = false;

public get(position: number) {
if (position >= this.length) {
Expand Down Expand Up @@ -129,6 +130,34 @@ class DoubleLinkedList {
}
this.length--;
}

public needsReorder(_node: DoubleLinkedListNode) {
if (!this.reordered &&
_node.value.previousSibling &&
isNodeInLinkedList(_node.value.previousSibling) &&
_node.previous &&
_node.previous.value !== _node.value.previousSibling) {
return true;
}
return false;
}

public reorder() {
if (this.reordered) return false;
let current = this.tail;
const head = this.head;
while (current) {
const prev = current.previous;
this.removeNode(current.value);
this.addNode(current.value);
if (current === head) {
break;
}
current = prev;
}
this.reordered = true;
return true;
}
}

const moveKey = (id: number, parentId: number) => `${id}@${parentId}`;
Expand Down Expand Up @@ -391,9 +420,13 @@ export default class MutationBuffer {
const parentId = this.mirror.getId(_node.value.parentNode);
const nextId = getNextId(_node.value);

if (nextId === -1) continue;
// nextId !== -1 && parentId !== -1
else if (parentId !== -1) {
if (nextId === -1) {
if (addList.needsReorder(_node) && addList.reorder()) {
tailNode = addList.tail;
}
continue;
} else if (parentId !== -1) {
// nextId !== -1 && parentId !== -1
node = _node;
break;
}
Expand Down

0 comments on commit 18d8ec8

Please sign in to comment.