Skip to content

Commit e7e0bc7

Browse files
committed
feat: Export getCanvasManager & allow passing it to record()
1 parent 20d18b7 commit e7e0bc7

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

packages/rrweb/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import record from './record';
2+
23
import { Replayer } from './replay';
34
import * as utils from './utils';
45

@@ -20,4 +21,10 @@ export type { recordOptions } from './types';
2021

2122
export { record, Replayer, utils };
2223

23-
export { takeFullSnapshot, mirror, freezePage, addCustomEvent } from './record';
24+
export {
25+
takeFullSnapshot,
26+
mirror,
27+
freezePage,
28+
addCustomEvent,
29+
getCanvasManager,
30+
} from './record';

packages/rrweb/src/record/index.ts

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
MaskInputOptions,
44
SlimDOMOptions,
55
createMirror,
6+
DataURLOptions,
67
} from '@sentry-internal/rrweb-snapshot';
78
import { initObservers, mutationBuffers } from './observer';
89
import {
@@ -27,6 +28,8 @@ import {
2728
scrollCallback,
2829
canvasMutationParam,
2930
adoptedStyleSheetParam,
31+
SamplingStrategy,
32+
blockClass,
3033
} from '@sentry-internal/rrweb-types';
3134
import type { CrossOriginIframeMessageEventContent } from '../types';
3235
import {
@@ -115,6 +118,7 @@ function record<T = eventWithTime>(
115118
ignoreCSSAttributes = new Set([]),
116119
errorHandler,
117120
onMutation,
121+
canvasManager: customCanvasManager,
118122
} = options;
119123

120124
registerErrorHandler(errorHandler);
@@ -337,20 +341,18 @@ function record<T = eventWithTime>(
337341

338342
const processedNodeManager = new ProcessedNodeManager();
339343

340-
const canvasManager: CanvasManagerInterface =
341-
typeof __RRWEB_EXCLUDE_CANVAS__ === 'boolean' && __RRWEB_EXCLUDE_CANVAS__
342-
? new CanvasManagerNoop()
343-
: new CanvasManager({
344-
recordCanvas,
345-
mutationCb: wrappedCanvasMutationEmit,
346-
win: window,
347-
blockClass,
348-
blockSelector,
349-
unblockSelector,
350-
mirror,
351-
sampling: sampling.canvas,
352-
dataURLOptions,
353-
});
344+
const canvasManager: CanvasManagerInterface = customCanvasManager
345+
? customCanvasManager
346+
: typeof __RRWEB_EXCLUDE_CANVAS__ === 'boolean' && __RRWEB_EXCLUDE_CANVAS__
347+
? new CanvasManagerNoop()
348+
: getCanvasManager({
349+
recordCanvas,
350+
blockClass,
351+
blockSelector,
352+
unblockSelector,
353+
sampling,
354+
dataURLOptions,
355+
});
354356

355357
const shadowDomManager: ShadowDomManagerInterface =
356358
typeof __RRWEB_EXCLUDE_SHADOW_DOM__ === 'boolean' &&
@@ -721,6 +723,14 @@ export function takeFullSnapshot(isCheckout?: boolean) {
721723
_takeFullSnapshot(isCheckout);
722724
}
723725

726+
function wrappedEmit(e: eventWithTime) {
727+
if (!_wrappedEmit) {
728+
return;
729+
}
730+
731+
_wrappedEmit(e);
732+
}
733+
724734
// record.addCustomEvent is removed because Sentry Session Replay does not use it
725735
// record.freezePage is removed because Sentry Session Replay does not use it
726736

@@ -729,3 +739,31 @@ record.mirror = mirror;
729739
record.takeFullSnapshot = takeFullSnapshot;
730740

731741
export default record;
742+
743+
const wrappedCanvasMutationEmit = (p: canvasMutationParam) =>
744+
wrappedEmit(
745+
wrapEvent({
746+
type: EventType.IncrementalSnapshot,
747+
data: {
748+
source: IncrementalSource.CanvasMutation,
749+
...p,
750+
},
751+
}),
752+
);
753+
754+
export function getCanvasManager(options: {
755+
recordCanvas: boolean;
756+
blockClass: blockClass;
757+
blockSelector: string | null;
758+
unblockSelector: string | null;
759+
sampling: SamplingStrategy;
760+
dataURLOptions: DataURLOptions;
761+
}): CanvasManagerInterface {
762+
return new CanvasManager({
763+
...options,
764+
sampling: options.sampling.canvas,
765+
mutationCb: wrappedCanvasMutationEmit,
766+
win: window,
767+
mirror,
768+
});
769+
}

packages/rrweb/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export type recordOptions<T> = {
8989
keepIframeSrcFn?: KeepIframeSrcFn;
9090
errorHandler?: ErrorHandler;
9191
onMutation?: (mutations: MutationRecord[]) => boolean;
92+
canvasManager?: CanvasManagerInterface;
9293
};
9394

9495
export type observerParam = {

0 commit comments

Comments
 (0)