Skip to content

Commit

Permalink
feat(canvas): Bind mutationCb outside of getCanvasManager (#141)
Browse files Browse the repository at this point in the history
From what I can tell, `record` ends up getting bundled separate from
`getCanvasManager` in user-land code and canvas mutation events end up
not being transmitted because `_wrappedEmit` does not get set and it
will hold the wrong `mirror` reference.
  • Loading branch information
billyvg committed Apr 26, 2024
1 parent f1da1c4 commit cb2a3d0
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,17 @@ function record<T = eventWithTime>(

const processedNodeManager = new ProcessedNodeManager();

const canvasManager: CanvasManagerInterface = getCanvasManager
? getCanvasManager({
recordCanvas,
blockClass,
blockSelector,
unblockSelector,
sampling: sampling['canvas'],
dataURLOptions,
})
: new CanvasManagerNoop();
const canvasManager: CanvasManagerInterface = _getCanvasManager(
getCanvasManager,
{
recordCanvas,
blockClass,
blockSelector,
unblockSelector,
sampling: sampling['canvas'],
dataURLOptions,
},
);

const shadowDomManager: ShadowDomManagerInterface =
typeof __RRWEB_EXCLUDE_SHADOW_DOM__ === 'boolean' &&
Expand Down Expand Up @@ -693,27 +694,43 @@ record.takeFullSnapshot = takeFullSnapshot;

export default record;

const wrappedCanvasMutationEmit = (p: canvasMutationParam) =>
wrappedEmit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.CanvasMutation,
...p,
},
}),
);
type PrivateOptions = 'mutationCb' | 'win' | 'mirror';
type PublicGetCanvasManagerOptions = Omit<
CanvasManagerConstructorOptions,
PrivateOptions
>;

interface PrivateGetCanvasManagerOptions
extends PublicGetCanvasManagerOptions,
Pick<CanvasManagerConstructorOptions, PrivateOptions> {}

function _getCanvasManager(
getCanvasManagerFn:
| undefined
| ((options: PrivateGetCanvasManagerOptions) => CanvasManagerInterface),
options: PublicGetCanvasManagerOptions,
) {
return getCanvasManagerFn
? getCanvasManagerFn({
...options,
mirror,
win: window,
mutationCb: (p: canvasMutationParam) =>
wrappedEmit(
wrapEvent({
type: EventType.IncrementalSnapshot,
data: {
source: IncrementalSource.CanvasMutation,
...p,
},
}),
),
})
: new CanvasManagerNoop();
}

export function getCanvasManager(
options: Omit<
CanvasManagerConstructorOptions,
'mutationCb' | 'win' | 'mirror'
>,
options: PublicGetCanvasManagerOptions,
): CanvasManagerInterface {
return new CanvasManager({
...options,
mutationCb: wrappedCanvasMutationEmit,
win: window,
mirror,
});
return new CanvasManager(options as CanvasManagerConstructorOptions);
}

0 comments on commit cb2a3d0

Please sign in to comment.