Skip to content

Commit 2fa46f3

Browse files
authored
feat: Remove getCanvasManager, export CanvasManager class directly (#153)
This simplifies the code a bit by exporting the CanvasManager directly. With [ReplayCanvas](getsentry/sentry-javascript#10112), we can rely on it for complex setup, but keeps it simple for our users.
1 parent 6390071 commit 2fa46f3

File tree

7 files changed

+35
-57
lines changed

7 files changed

+35
-57
lines changed

.size-limit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ module.exports = [
77
gzip: true
88
},
99
{
10-
name: 'rrweb - record & getCanvasManager only (gzipped)',
10+
name: 'rrweb - record & CanvasManager only (gzipped)',
1111
path: 'packages/rrweb/es/rrweb/packages/rrweb/src/entries/all.js',
12-
import: '{ record, getCanvasManager }',
12+
import: '{ record, CanvasManager }',
1313
gzip: true
1414
},
1515
{

packages/rrweb/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export { record, Replayer, utils, canvasMutation };
2727
export { deserializeArg } from './replay/canvas/deserialize-args';
2828

2929
export {
30+
CanvasManager,
3031
takeFullSnapshot,
3132
mirror,
3233
freezePage,
3334
addCustomEvent,
34-
getCanvasManager,
3535
} from './record';
36+
export type { CanvasManagerConstructorOptions } from './record';

packages/rrweb/src/record/index.ts

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
MaskInputOptions,
44
SlimDOMOptions,
55
createMirror,
6-
DataURLOptions,
76
} from '@sentry-internal/rrweb-snapshot';
87
import { initObservers, mutationBuffers } from './observer';
98
import {
@@ -53,6 +52,7 @@ import {
5352
registerErrorHandler,
5453
unregisterErrorHandler,
5554
} from './error-handler';
55+
export type { CanvasManagerConstructorOptions } from './observers/canvas/canvas-manager';
5656

5757
function wrapEvent(e: event): eventWithTime {
5858
const eWithTime = e as eventWithTime;
@@ -340,6 +340,18 @@ function record<T = eventWithTime>(
340340
const canvasManager: CanvasManagerInterface = _getCanvasManager(
341341
getCanvasManager,
342342
{
343+
mirror,
344+
win: window,
345+
mutationCb: (p: canvasMutationParam) =>
346+
wrappedEmit(
347+
wrapEvent({
348+
type: EventType.IncrementalSnapshot,
349+
data: {
350+
source: IncrementalSource.CanvasMutation,
351+
...p,
352+
},
353+
}),
354+
),
343355
recordCanvas,
344356
blockClass,
345357
blockSelector,
@@ -717,14 +729,6 @@ export function takeFullSnapshot(isCheckout?: boolean) {
717729
_takeFullSnapshot(isCheckout);
718730
}
719731

720-
function wrappedEmit(e: eventWithTime) {
721-
if (!_wrappedEmit) {
722-
return;
723-
}
724-
725-
_wrappedEmit(e);
726-
}
727-
728732
// record.addCustomEvent is removed because Sentry Session Replay does not use it
729733
// record.freezePage is removed because Sentry Session Replay does not use it
730734

@@ -734,48 +738,22 @@ record.takeFullSnapshot = takeFullSnapshot;
734738

735739
export default record;
736740

737-
type PrivateOptions = 'mutationCb' | 'win' | 'mirror';
738-
type PublicGetCanvasManagerOptions = Omit<
739-
CanvasManagerConstructorOptions,
740-
PrivateOptions
741-
>;
742-
743-
interface PrivateGetCanvasManagerOptions
744-
extends PublicGetCanvasManagerOptions,
745-
Pick<CanvasManagerConstructorOptions, PrivateOptions> {}
746-
747741
function _getCanvasManager(
748742
getCanvasManagerFn:
749743
| undefined
750-
| ((options: PrivateGetCanvasManagerOptions) => CanvasManagerInterface),
751-
options: PublicGetCanvasManagerOptions,
744+
| ((
745+
options: Partial<CanvasManagerConstructorOptions>,
746+
) => CanvasManagerInterface),
747+
options: CanvasManagerConstructorOptions,
752748
) {
753749
try {
754750
return getCanvasManagerFn
755-
? getCanvasManagerFn({
756-
...options,
757-
mirror,
758-
win: window,
759-
mutationCb: (p: canvasMutationParam) =>
760-
wrappedEmit(
761-
wrapEvent({
762-
type: EventType.IncrementalSnapshot,
763-
data: {
764-
source: IncrementalSource.CanvasMutation,
765-
...p,
766-
},
767-
}),
768-
),
769-
})
751+
? getCanvasManagerFn(options)
770752
: new CanvasManagerNoop();
771753
} catch {
772754
console.warn('Unable to initialize CanvasManager');
773755
return new CanvasManagerNoop();
774756
}
775757
}
776758

777-
export function getCanvasManager(
778-
options: PublicGetCanvasManagerOptions,
779-
): CanvasManagerInterface {
780-
return new CanvasManager(options as CanvasManagerConstructorOptions);
781-
}
759+
export { CanvasManager };

packages/rrweb/src/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ export type recordOptions<T> = {
8484
errorHandler?: ErrorHandler;
8585
onMutation?: (mutations: MutationRecord[]) => boolean;
8686
getCanvasManager?: (
87-
options: Omit<
88-
CanvasManagerConstructorOptions,
89-
'mutationCb' | 'win' | 'mirror'
90-
>,
87+
options: CanvasManagerConstructorOptions,
9188
) => CanvasManagerInterface;
9289
};
9390

packages/rrweb/test/record/cross-origin-iframes.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from '../utils';
1818
import { unpack } from '../../src/packer/unpack';
1919
import type * as http from 'http';
20-
import type { CanvasManagerInterface } from '../../src/record/observers/canvas/canvas-manager';
20+
import type { CanvasManager } from '../../src/record/observers/canvas/canvas-manager';
2121

2222
interface ISuite {
2323
code: string;
@@ -35,7 +35,7 @@ interface IWindow extends Window {
3535
) => listenerHandler | undefined;
3636
addCustomEvent<T>(tag: string, payload: T): void;
3737
pack: (e: eventWithTime) => string;
38-
getCanvasManager: () => CanvasManagerInterface;
38+
CanvasManager: typeof CanvasManager;
3939
};
4040
emit: (e: eventWithTime) => undefined;
4141
snapshots: eventWithTime[];
@@ -54,12 +54,12 @@ async function injectRecordScript(
5454
options = options || {};
5555
await frame.evaluate((options) => {
5656
(window as unknown as IWindow).snapshots = [];
57-
const { record, pack, getCanvasManager } = (window as unknown as IWindow)
57+
const { record, pack, CanvasManager } = (window as unknown as IWindow)
5858
.rrweb;
5959
const config: recordOptions<eventWithTime> = {
6060
recordCrossOriginIframes: true,
6161
recordCanvas: true,
62-
getCanvasManager,
62+
getCanvasManager: (options) => new CanvasManager(options),
6363
emit(event) {
6464
(window as unknown as IWindow).snapshots.push(event);
6565
(window as unknown as IWindow).emit(event);

packages/rrweb/test/record/webgl.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
waitForRAF,
1717
} from '../utils';
1818
import type { ICanvas } from '@sentry-internal/rrweb-snapshot';
19-
import type { CanvasManagerInterface } from '../../src/record/observers/canvas/canvas-manager';
19+
import type { CanvasManager } from '../../src/record/observers/canvas/canvas-manager';
2020

2121
interface ISuite {
2222
code: string;
@@ -31,7 +31,7 @@ interface IWindow extends Window {
3131
options: recordOptions<eventWithTime>,
3232
) => listenerHandler | undefined;
3333
addCustomEvent<T>(tag: string, payload: T): void;
34-
getCanvasManager: () => CanvasManagerInterface;
34+
CanvasManager: typeof CanvasManager;
3535
};
3636
emit: (e: eventWithTime) => undefined;
3737
}
@@ -66,10 +66,10 @@ const setup = function (
6666
ctx.page.on('console', (msg) => console.log('PAGE LOG:', msg.text()));
6767

6868
await ctx.page.evaluate((canvasSample) => {
69-
const { record, getCanvasManager } = (window as unknown as IWindow).rrweb;
69+
const { record, CanvasManager } = (window as unknown as IWindow).rrweb;
7070
record({
7171
recordCanvas: true,
72-
getCanvasManager,
72+
getCanvasManager: (options) => new CanvasManager(options),
7373
sampling: {
7474
canvas: canvasSample,
7575
},

packages/rrweb/test/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,9 @@ export function generateRecordSnippet(options: recordOptions<eventWithTime>) {
712712
inlineImages: ${options.inlineImages},
713713
plugins: ${options.plugins},
714714
getCanvasManager: ${
715-
options.recordCanvas ? 'rrweb.getCanvasManager' : 'undefined'
715+
options.recordCanvas
716+
? '(opts) => new rrweb.CanvasManager(opts)'
717+
: 'undefined'
716718
}
717719
});
718720
`;

0 commit comments

Comments
 (0)