From 0e540298ae0636e6eccde2137d021caba034afd3 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Wed, 19 Feb 2025 05:37:54 -0800 Subject: [PATCH] remove requirement for main queue setup for RCTPlatform (#49474) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49474 changelog: [internal] Can move setup to the background setup: 1. Use RCTTraitCollectionProxy to access current trait collection to check if force touch is available. 2. UIDevice.currentDevice is thread safe and can be read safely from a background thread. Reviewed By: rshest Differential Revision: D69744257 fbshipit-source-id: 8857a827c3d3e03a9b2ced15e6970d728693a043 --- .../React/CoreModules/RCTPlatform.mm | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTPlatform.mm b/packages/react-native/React/CoreModules/RCTPlatform.mm index ec8a5f3220e..8ae71b13007 100644 --- a/packages/react-native/React/CoreModules/RCTPlatform.mm +++ b/packages/react-native/React/CoreModules/RCTPlatform.mm @@ -10,6 +10,7 @@ #import #import +#import #import #import @@ -48,7 +49,7 @@ RCT_EXPORT_MODULE(PlatformConstants) + (BOOL)requiresMainQueueSetup { - return YES; + return NO; } - (dispatch_queue_t)methodQueue @@ -64,30 +65,27 @@ RCT_EXPORT_MODULE(PlatformConstants) - (ModuleConstants)getConstants { - __block ModuleConstants constants; - RCTUnsafeExecuteOnMainQueueSync(^{ - UIDevice *device = [UIDevice currentDevice]; - auto versions = RCTGetReactNativeVersion(); - constants = typedConstants({ - .forceTouchAvailable = RCTForceTouchAvailable() ? true : false, - .osVersion = [device systemVersion], - .systemName = [device systemName], - .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), - .isTesting = RCTRunningInTestEnvironment() ? true : false, - .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( - {.minor = [versions[@"minor"] doubleValue], - .major = [versions[@"major"] doubleValue], - .patch = [versions[@"patch"] doubleValue], - .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}), + UIDevice *device = [UIDevice currentDevice]; + bool isForceTouchAvailable = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection.forceTouchCapability == + UIForceTouchCapabilityAvailable; + auto versions = RCTGetReactNativeVersion(); + return typedConstants({ + .forceTouchAvailable = isForceTouchAvailable, + .osVersion = [device systemVersion], + .systemName = [device systemName], + .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), + .isTesting = RCTRunningInTestEnvironment() ? true : false, + .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( + {.minor = [versions[@"minor"] doubleValue], + .major = [versions[@"major"] doubleValue], + .patch = [versions[@"patch"] doubleValue], + .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}), #if TARGET_OS_MACCATALYST - .isMacCatalyst = true, + .isMacCatalyst = true, #else - .isMacCatalyst = false, + .isMacCatalyst = false, #endif - }); }); - - return constants; } - (std::shared_ptr)getTurboModule:(const ObjCTurboModule::InitParams &)params