Skip to content

Commit 6cb8335

Browse files
committed
Refactor to preclude the need for a continuous raf loop
1 parent 729361e commit 6cb8335

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

packages/rrweb/src/record/processed-node-manager.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,8 @@ import type MutationBuffer from './mutation';
55
*/
66
export default class ProcessedNodeManager {
77
private nodeMap: WeakMap<Node, Set<MutationBuffer>> = new WeakMap();
8-
// Whether to continue RAF loop.
9-
private loop = true;
108

11-
constructor() {
12-
this.periodicallyClear();
13-
}
14-
15-
private periodicallyClear() {
16-
requestAnimationFrame(() => {
17-
this.clear();
18-
if (this.loop) this.periodicallyClear();
19-
});
20-
}
9+
private active = false;
2110

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

2918
public add(node: Node, buffer: MutationBuffer) {
19+
if (!this.active) {
20+
this.active = true;
21+
requestAnimationFrame(() => {
22+
this.nodeMap = new WeakMap();
23+
this.active = false;
24+
});
25+
}
3026
this.nodeMap.set(node, (this.nodeMap.get(node) || new Set()).add(buffer));
3127
}
3228

33-
private clear() {
34-
this.nodeMap = new WeakMap();
35-
}
36-
3729
public destroy() {
38-
// Stop the RAF loop.
39-
this.loop = false;
30+
// cleanup no longer needed
4031
}
4132
}

0 commit comments

Comments
 (0)