diff --git a/src/record/index.ts b/src/record/index.ts index fdaad963f7..62e3adf85b 100644 --- a/src/record/index.ts +++ b/src/record/index.ts @@ -147,7 +147,6 @@ function record( ) { // 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(); } @@ -182,7 +181,7 @@ function record( ); 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, { blockClass, blockSelector, @@ -221,10 +220,7 @@ function record( }, }), ); - 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 { diff --git a/src/record/mutation.ts b/src/record/mutation.ts index 3689c1b847..b4bbf27110 100644 --- a/src/record/mutation.ts +++ b/src/record/mutation.ts @@ -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 diff --git a/typings/record/mutation.d.ts b/typings/record/mutation.d.ts index 78b3590106..72938fb193 100644 --- a/typings/record/mutation.d.ts +++ b/typings/record/mutation.d.ts @@ -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;