Skip to content

Implement configurable throttling on mutation emission #1694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/friendly-rockets-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"rrweb": patch
"@rrweb/types": patch
---

Implement configurable throttling on mutation emission using a new `sampling.mutation` setting
4 changes: 3 additions & 1 deletion docs/recipes/optimize-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Some common patterns may emit lots of events are:

## Sampling

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

**Scenario 1**

Expand All @@ -35,6 +35,8 @@ rrweb.record({
mouseInteraction: false
// set the interval of scrolling event
scroll: 150 // do not emit twice in 150ms
// set the interval of mutation events (page changes)
mutation: 50 // do not emit more than 20 mutations per second
// set the interval of media interaction event
media: 800
// set the timing of record input
Expand Down
7 changes: 7 additions & 0 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
inDom,
getShadowHost,
closestElementOfNode,
throttle,
} from '../utils';
import dom from '@rrweb/utils';

Expand Down Expand Up @@ -184,6 +185,7 @@ export default class MutationBuffer {
private recordCanvas: observerParam['recordCanvas'];
private inlineImages: observerParam['inlineImages'];
private slimDOMOptions: observerParam['slimDOMOptions'];
private sampling: observerParam['sampling'];
private dataURLOptions: observerParam['dataURLOptions'];
private doc: observerParam['doc'];
private mirror: observerParam['mirror'];
Expand All @@ -210,6 +212,7 @@ export default class MutationBuffer {
'recordCanvas',
'inlineImages',
'slimDOMOptions',
'sampling',
'dataURLOptions',
'doc',
'mirror',
Expand All @@ -223,6 +226,10 @@ export default class MutationBuffer {
// just a type trick, the runtime result is correct
this[key] = options[key] as never;
});

if (this.sampling.mutation) {
this.emit = throttle(this.emit, this.sampling.mutation);
}
}

public freeze() {
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export type MutationBufferParam = Pick<
| 'recordCanvas'
| 'inlineImages'
| 'slimDOMOptions'
| 'sampling'
| 'dataURLOptions'
| 'doc'
| 'mirror'
Expand Down
8 changes: 7 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,15 @@ export type SamplingStrategy = Partial<{
*/
mouseInteraction: boolean | Record<string, boolean | undefined>;
/**
* number is the throttle threshold of recording scroll
* number is the throttle threshold of recording scroll in ms
* default: 100
*/
scroll: number;
/**
* number is the throttle threshold of mutation emission in ms
* off by default
*/
mutation: number;
/**
* number is the throttle threshold of recording media interactions
*/
Expand Down
Loading