Refactor to preclude the need for a continuous raf loop (#1328)

* Refactor to preclude the need for a continuous raf loop

* Apply formatting changes

* Create shadow-dom-unbusify.md

---------

Co-authored-by: eoghanmurray <eoghanmurray@users.noreply.github.com>
This commit is contained in:
Eoghan Murray
2026-04-01 12:00:00 +08:00
committed by GitHub
parent 91bdd6e2c8
commit 2633dea9c0
2 changed files with 14 additions and 18 deletions

View File

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

View File

@@ -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);
@@ -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
}
}