Skip to content
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

fix(rrweb): Use unpatched requestAnimationFrame when possible #150

Merged
merged 2 commits into from
Jan 4, 2024
Merged
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
16 changes: 8 additions & 8 deletions packages/rrweb/src/record/observers/canvas/canvas-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
CanvasArg,
ImageBitmapDataURLWorkerResponse,
} from '@sentry-internal/rrweb-types';
import { isBlocked } from '../../../utils';
import { isBlocked, onRequestAnimationFrame } from '../../../utils';
import { CanvasContext } from '@sentry-internal/rrweb-types';
import initCanvas2DMutationObserver from './2d';
import initCanvasContextObserver from './canvas';
Expand Down Expand Up @@ -147,7 +147,7 @@
this.pendingCanvasMutations.set(target, []);
}

this.pendingCanvasMutations.get(target)!.push(mutation);

Check warning on line 150 in packages/rrweb/src/record/observers/canvas/canvas-manager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/observers/canvas/canvas-manager.ts#L150

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
};

private initCanvasFPSObserver(
Expand Down Expand Up @@ -227,7 +227,7 @@
lastSnapshotTime &&
timestamp - lastSnapshotTime < timeBetweenSnapshots
) {
rafId = requestAnimationFrame(takeCanvasSnapshots);
rafId = onRequestAnimationFrame(takeCanvasSnapshots);
return;
}
lastSnapshotTime = timestamp;
Expand Down Expand Up @@ -278,10 +278,10 @@
})();
});
});
rafId = requestAnimationFrame(takeCanvasSnapshots);
rafId = onRequestAnimationFrame(takeCanvasSnapshots);
};

rafId = requestAnimationFrame(takeCanvasSnapshots);
rafId = onRequestAnimationFrame(takeCanvasSnapshots);

this.resetObservers = () => {
canvasContextReset();
Expand Down Expand Up @@ -330,15 +330,15 @@
}

private startPendingCanvasMutationFlusher() {
requestAnimationFrame(() => this.flushPendingCanvasMutations());
onRequestAnimationFrame(() => this.flushPendingCanvasMutations());
}

private startRAFTimestamping() {
const setLatestRAFTimestamp = (timestamp: DOMHighResTimeStamp) => {
this.rafStamps.latestId = timestamp;
requestAnimationFrame(setLatestRAFTimestamp);
onRequestAnimationFrame(setLatestRAFTimestamp);
};
requestAnimationFrame(setLatestRAFTimestamp);
onRequestAnimationFrame(setLatestRAFTimestamp);
}

flushPendingCanvasMutations() {
Expand All @@ -348,7 +348,7 @@
this.flushPendingCanvasMutationFor(canvas, id);
},
);
requestAnimationFrame(() => this.flushPendingCanvasMutations());
onRequestAnimationFrame(() => this.flushPendingCanvasMutations());
}

