Skip to content

Commit

Permalink
Refactor to preclude the need for a continuous raf loop (rrweb-io#1328)
Browse files Browse the repository at this point in the history
* Refactor to preclude the need for a continuous raf loop

* Apply formatting changes

* Create shadow-dom-unbusify.md

---------

Co-authored-by: eoghanmurray <[email protected]>
  • Loading branch information
2 people authored and billyvg committed Sep 17, 2024
1 parent 3ef3618 commit 4ea2382
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/shadow-dom-unbusify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

Refactor to preclude the need for a continuous raf loop running in the background which is related to shadowDom
27 changes: 9 additions & 18 deletions packages/rrweb/src/record/processed-node-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,8 @@ import type MutationBuffer from './mutation';
*/
export default class ProcessedNodeManager {
private nodeMap: WeakMap<Node, Set<MutationBuffer>> = new WeakMap();
// Whether to continue RAF loop.
private loop = true;

constructor() {
this.periodicallyClear();
}

private periodicallyClear() {
requestAnimationFrame(() => {
this.clear();
if (this.loop) this.periodicallyClear();
});
}
private active = false;

public inOtherBuffer(node: Node, thisBuffer: MutationBuffer) {
const buffers = this.nodeMap.get(node);
Expand All @@ -27,15 +16,17 @@ export default class ProcessedNodeManager {
}

public add(node: Node, buffer: MutationBuffer) {
if (!this.active) {
this.active = true;
requestAnimationFrame(() => {
this.nodeMap = new WeakMap();
this.active = false;
});
}
this.nodeMap.set(node, (this.nodeMap.get(node) || new Set()).add(buffer));
}

private clear() {
this.nodeMap = new WeakMap();
}

public destroy() {
// Stop the RAF loop.
this.loop = false;
// cleanup no longer needed
}
}

0 comments on commit 4ea2382

Please sign in to comment.