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: move requestAnimationFrame back to Reanimated #6991

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
IReanimatedModule,
ReanimatedModuleProxy,
} from './reanimatedModuleProxy';
import { setupRequestAnimationFrame } from '../requestAnimationFrame';

const IS_WEB = shouldBeUseWeb();

Expand Down Expand Up @@ -81,7 +82,11 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
checkCppVersion();
}
this.#reanimatedModuleProxy = global.__reanimatedModuleProxy;
executeOnUIRuntimeSync(registerReanimatedError);
tjzel marked this conversation as resolved.
Show resolved Hide resolved
executeOnUIRuntimeSync(function initializeUI() {
'worklet';
registerReanimatedError();
setupRequestAnimationFrame();
})();
}

registerSensor(
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native-reanimated/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { createCustomError, registerCustomError } from './WorkletsResolver';

export const ReanimatedError = createCustomError('Reanimated');

const ReanimatedErrorConstructor = ReanimatedError;
tjzel marked this conversation as resolved.
Show resolved Hide resolved

export function registerReanimatedError() {
'worklet';
registerCustomError(ReanimatedError, 'Reanimated');
registerCustomError(ReanimatedErrorConstructor, 'Reanimated');
}
41 changes: 41 additions & 0 deletions packages/react-native-reanimated/src/requestAnimationFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

import { callMicrotasks } from "./WorkletsResolver";

export function setupRequestAnimationFrame() {
'worklet';

// Jest mocks requestAnimationFrame API and it does not like if that mock gets overridden
// so we avoid doing requestAnimationFrame batching in Jest environment.
const nativeRequestAnimationFrame = global.requestAnimationFrame;

let animationFrameCallbacks: Array<(timestamp: number) => void> = [];
let flushRequested = false;

global.__flushAnimationFrame = (frameTimestamp: number) => {
const currentCallbacks = animationFrameCallbacks;
animationFrameCallbacks = [];
currentCallbacks.forEach((f) => f(frameTimestamp));
callMicrotasks();
};

global.requestAnimationFrame = (
callback: (timestamp: number) => void
): number => {
animationFrameCallbacks.push(callback);
if (!flushRequested) {
flushRequested = true;
nativeRequestAnimationFrame((timestamp) => {
flushRequested = false;
global.__frameTimestamp = timestamp;
global.__flushAnimationFrame(timestamp);
global.__frameTimestamp = undefined;
});
}
// Reanimated currently does not support cancelling callbacks requested with
// requestAnimationFrame. We return -1 as identifier which isn't in line
// with the spec but it should give users better clue in case they actually
// attempt to store the value returned from rAF and use it for cancelling.
return -1;
};
}
46 changes: 1 addition & 45 deletions packages/react-native-worklets/src/worklets/initializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ import {
isWeb,
shouldBeUseWeb,
} from './PlatformChecker';
import {
callMicrotasks,
executeOnUIRuntimeSync,
runOnJS,
setupMicrotasks,
} from './threads';
import { executeOnUIRuntimeSync, runOnJS, setupMicrotasks } from './threads';
import { registerWorkletsError, WorkletsError } from './WorkletsError';
import type { IWorkletsModule } from './WorkletsModule';

Expand Down Expand Up @@ -148,44 +143,6 @@ export function setupConsole() {
}
}

function setupRequestAnimationFrame() {
'worklet';

// Jest mocks requestAnimationFrame API and it does not like if that mock gets overridden
// so we avoid doing requestAnimationFrame batching in Jest environment.
const nativeRequestAnimationFrame = global.requestAnimationFrame;

let animationFrameCallbacks: Array<(timestamp: number) => void> = [];
let flushRequested = false;

global.__flushAnimationFrame = (frameTimestamp: number) => {
const currentCallbacks = animationFrameCallbacks;
animationFrameCallbacks = [];
currentCallbacks.forEach((f) => f(frameTimestamp));
callMicrotasks();
};

global.requestAnimationFrame = (
callback: (timestamp: number) => void
): number => {
animationFrameCallbacks.push(callback);
if (!flushRequested) {
flushRequested = true;
nativeRequestAnimationFrame((timestamp) => {
flushRequested = false;
global.__frameTimestamp = timestamp;
global.__flushAnimationFrame(timestamp);
global.__frameTimestamp = undefined;
});
}
// Reanimated currently does not support cancelling callbacks requested with
// requestAnimationFrame. We return -1 as identifier which isn't in line
// with the spec but it should give users better clue in case they actually
// attempt to store the value returned from rAF and use it for cancelling.
return -1;
};
}

export function initializeUIRuntime(WorkletsModule: IWorkletsModule) {
if (isWeb()) {
return;
Expand All @@ -211,7 +168,6 @@ export function initializeUIRuntime(WorkletsModule: IWorkletsModule) {
setupCallGuard();
setupConsole();
setupMicrotasks();
setupRequestAnimationFrame();
})();
}
}
Loading