From 3129344affda1086f3a250480b5c5d917eb8cf52 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Fri, 21 Apr 2023 16:37:04 -0700 Subject: [PATCH] Remove getNative* TurboModuleRegistry APIs Summary: These getNative* TurboModuleRegistry APIs make the TurboModuleManager harder to understand. They should have never been introduced. Let's remove them for added clarity. Changelog: [Android][Removed] - Remove TurboModuleManager.getNativeModule,getNativeModules,hasNativeModule Reviewed By: mdvacca Differential Revision: D45158032 fbshipit-source-id: 6f904dfe43fe7b2c215070a19f474acceddbf2f8 --- .../react/bridge/CatalystInstanceImpl.java | 6 +- .../react/bridgeless/ReactInstance.java | 8 +-- .../turbomodule/core/TurboModuleManager.java | 55 +++---------------- .../core/interfaces/TurboModuleRegistry.java | 27 ++------- 4 files changed, 21 insertions(+), 75 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java index d377ceb595394d..0329ae8e92b0b1 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java @@ -457,7 +457,7 @@ public T getJSModule(Class jsInterface) { @Override public boolean hasNativeModule(Class nativeModuleInterface) { String moduleName = getNameFromAnnotation(nativeModuleInterface); - return getTurboModuleRegistry() != null && getTurboModuleRegistry().hasNativeModule(moduleName) + return getTurboModuleRegistry() != null && getTurboModuleRegistry().hasModule(moduleName) ? true : mNativeModuleRegistry.hasModule(moduleName); } @@ -482,7 +482,7 @@ private TurboModuleRegistry getTurboModuleRegistry() { @Nullable public NativeModule getNativeModule(String moduleName) { if (getTurboModuleRegistry() != null) { - NativeModule module = getTurboModuleRegistry().getNativeModule(moduleName); + NativeModule module = getTurboModuleRegistry().getModule(moduleName); if (module != null) { return module; } @@ -509,7 +509,7 @@ public Collection getNativeModules() { nativeModules.addAll(mNativeModuleRegistry.getAllModules()); if (getTurboModuleRegistry() != null) { - for (NativeModule module : getTurboModuleRegistry().getNativeModules()) { + for (NativeModule module : getTurboModuleRegistry().getModules()) { nativeModules.add(module); } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java index 96813436fde149..1a1f9368d9aa2f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactInstance.java @@ -198,7 +198,7 @@ public String[] getComponentNames() { // Eagerly initialize TurboModules for (String moduleName : mTurboModuleManager.getEagerInitModuleNames()) { - mTurboModuleManager.getNativeModule(moduleName); + mTurboModuleManager.getModule(moduleName); } Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); @@ -290,14 +290,14 @@ public void setSourceURLs(String deviceURL, String remoteURL) { public boolean hasNativeModule(Class nativeModuleInterface) { ReactModule annotation = nativeModuleInterface.getAnnotation(ReactModule.class); if (annotation != null) { - return mTurboModuleManager.hasNativeModule(annotation.name()); + return mTurboModuleManager.hasModule(annotation.name()); } return false; } public Collection getNativeModules() { Collection nativeModules = new ArrayList<>(); - for (NativeModule module : mTurboModuleManager.getNativeModules()) { + for (NativeModule module : mTurboModuleManager.getModules()) { nativeModules.add(module); } return nativeModules; @@ -313,7 +313,7 @@ public Collection getNativeModules() { public @Nullable NativeModule getNativeModule(String nativeModuleName) { synchronized (mTurboModuleManager) { - return mTurboModuleManager.getNativeModule(nativeModuleName); + return mTurboModuleManager.getModule(nativeModuleName); } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/TurboModuleManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/TurboModuleManager.java index b3a4440a3fb02b..2d2f4e0dffb0d0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/TurboModuleManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/TurboModuleManager.java @@ -131,7 +131,7 @@ private static List getMethodDescripto @DoNotStrip @Nullable private NativeModule getLegacyJavaModule(String moduleName) { - final NativeModule module = getNativeModule(moduleName); + final NativeModule module = getModule(moduleName); if (shouldRouteTurboModulesThroughInteropLayer()) { return !(module instanceof CxxModuleWrapper) ? module : null; @@ -146,7 +146,7 @@ private NativeModule getLegacyJavaModule(String moduleName) { @DoNotStrip @Nullable private CxxModuleWrapper getLegacyCxxModule(String moduleName) { - final NativeModule module = getNativeModule(moduleName); + final NativeModule module = getModule(moduleName); if (shouldRouteTurboModulesThroughInteropLayer()) { return module instanceof CxxModuleWrapper ? (CxxModuleWrapper) module : null; @@ -165,7 +165,7 @@ private CxxModuleWrapper getTurboLegacyCxxModule(String moduleName) { return null; } - final NativeModule module = getNativeModule(moduleName); + final NativeModule module = getModule(moduleName); return module instanceof CxxModuleWrapper && module instanceof TurboModule ? (CxxModuleWrapper) module : null; @@ -178,18 +178,12 @@ private TurboModule getTurboJavaModule(String moduleName) { return null; } - final NativeModule module = getNativeModule(moduleName); + final NativeModule module = getModule(moduleName); return !(module instanceof CxxModuleWrapper) && module instanceof TurboModule ? (TurboModule) module : null; } - @Deprecated - public TurboModule getModule(String moduleName) { - NativeModule module = getNativeModule(moduleName); - return module instanceof TurboModule ? (TurboModule) module : null; - } - /** * Return the NativeModule instance that corresponds to the provided moduleName. * @@ -197,7 +191,7 @@ public TurboModule getModule(String moduleName) { * null after TurboModuleManager has been torn down. */ @Nullable - public NativeModule getNativeModule(String moduleName) { + public NativeModule getModule(String moduleName) { ModuleHolder moduleHolder; synchronized (mModuleCleanupLock) { @@ -220,7 +214,7 @@ public NativeModule getNativeModule(String moduleName) { } TurboModulePerfLogger.moduleCreateStart(moduleName, moduleHolder.getModuleId()); - NativeModule module = getOrCreateNativeModule(moduleName, moduleHolder, true); + NativeModule module = getOrCreateModule(moduleName, moduleHolder, true); if (module != null) { TurboModulePerfLogger.moduleCreateEnd(moduleName, moduleHolder.getModuleId()); @@ -238,7 +232,7 @@ public NativeModule getNativeModule(String moduleName) { * first thread creates x. All n - 1 other threads wait until the x is created and initialized. */ @Nullable - private NativeModule getOrCreateNativeModule( + private NativeModule getOrCreateModule( String moduleName, @NonNull ModuleHolder moduleHolder, boolean shouldPerfLog) { boolean shouldCreateModule = false; @@ -315,21 +309,8 @@ private NativeModule getOrCreateNativeModule( } } - @Deprecated - public Collection getModules() { - final Collection modules = new ArrayList<>(); - - for (final NativeModule module : getNativeModules()) { - if (module instanceof TurboModule) { - modules.add((TurboModule) module); - } - } - - return modules; - } - /** Which NativeModules have been created? */ - public Collection getNativeModules() { + public Collection getModules() { final List moduleHolders = new ArrayList<>(); synchronized (mModuleCleanupLock) { moduleHolders.addAll(mModuleHolders.values()); @@ -348,30 +329,12 @@ public Collection getNativeModules() { return modules; } - @Deprecated public boolean hasModule(String moduleName) { ModuleHolder moduleHolder; synchronized (mModuleCleanupLock) { moduleHolder = mModuleHolders.get(moduleName); } - if (moduleHolder != null) { - synchronized (moduleHolder) { - if (moduleHolder.getModule() instanceof TurboModule) { - return true; - } - } - } - - return false; - } - - public boolean hasNativeModule(String moduleName) { - ModuleHolder moduleHolder; - synchronized (mModuleCleanupLock) { - moduleHolder = mModuleHolders.get(moduleName); - } - if (moduleHolder != null) { synchronized (moduleHolder) { if (moduleHolder.getModule() != null) { @@ -418,7 +381,7 @@ public void onCatalystInstanceDestroy() { * initialized. In this case, we should wait for initialization to complete, before destroying * the TurboModule. */ - final NativeModule nativeModule = getOrCreateNativeModule(moduleName, moduleHolder, false); + final NativeModule nativeModule = getOrCreateModule(moduleName, moduleHolder, false); if (nativeModule != null) { nativeModule.invalidate(); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/interfaces/TurboModuleRegistry.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/interfaces/TurboModuleRegistry.java index b7fc5a944191f9..a255d1a4140531 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/interfaces/TurboModuleRegistry.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/interfaces/TurboModuleRegistry.java @@ -19,36 +19,19 @@ * deleted, we should rename this interface accordingly. */ public interface TurboModuleRegistry { - /** - * Return the TurboModule instance that has that name `moduleName`. If the `moduleName` - * TurboModule hasn't been instantiated, instantiate it. If no TurboModule is registered under - * `moduleName`, return null. - */ - @Deprecated - @Nullable - TurboModule getModule(String moduleName); - - /** Get all instantiated TurboModules. */ - @Deprecated - Collection getModules(); - - /** Has the TurboModule with name `moduleName` been instantiated? */ - @Deprecated - boolean hasModule(String moduleName); - /** * Return the NativeModule instance that has that name `moduleName`. If the `moduleName` - * NativeModule hasn't been instantiated, instantiate it. If no NativeModule is registered under + * TurboModule hasn't been instantiated, instantiate it. If no TurboModule is registered under * `moduleName`, return null. */ @Nullable - NativeModule getNativeModule(String moduleName); + NativeModule getModule(String moduleName); - /** Get all instantiated NativeModule. */ - Collection getNativeModules(); + /** Get all instantiated NativeModules. */ + Collection getModules(); /** Has the NativeModule with name `moduleName` been instantiated? */ - boolean hasNativeModule(String moduleName); + boolean hasModule(String moduleName); /** * Return the names of all the NativeModules that are supposed to be eagerly initialized. By