Discovered that the common case of mouse movement or scrolling happening during takeFullSnapshot was causing mutations to be immediately emitted, contrary to the goal of https://github.com/rrweb-io/rrweb/pull/385 (#470)

This commit is contained in:
Eoghan Murray
2021-01-31 13:43:36 +00:00
committed by GitHub
parent 13f1a61743
commit 9187bec814
3 changed files with 20 additions and 9 deletions

View File

@@ -147,7 +147,6 @@ function record<T = eventWithTime>(
) { ) {
// we've got a user initiated event so first we need to apply // we've got a user initiated event so first we need to apply
// all DOM changes that have been buffering during paused state // all DOM changes that have been buffering during paused state
mutationBuffer.emit();
mutationBuffer.unfreeze(); mutationBuffer.unfreeze();
} }
@@ -182,7 +181,7 @@ function record<T = eventWithTime>(
); );
let wasFrozen = mutationBuffer.isFrozen(); let wasFrozen = mutationBuffer.isFrozen();
mutationBuffer.freeze(); // don't allow any mirror modifications during snapshotting mutationBuffer.lock(); // don't allow any mirror modifications during snapshotting
const [node, idNodeMap] = snapshot(document, { const [node, idNodeMap] = snapshot(document, {
blockClass, blockClass,
blockSelector, blockSelector,
@@ -221,10 +220,7 @@ function record<T = eventWithTime>(
}, },
}), }),
); );
if (!wasFrozen) { mutationBuffer.unlock(); // generate & emit any mutations that happened during snapshotting, as can now apply against the newly built mirror
mutationBuffer.emit(); // emit anything queued up now
mutationBuffer.unfreeze();
}
} }
try { try {

View File

@@ -173,20 +173,33 @@ export default class MutationBuffer {
public unfreeze() { public unfreeze() {
this.frozen = false; this.frozen = false;
this.emit();
} }
public isFrozen() { public isFrozen() {
return this.frozen; return this.frozen;
} }
public lock() {
this.locked = true;
}
public unlock() {
this.locked = false;
this.emit();
}
public processMutations = (mutations: mutationRecord[]) => { public processMutations = (mutations: mutationRecord[]) => {
mutations.forEach(this.processMutation); mutations.forEach(this.processMutation);
if (!this.frozen) { this.emit();
this.emit();
}
}; };
public emit = () => { public emit = () => {
if (this.frozen || this.locked) {
return;
}
// delay any modification of the mirror until this function // delay any modification of the mirror until this function
// so that the mirror for takeFullSnapshot doesn't get mutated while it's event is being processed // so that the mirror for takeFullSnapshot doesn't get mutated while it's event is being processed

View File

@@ -21,6 +21,8 @@ export default class MutationBuffer {
freeze(): void; freeze(): void;
unfreeze(): void; unfreeze(): void;
isFrozen(): boolean; isFrozen(): boolean;
lock(): void;
unlock(): void;
processMutations: (mutations: mutationRecord[]) => void; processMutations: (mutations: mutationRecord[]) => void;
emit: () => void; emit: () => void;
private processMutation; private processMutation;