Skip to content

Commit

Permalink
ref: Avoid async
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Dec 19, 2023
1 parent f040256 commit 9ef2944
Showing 1 changed file with 43 additions and 39 deletions.
82 changes: 43 additions & 39 deletions packages/rrweb/src/record/observers/canvas/canvas-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,46 +229,50 @@ export class CanvasManager implements CanvasManagerInterface {
}
lastSnapshotTime = timestamp;

getCanvas()
// eslint-disable-next-line @typescript-eslint/no-misused-promises
.forEach(async (canvas: HTMLCanvasElement) => {
const id = this.mirror.getId(canvas);
if (snapshotInProgressMap.get(id)) return;
snapshotInProgressMap.set(id, true);
if (['webgl', 'webgl2'].includes((canvas as ICanvas).__context)) {
// if the canvas hasn't been modified recently,
// its contents won't be in memory and `createImageBitmap`
// will return a transparent imageBitmap

const context = canvas.getContext((canvas as ICanvas).__context) as
| WebGLRenderingContext
| WebGL2RenderingContext
| null;
if (
context?.getContextAttributes()?.preserveDrawingBuffer === false
) {
// Hack to load canvas back into memory so `createImageBitmap` can grab it's contents.
// Context: https://twitter.com/Juice10/status/1499775271758704643
// Preferably we set `preserveDrawingBuffer` to true, but that's not always possible,
// especially when canvas is loaded before rrweb.
// This hack can wipe the background color of the canvas in the (unlikely) event that
// the canvas background was changed but clear was not called directly afterwards.
// Example of this hack having negative side effect: https://visgl.github.io/react-map-gl/examples/layers
context.clear(context.COLOR_BUFFER_BIT);
}
getCanvas().forEach((canvas: HTMLCanvasElement) => {
const id = this.mirror.getId(canvas);
if (snapshotInProgressMap.get(id)) return;
snapshotInProgressMap.set(id, true);
if (['webgl', 'webgl2'].includes((canvas as ICanvas).__context)) {
// if the canvas hasn't been modified recently,
// its contents won't be in memory and `createImageBitmap`
// will return a transparent imageBitmap

const context = canvas.getContext((canvas as ICanvas).__context) as
| WebGLRenderingContext
| WebGL2RenderingContext
| null;
if (
context?.getContextAttributes()?.preserveDrawingBuffer === false
) {
// Hack to load canvas back into memory so `createImageBitmap` can grab it's contents.
// Context: https://twitter.com/Juice10/status/1499775271758704643
// Preferably we set `preserveDrawingBuffer` to true, but that's not always possible,
// especially when canvas is loaded before rrweb.
// This hack can wipe the background color of the canvas in the (unlikely) event that
// the canvas background was changed but clear was not called directly afterwards.
// Example of this hack having negative side effect: https://visgl.github.io/react-map-gl/examples/layers
context.clear(context.COLOR_BUFFER_BIT);
}
const bitmap = await createImageBitmap(canvas);
worker.postMessage(
{
id,
bitmap,
width: canvas.width,
height: canvas.height,
dataURLOptions: options.dataURLOptions,
},
[bitmap],
);
});
}

createImageBitmap(canvas)
.then((bitmap) => {
worker.postMessage(
{
id,
bitmap,
width: canvas.width,
height: canvas.height,
dataURLOptions: options.dataURLOptions,
},
[bitmap],
);
})
.catch(() => {
// noop
});
});
rafId = requestAnimationFrame(takeCanvasSnapshots);
};

Expand Down

0 comments on commit 9ef2944

Please sign in to comment.