Skip to content

Commit 061174c

Browse files
RSNarafacebook-github-bot
authored andcommitted
Bridgeless: Implement main queue setup api in turbo modules (#50040)
Summary: Pull Request resolved: #50040 This diff implements main queue module setup. Sometimes, people need to capture uikit things, and use them from javascript. In those cases, people can write main queue modules. These modules will be eagerly initialized on the main queue, during react native init. ## On Necessity **Sync** dispatches to the main thread from the js thread can deadlock react native. And **async** dispatches to the main thread from the js thread sometimes might not be enough: it could lead to flickery rendering. So, we need to allow people to capture ui thread things, before any js executes. ## Caveat This api is dangerous and discouraged. All react native surfaces will pay the cost of one surface introducing a main queue module. It could also slow down common/critical interactions in your app, if you're not careful. We will introduce performance logging for this infrastructure. So that we can monitor and file tasks, when main queue module init starts taking "too long." Changelog: [General][Breaking]: Introduce beforeload callback arg into ReactInstance::loadScript Reviewed By: mdvacca Differential Revision: D71084243 fbshipit-source-id: 8fdb84761ac69468afc428f4f79eff6322449e3c
1 parent 636665c commit 061174c

25 files changed

+217
-56
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<df008a78acc9b0104c54e06849d5e9b9>>
7+
* @generated SignedSource<<49a61b2330d748fc05bd5e8ce042bf72>>
88
*/
99

1010
/**
@@ -118,6 +118,12 @@ public object ReactNativeFeatureFlags {
118118
@JvmStatic
119119
public fun enableLongTaskAPI(): Boolean = accessor.enableLongTaskAPI()
120120

121+
/**
122+
* Makes modules requiring main queue setup initialize on the main thread, during React Native init.
123+
*/
124+
@JvmStatic
125+
public fun enableMainQueueModulesOnIOS(): Boolean = accessor.enableMainQueueModulesOnIOS()
126+
121127
/**
122128
* Parse CSS strings using the Fabric CSS parser instead of ViewConfig processing
123129
*/

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<9813fd15c7f54d69d3de6084f733c3a6>>
7+
* @generated SignedSource<<86fe1d1217f974eb9cbe2fc6b556b6f3>>
88
*/
99

1010
/**
@@ -35,6 +35,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
3535
private var enableLayoutAnimationsOnAndroidCache: Boolean? = null
3636
private var enableLayoutAnimationsOnIOSCache: Boolean? = null
3737
private var enableLongTaskAPICache: Boolean? = null
38+
private var enableMainQueueModulesOnIOSCache: Boolean? = null
3839
private var enableNativeCSSParsingCache: Boolean? = null
3940
private var enableNewBackgroundAndBorderDrawablesCache: Boolean? = null
4041
private var enablePropsUpdateReconciliationAndroidCache: Boolean? = null
@@ -198,6 +199,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
198199
return cached
199200
}
200201

202+
override fun enableMainQueueModulesOnIOS(): Boolean {
203+
var cached = enableMainQueueModulesOnIOSCache
204+
if (cached == null) {
205+
cached = ReactNativeFeatureFlagsCxxInterop.enableMainQueueModulesOnIOS()
206+
enableMainQueueModulesOnIOSCache = cached
207+
}
208+
return cached
209+
}
210+
201211
override fun enableNativeCSSParsing(): Boolean {
202212
var cached = enableNativeCSSParsingCache
203213
if (cached == null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<06c5ef75b624d953e7a8d9297feaf816>>
7+
* @generated SignedSource<<e77961bfb13e7465783760d67c60c698>>
88
*/
99

1010
/**
@@ -58,6 +58,8 @@ public object ReactNativeFeatureFlagsCxxInterop {
5858

5959
@DoNotStrip @JvmStatic public external fun enableLongTaskAPI(): Boolean
6060

61+
@DoNotStrip @JvmStatic public external fun enableMainQueueModulesOnIOS(): Boolean
62+
6163
@DoNotStrip @JvmStatic public external fun enableNativeCSSParsing(): Boolean
6264

6365
@DoNotStrip @JvmStatic public external fun enableNewBackgroundAndBorderDrawables(): Boolean

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<cbe1d77424b561149b0bda39955dffac>>
7+
* @generated SignedSource<<f6242ae2989dd2da5ad73861933787f2>>
88
*/
99

1010
/**
@@ -53,6 +53,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi
5353

5454
override fun enableLongTaskAPI(): Boolean = false
5555

56+
override fun enableMainQueueModulesOnIOS(): Boolean = false
57+
5658
override fun enableNativeCSSParsing(): Boolean = false
5759

5860
override fun enableNewBackgroundAndBorderDrawables(): Boolean = false

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<0fb76e329360157ea9772a0d8bebc873>>
7+
* @generated SignedSource<<5fc2a365b51c5e14f99ec1d3b162f4e3>>
88
*/
99

1010
/**
@@ -39,6 +39,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
3939
private var enableLayoutAnimationsOnAndroidCache: Boolean? = null
4040
private var enableLayoutAnimationsOnIOSCache: Boolean? = null
4141
private var enableLongTaskAPICache: Boolean? = null
42+
private var enableMainQueueModulesOnIOSCache: Boolean? = null
4243
private var enableNativeCSSParsingCache: Boolean? = null
4344
private var enableNewBackgroundAndBorderDrawablesCache: Boolean? = null
4445
private var enablePropsUpdateReconciliationAndroidCache: Boolean? = null
@@ -217,6 +218,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
217218
return cached
218219
}
219220

221+
override fun enableMainQueueModulesOnIOS(): Boolean {
222+
var cached = enableMainQueueModulesOnIOSCache
223+
if (cached == null) {
224+
cached = currentProvider.enableMainQueueModulesOnIOS()
225+
accessedFeatureFlags.add("enableMainQueueModulesOnIOS")
226+
enableMainQueueModulesOnIOSCache = cached
227+
}
228+
return cached
229+
}
230+
220231
override fun enableNativeCSSParsing(): Boolean {
221232
var cached = enableNativeCSSParsingCache
222233
if (cached == null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<0c16f7af743a6c4d7aaabf010c1c7327>>
7+
* @generated SignedSource<<1db9978c9096c9775fd7cd7d37637c8c>>
88
*/
99

1010
/**
@@ -53,6 +53,8 @@ public interface ReactNativeFeatureFlagsProvider {
5353

5454
@DoNotStrip public fun enableLongTaskAPI(): Boolean
5555

56+
@DoNotStrip public fun enableMainQueueModulesOnIOS(): Boolean
57+
5658
@DoNotStrip public fun enableNativeCSSParsing(): Boolean
5759

5860
@DoNotStrip public fun enableNewBackgroundAndBorderDrawables(): Boolean

packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<c866e69278d249dd79cb2fc193d1a7e2>>
7+
* @generated SignedSource<<696c541ad3725d2d39eaaf8b9b4a317c>>
88
*/
99

1010
/**
@@ -129,6 +129,12 @@ class ReactNativeFeatureFlagsProviderHolder
129129
return method(javaProvider_);
130130
}
131131

132+
bool enableMainQueueModulesOnIOS() override {
133+
static const auto method =
134+
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("enableMainQueueModulesOnIOS");
135+
return method(javaProvider_);
136+
}
137+
132138
bool enableNativeCSSParsing() override {
133139
static const auto method =
134140
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("enableNativeCSSParsing");
@@ -370,6 +376,11 @@ bool JReactNativeFeatureFlagsCxxInterop::enableLongTaskAPI(
370376
return ReactNativeFeatureFlags::enableLongTaskAPI();
371377
}
372378

379+
bool JReactNativeFeatureFlagsCxxInterop::enableMainQueueModulesOnIOS(
380+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
381+
return ReactNativeFeatureFlags::enableMainQueueModulesOnIOS();
382+
}
383+
373384
bool JReactNativeFeatureFlagsCxxInterop::enableNativeCSSParsing(
374385
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
375386
return ReactNativeFeatureFlags::enableNativeCSSParsing();
@@ -581,6 +592,9 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
581592
makeNativeMethod(
582593
"enableLongTaskAPI",
583594
JReactNativeFeatureFlagsCxxInterop::enableLongTaskAPI),
595+
makeNativeMethod(
596+
"enableMainQueueModulesOnIOS",
597+
JReactNativeFeatureFlagsCxxInterop::enableMainQueueModulesOnIOS),
584598
makeNativeMethod(
585599
"enableNativeCSSParsing",
586600
JReactNativeFeatureFlagsCxxInterop::enableNativeCSSParsing),

packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<ee83dc14463351d400cd75624c803176>>
7+
* @generated SignedSource<<e66d1a86ca5ae4c2ce4ec7ca20390edb>>
88
*/
99

1010
/**
@@ -75,6 +75,9 @@ class JReactNativeFeatureFlagsCxxInterop
7575
static bool enableLongTaskAPI(
7676
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
7777

78+
static bool enableMainQueueModulesOnIOS(
79+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
80+
7881
static bool enableNativeCSSParsing(
7982
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
8083

packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<4d47f61435ba3a74e84752a02def81c2>>
7+
* @generated SignedSource<<07e6cf68cec4f7c7d61de4958b11ff4f>>
88
*/
99

1010
/**
@@ -86,6 +86,10 @@ bool ReactNativeFeatureFlags::enableLongTaskAPI() {
8686
return getAccessor().enableLongTaskAPI();
8787
}
8888

89+
bool ReactNativeFeatureFlags::enableMainQueueModulesOnIOS() {
90+
return getAccessor().enableMainQueueModulesOnIOS();
91+
}
92+
8993
bool ReactNativeFeatureFlags::enableNativeCSSParsing() {
9094
return getAccessor().enableNativeCSSParsing();
9195
}

packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<29cdbefbe02064df4fb65388e64f95a4>>
7+
* @generated SignedSource<<ef74757e57be86fbc860f5fd02973885>>
88
*/
99

1010
/**
@@ -114,6 +114,11 @@ class ReactNativeFeatureFlags {
114114
*/
115115
RN_EXPORT static bool enableLongTaskAPI();
116116

117+
/**
118+
* Makes modules requiring main queue setup initialize on the main thread, during React Native init.
119+
*/
120+
RN_EXPORT static bool enableMainQueueModulesOnIOS();
121+
117122
/**
118123
* Parse CSS strings using the Fabric CSS parser instead of ViewConfig processing
119124
*/

0 commit comments

Comments
 (0)