flushPendingCanvasMutationFor(canvas: HTMLCanvasElement, id: number) {
Expand All @@ -360,7 +360,7 @@
if (!valuesWithType || id === -1) return;

const values = valuesWithType.map((value) => {
const { type, ...rest } = value;

Check warning on line 363 in packages/rrweb/src/record/observers/canvas/canvas-manager.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/observers/canvas/canvas-manager.ts#L363

[@typescript-eslint/no-unused-vars] 'type' is assigned a value but never used.
return rest;
});
const { type } = valuesWithType[0];
Expand Down
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/processed-node-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { onRequestAnimationFrame } from '../utils';
import type MutationBuffer from './mutation';

/**
Expand All @@ -13,7 +14,7 @@ export default class ProcessedNodeManager {
}

private periodicallyClear() {
requestAnimationFrame(() => {
onRequestAnimationFrame(() => {
this.clear();
if (this.loop) this.periodicallyClear();
});
Expand Down
7 changes: 4 additions & 3 deletions packages/rrweb/src/replay/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
EventType,
IncrementalSource,
} from '@sentry-internal/rrweb-types';
import { onRequestAnimationFrame } from '../utils';

export class Timer {
public timeOffset = 0;
Expand Down Expand Up @@ -39,14 +40,14 @@ export class Timer {
this.actions.splice(index, 0, action);
}
if (rafWasActive) {
this.raf = requestAnimationFrame(this.rafCheck.bind(this));
this.raf = onRequestAnimationFrame(this.rafCheck.bind(this));
}
}

public start() {
this.timeOffset = 0;
this.lastTimestamp = performance.now();
this.raf = requestAnimationFrame(this.rafCheck.bind(this));
this.raf = onRequestAnimationFrame(this.rafCheck.bind(this));
}

private rafCheck() {
Expand All @@ -64,7 +65,7 @@ export class Timer {
}
}
if (this.actions.length > 0) {
this.raf = requestAnimationFrame(this.rafCheck.bind(this));
this.raf = onRequestAnimationFrame(this.rafCheck.bind(this));
} else {
this.raf = true; // was active
}
Expand Down
43 changes: 43 additions & 0 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'now you can use replayer.getMirror() to access the mirror instance of a replayer,' +
'\r\n' +
'or you can use record.mirror to access the mirror instance during recording.';
/** @deprecated */

Check warning on line 37 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L37

[tsdoc/syntax] tsdoc-missing-deprecation-message: The @deprecated block must include a deprecation message, e.g. describing the recommended alternative
export let _mirror: DeprecatedMirror = {
map: {},
getId() {
Expand Down Expand Up @@ -118,7 +118,7 @@
set(value) {
// put hooked setter into event loop to avoid of set latency
setTimeout(() => {
d.set!.call(this, value);

Check warning on line 121 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L121

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}, 0);
if (original && original.set) {
original.set.call(this, value);
Expand All @@ -131,7 +131,7 @@

// copy from https://github.com/getsentry/sentry-javascript/blob/b2109071975af8bf0316d3b5b38f519bdaf5dc15/packages/utils/src/object.ts
export function patch(
source: { [key: string]: any },

Check warning on line 134 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L134

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
name: string,
replacement: (...args: unknown[]) => unknown,
): () => void {
Expand Down Expand Up @@ -589,3 +589,46 @@
if (!doc) return false;
return doc.contains(n) || shadowHostInDom(n);
}

let cachedRequestAnimationFrameImplementation:
| undefined
| typeof requestAnimationFrame;

/**
* We generally want to use window.requestAnimationFrame.
* However, in some cases this may be wrapped (e.g. by Zone.js for Angular),
* so we try to get an unpatched version of this from a sandboxed iframe.
*/
function getRequestAnimationFrameImplementation(): typeof requestAnimationFrame {
if (cachedRequestAnimationFrameImplementation) {
return cachedRequestAnimationFrameImplementation;
}

const document = window.document;
let requestAnimationFrameImplementation = window.requestAnimationFrame;
if (document && typeof document.createElement === 'function') {
try {
const sandbox = document.createElement('iframe');
sandbox.hidden = true;
document.head.appendChild(sandbox);
const contentWindow = sandbox.contentWindow;
if (contentWindow && contentWindow.requestAnimationFrame) {
requestAnimationFrameImplementation =
// eslint-disable-next-line @typescript-eslint/unbound-method
contentWindow.requestAnimationFrame;
}
document.head.removeChild(sandbox);
} catch (e) {
// Could not create sandbox iframe, just use window.requestAnimationFrame
}
}

return (cachedRequestAnimationFrameImplementation =
requestAnimationFrameImplementation.bind(window));
}

export function onRequestAnimationFrame(
...rest: Parameters<typeof requestAnimationFrame>
): ReturnType<typeof requestAnimationFrame> {
return getRequestAnimationFrameImplementation()(...rest);
}
Loading