Skip to content

Commit

Permalink
Discovered that the common case of mouse movement or scrolling happen…
Browse files Browse the repository at this point in the history
…ing during `takeFullSnapshot` was causing mutations to be immediately emitted, contrary to the goal of rrweb-io#385
  • Loading branch information
eoghanmurray committed Jan 29, 2021
1 parent 1d4d538 commit d84664b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ function record<T = eventWithTime>(
) {
// we've got a user initiated event so first we need to apply
// all DOM changes that have been buffering during paused state
mutationBuffer.emit();
mutationBuffer.unfreeze();
}

Expand Down Expand Up @@ -187,8 +186,7 @@ function record<T = eventWithTime>(
isCheckout,
);

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(
tdoc, {
blockClass,
Expand Down Expand Up @@ -228,10 +226,7 @@ function record<T = eventWithTime>(
},
}),
);
if (!wasFrozen) {
mutationBuffer.emit(); // emit anything queued up now
mutationBuffer.unfreeze();
}
mutationBuffer.unlock(); // generate & emit any mutations that happened during snapshotting, as can now apply against the newly built mirror
}

try {
Expand Down
19 changes: 16 additions & 3 deletions src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,33 @@ export default class MutationBuffer {

public unfreeze() {
this.frozen = false;
this.emit();
}

public isFrozen() {
return this.frozen;
}

public lock() {
this.locked = true;
}

public unlock() {
this.locked = false;
this.emit();
}

public processMutations = (mutations: mutationRecord[]) => {
mutations.forEach(this.processMutation);
if (!this.frozen) {
this.emit();
}
this.emit();
};

public emit = () => {

if (this.frozen || this.locked) {
return;
}

// 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

Expand Down
2 changes: 2 additions & 0 deletions typings/record/mutation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default class MutationBuffer {
freeze(): void;
unfreeze(): void;
isFrozen(): boolean;
lock(): void;
unlock(): void;
processMutations: (mutations: mutationRecord[]) => void;
emit: () => void;
private processMutation;
Expand Down

0 comments on commit d84664b

Please sign in to comment.