Skip to content

Commit 2bf866e

Browse files
RSNarafacebook-github-bot
authored andcommitted
Make NativeModules immediately initializable
Summary: ## Problem: NativeModules can only be initialized after we mark them initializable on the NativeModules thread. This work is scheduled in ReactInstanceManager.setupReactContext(), after we schedule the execution of the JS bundle on the JavaScript thread in ReactInstanceManager.createReactContext(): https://www.internalfb.com/intern/diffusion/FBS/browse/master/xplat/js/react-native-github/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java?commit=9b58f58b8eb12281567b8d24d6d000e868e61a1f&lines=1256-1257%2C1331%2C1334-1335%2C1333-1334 So, if the timings don't work out, the JavaScript thread could start executing the JS bundle before the NativeModule thread makes all NativeModules initializable. In this case, those NativeModule will just appear to be null in C++/JavaScript. ## Fix This diff makes all NativeModules initializable immediately after their ModuleHolder is created. ModuleHolder.markInitializable() simply initializes initializes modules that were eagerly created. Changelog: [Android][Fixed] - Make NativeModules immediately initializable Reviewed By: JoshuaGross Differential Revision: D26233372 fbshipit-source-id: a9223ff093da5b80781169be88e6ec9516c7a29b
1 parent f66dcab commit 2bf866e

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleHolder.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public class ModuleHolder {
5050
private @Nullable @GuardedBy("this") NativeModule mModule;
5151

5252
// These are used to communicate phases of creation and initialization across threads
53-
private @GuardedBy("this") boolean mInitializable;
5453
private @GuardedBy("this") boolean mIsCreating;
5554
private @GuardedBy("this") boolean mIsInitializing;
5655

@@ -89,7 +88,6 @@ public ModuleHolder(NativeModule nativeModule) {
8988
boolean shouldInitializeNow = false;
9089
NativeModule module = null;
9190
synchronized (this) {
92-
mInitializable = true;
9391
if (mModule != null) {
9492
Assertions.assertCondition(!mIsInitializing);
9593
shouldInitializeNow = true;
@@ -193,7 +191,7 @@ private NativeModule create() {
193191
boolean shouldInitializeNow = false;
194192
synchronized (this) {
195193
mModule = module;
196-
if (mInitializable && !mIsInitializing) {
194+
if (!mIsInitializing) {
197195
shouldInitializeNow = true;
198196
}
199197
}
@@ -227,7 +225,7 @@ private void doInitialize(NativeModule module) {
227225
boolean shouldInitialize = false;
228226
// Check to see if another thread is initializing the object, if not claim the responsibility
229227
synchronized (this) {
230-
if (mInitializable && !mIsInitializing) {
228+
if (!mIsInitializing) {
231229
shouldInitialize = true;
232230
mIsInitializing = true;
233231
}

0 commit comments

Comments
 (0)