From fbf6a8c888cf05ad138dcbf5e4537d7c451e20de Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Sun, 30 Apr 2023 03:28:33 -0700 Subject: [PATCH] Ensure NativeAnimatedHelper cannot be setup incorrectly (#37157) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37157 ## Context NativeAnimatedHelper must be set up in a very particular way: - On iOS in Bridgeless mode, it must use NativeAnimated**Turbo**Module. - Everywhere else, it must use NativeAnimatedModule. ## Problem For some unknown reason, NativeAnimatedModule was getting loaded on D44933587. ## Changes This diff makes the aformentioned error impossible. It moves the module load gating logic into the two NativeModule spec files. Changelog: [Internal] Reviewed By: philIip Differential Revision: D45292907 fbshipit-source-id: 771bf64d7b491732e8860cf704bc9c3686980e74 --- .../Libraries/Animated/NativeAnimatedHelper.js | 4 +--- .../Libraries/Animated/NativeAnimatedModule.js | 6 +++++- .../Animated/NativeAnimatedTurboModule.js | 9 ++++++--- .../Animated/shouldUseTurboAnimatedModule.js | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 packages/react-native/Libraries/Animated/shouldUseTurboAnimatedModule.js diff --git a/packages/react-native/Libraries/Animated/NativeAnimatedHelper.js b/packages/react-native/Libraries/Animated/NativeAnimatedHelper.js index 5a6c508fd04..a96287de26d 100644 --- a/packages/react-native/Libraries/Animated/NativeAnimatedHelper.js +++ b/packages/react-native/Libraries/Animated/NativeAnimatedHelper.js @@ -28,9 +28,7 @@ import invariant from 'invariant'; // TODO T69437152 @petetheheat - Delete this fork when Fabric ships to 100%. const NativeAnimatedModule = - Platform.OS === 'ios' && global.RN$Bridgeless === true - ? NativeAnimatedTurboModule - : NativeAnimatedNonTurboModule; + NativeAnimatedNonTurboModule ?? NativeAnimatedTurboModule; let __nativeAnimatedNodeTagCount = 1; /* used for animated nodes */ let __nativeAnimationIdCount = 1; /* used for started animations */ diff --git a/packages/react-native/Libraries/Animated/NativeAnimatedModule.js b/packages/react-native/Libraries/Animated/NativeAnimatedModule.js index 9fc932e6b50..a080eb4d66e 100644 --- a/packages/react-native/Libraries/Animated/NativeAnimatedModule.js +++ b/packages/react-native/Libraries/Animated/NativeAnimatedModule.js @@ -11,6 +11,7 @@ import type {TurboModule} from '../TurboModule/RCTExport'; import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; +import shouldUseTurboAnimatedModule from './shouldUseTurboAnimatedModule'; type EndResult = {finished: boolean, ...}; type EndCallback = (result: EndResult) => void; @@ -70,4 +71,7 @@ export interface Spec extends TurboModule { +queueAndExecuteBatchedOperations?: (operationsAndArgs: Array) => void; } -export default (TurboModuleRegistry.get('NativeAnimatedModule'): ?Spec); +const NativeModule: ?Spec = !shouldUseTurboAnimatedModule() + ? TurboModuleRegistry.get('NativeAnimatedModule') + : null; +export default NativeModule; diff --git a/packages/react-native/Libraries/Animated/NativeAnimatedTurboModule.js b/packages/react-native/Libraries/Animated/NativeAnimatedTurboModule.js index 58664ca8742..f9ea5881232 100644 --- a/packages/react-native/Libraries/Animated/NativeAnimatedTurboModule.js +++ b/packages/react-native/Libraries/Animated/NativeAnimatedTurboModule.js @@ -11,6 +11,7 @@ import type {TurboModule} from '../TurboModule/RCTExport'; import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; +import shouldUseTurboAnimatedModule from './shouldUseTurboAnimatedModule'; type EndResult = {finished: boolean, ...}; type EndCallback = (result: EndResult) => void; @@ -70,6 +71,8 @@ export interface Spec extends TurboModule { +queueAndExecuteBatchedOperations?: (operationsAndArgs: Array) => void; } -export default (TurboModuleRegistry.get( - 'NativeAnimatedTurboModule', -): ?Spec); +const NativeModule: ?Spec = shouldUseTurboAnimatedModule() + ? TurboModuleRegistry.get('NativeAnimatedTurboModule') + : null; + +export default NativeModule; diff --git a/packages/react-native/Libraries/Animated/shouldUseTurboAnimatedModule.js b/packages/react-native/Libraries/Animated/shouldUseTurboAnimatedModule.js new file mode 100644 index 00000000000..c112ba99aca --- /dev/null +++ b/packages/react-native/Libraries/Animated/shouldUseTurboAnimatedModule.js @@ -0,0 +1,17 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +import Platform from '../Utilities/Platform'; + +function shouldUseTurboAnimatedModule(): boolean { + return Platform.OS === 'ios' && global.RN$Bridgeless === true; +} + +export default shouldUseTurboAnimatedModule;