Skip to content

Commit

Permalink
fix: assert ReanimatedModule is loaded before initializing UI runtime (
Browse files Browse the repository at this point in the history
…#6835)

## Summary

`initializeUIRuntime` gets called in top level, therefore the JS engine
may decide to execute it before `ReanimatedModule` is resolved, which is
generally bad. This fixes that.

## Test plan

See `CounterExample` before and after.
  • Loading branch information
tjzel authored Dec 20, 2024
1 parent 1e9cdd2 commit 7e40230
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/react-native-reanimated/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function unregisterSensor(sensorId: number): void {
}

if (!isWeb()) {
initializeUIRuntime();
initializeUIRuntime(ReanimatedModule);
}

type FeaturesConfig = {
Expand Down
9 changes: 8 additions & 1 deletion packages/react-native-reanimated/src/initializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
registerLoggerConfig,
replaceLoggerImplementation,
} from './logger';
import type { IReanimatedModule } from './commonTypes';

const IS_JEST = isJest();
const SHOULD_BE_USE_WEB = shouldBeUseWeb();
Expand Down Expand Up @@ -178,7 +179,13 @@ function setupRequestAnimationFrame() {
};
}

export function initializeUIRuntime() {
export function initializeUIRuntime(ReanimatedModule: IReanimatedModule) {
if (!ReanimatedModule) {
// eslint-disable-next-line reanimated/use-reanimated-error
throw new Error(
'[Reanimated] Reanimated is trying to initialize the UI runtime without a valid ReanimatedModule'
);
}
if (IS_JEST) {
// requestAnimationFrame react-native jest's setup is incorrect as it polyfills
// the method directly using setTimeout, therefore the callback doesn't get the
Expand Down

0 comments on commit 7e40230

Please sign in to comment.