Skip to content

Commit 5acec54

Browse files
committed
Implement configurable throttling on mutation emission using the new sampling.mutation ms setting
- can prevent overly frequent (javascript based) animation per second on the same attribute; the mutation buffer will emit a single change at the end if throttled changes are against the same element See upstream rrweb-io#1694
1 parent f9ce482 commit 5acec54

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

docs/recipes/optimize-storage.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Some common patterns may emit lots of events are:
2121

2222
## Sampling
2323

24-
Use the sampling config in the recording can reduce the storage size by dropping some events:
24+
Use the sampling config in the recording can reduce the storage size by dropping or merging some events:
2525

2626
**Scenario 1**
2727

@@ -35,6 +35,8 @@ rrweb.record({
3535
mouseInteraction: false
3636
// set the interval of scrolling event
3737
scroll: 150 // do not emit twice in 150ms
38+
// set the interval of mutation events (page changes)
39+
mutation: 50 // do not emit more than 20 mutations per second
3840
// set the interval of media interaction event
3941
media: 800
4042
// set the timing of record input

packages/rrweb/src/record/mutation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
inDom,
3232
getShadowHost,
3333
closestElementOfNode,
34+
throttle,
3435
} from '../utils';
3536
import dom from '@deepprediction/rrweb-utils';
3637

@@ -184,6 +185,7 @@ export default class MutationBuffer {
184185
private recordCanvas: observerParam['recordCanvas'];
185186
private inlineImages: observerParam['inlineImages'];
186187
private slimDOMOptions: observerParam['slimDOMOptions'];
188+
private sampling: observerParam['sampling'];
187189
private dataURLOptions: observerParam['dataURLOptions'];
188190
private doc: observerParam['doc'];
189191
private mirror: observerParam['mirror'];
@@ -210,6 +212,7 @@ export default class MutationBuffer {
210212
'recordCanvas',
211213
'inlineImages',
212214
'slimDOMOptions',
215+
'sampling',
213216
'dataURLOptions',
214217
'doc',
215218
'mirror',
@@ -223,6 +226,10 @@ export default class MutationBuffer {
223226
// just a type trick, the runtime result is correct
224227
this[key] = options[key] as never;
225228
});
229+
230+
if (this.sampling.mutation) {
231+
this.emit = throttle(this.emit, this.sampling.mutation);
232+
}
226233
}
227234

228235
public freeze() {

packages/rrweb/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export type MutationBufferParam = Pick<
143143
| 'recordCanvas'
144144
| 'inlineImages'
145145
| 'slimDOMOptions'
146+
| 'sampling'
146147
| 'dataURLOptions'
147148
| 'doc'
148149
| 'mirror'

packages/types/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,15 @@ export type SamplingStrategy = Partial<{
201201
*/
202202
mouseInteraction: boolean | Record<string, boolean | undefined>;
203203
/**
204-
* number is the throttle threshold of recording scroll
204+
* number is the throttle threshold of recording scroll in ms
205+
* default: 100
205206
*/
206207
scroll: number;
208+
/**
209+
* number is the throttle threshold of mutation emission in ms
210+
* off by default
211+
*/
212+
mutation: number;
207213
/**
208214
* number is the throttle threshold of recording media interactions
209215
*/

0 commit comments

Comments
 (0)