Skip to content

Commit d7c8729

Browse files
RSNarafacebook-github-bot
authored andcommitted
Make PlatformConstants use main queue setup (#50141)
Summary: Pull Request resolved: #50141 Pull Request resolved: #50111 ## Rationale Rendering can now include main -> js sync calls. If we allow js -> main sync calls during rendering, react native can deadlock. So, this diff moves the js -> main sync calls to "main queue module setup", which occurs before rendering. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D71480047
1 parent 428a1d1 commit d7c8729

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

packages/react-native/React/CoreModules/RCTPlatform.mm

+30-27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <UIKit/UIKit.h>
1111

1212
#import <FBReactNativeSpec/FBReactNativeSpec.h>
13+
#import <React/RCTInitializing.h>
1314
#import <React/RCTUtils.h>
1415
#import <React/RCTVersion.h>
1516

@@ -39,10 +40,12 @@
3940
}
4041
}
4142

42-
@interface RCTPlatform () <NativePlatformConstantsIOSSpec>
43+
@interface RCTPlatform () <NativePlatformConstantsIOSSpec, RCTInitializing>
4344
@end
4445

45-
@implementation RCTPlatform
46+
@implementation RCTPlatform {
47+
ModuleConstants<JS::NativePlatformConstantsIOS::Constants> _constants;
48+
}
4649

4750
RCT_EXPORT_MODULE(PlatformConstants)
4851

@@ -51,6 +54,29 @@ + (BOOL)requiresMainQueueSetup
5154
return YES;
5255
}
5356

57+
- (void)initialize
58+
{
59+
UIDevice *device = [UIDevice currentDevice];
60+
auto versions = RCTGetReactNativeVersion();
61+
_constants = typedConstants<JS::NativePlatformConstantsIOS::Constants>({
62+
.forceTouchAvailable = RCTForceTouchAvailable() ? true : false,
63+
.osVersion = [device systemVersion],
64+
.systemName = [device systemName],
65+
.interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]),
66+
.isTesting = RCTRunningInTestEnvironment() ? true : false,
67+
.reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder(
68+
{.minor = [versions[@"minor"] doubleValue],
69+
.major = [versions[@"major"] doubleValue],
70+
.patch = [versions[@"patch"] doubleValue],
71+
.prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}),
72+
#if TARGET_OS_MACCATALYST
73+
.isMacCatalyst = true,
74+
#else
75+
.isMacCatalyst = false,
76+
#endif
77+
});
78+
}
79+
5480
- (dispatch_queue_t)methodQueue
5581
{
5682
return dispatch_get_main_queue();
@@ -59,35 +85,12 @@ - (dispatch_queue_t)methodQueue
5985
// TODO: Use the generated struct return type.
6086
- (ModuleConstants<JS::NativePlatformConstantsIOS::Constants>)constantsToExport
6187
{
62-
return (ModuleConstants<JS::NativePlatformConstantsIOS::Constants>)[self getConstants];
88+
return _constants;
6389
}
6490

6591
- (ModuleConstants<JS::NativePlatformConstantsIOS::Constants>)getConstants
6692
{
67-
__block ModuleConstants<JS::NativePlatformConstantsIOS::Constants> constants;
68-
RCTUnsafeExecuteOnMainQueueSync(^{
69-
UIDevice *device = [UIDevice currentDevice];
70-
auto versions = RCTGetReactNativeVersion();
71-
constants = typedConstants<JS::NativePlatformConstantsIOS::Constants>({
72-
.forceTouchAvailable = RCTForceTouchAvailable() ? true : false,
73-
.osVersion = [device systemVersion],
74-
.systemName = [device systemName],
75-
.interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]),
76-
.isTesting = RCTRunningInTestEnvironment() ? true : false,
77-
.reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder(
78-
{.minor = [versions[@"minor"] doubleValue],
79-
.major = [versions[@"major"] doubleValue],
80-
.patch = [versions[@"patch"] doubleValue],
81-
.prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}),
82-
#if TARGET_OS_MACCATALYST
83-
.isMacCatalyst = true,
84-
#else
85-
.isMacCatalyst = false,
86-
#endif
87-
});
88-
});
89-
90-
return constants;
93+
return _constants;
9194
}
9295

9396
- (std::shared_ptr<TurboModule>)getTurboModule:(const ObjCTurboModule::InitParams &)params

packages/react-native/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,13 @@
149149
{
150150
"name": "FBReactNativeSpec",
151151
"type": "modules",
152-
"ios": {},
152+
"ios": {
153+
"modules": {
154+
"PlatformConstants": {
155+
"unstableRequiresMainQueueSetup": true
156+
}
157+
}
158+
},
153159
"android": {},
154160
"jsSrcsDir": "src"
155161
},

0 commit comments

Comments
 (0)