Skip to content

Commit

Permalink
Remove getNative* TurboModuleRegistry APIs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RSNara authored and facebook-github-bot committed Apr 21, 2023
1 parent 1f1918e commit ac2a4d8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public <T extends JavaScriptModule> T getJSModule(Class<T> jsInterface) {
@Override
public <T extends NativeModule> boolean hasNativeModule(Class<T> nativeModuleInterface) {
String moduleName = getNameFromAnnotation(nativeModuleInterface);
return getTurboModuleRegistry() != null && getTurboModuleRegistry().hasNativeModule(moduleName)
return getTurboModuleRegistry() != null && getTurboModuleRegistry().hasModule(moduleName)
? true
: mNativeModuleRegistry.hasModule(moduleName);
}
Expand All @@ -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;
}
Expand All @@ -509,7 +509,7 @@ public Collection<NativeModule> getNativeModules() {
nativeModules.addAll(mNativeModuleRegistry.getAllModules());

if (getTurboModuleRegistry() != null) {
for (NativeModule module : getTurboModuleRegistry().getNativeModules()) {
for (NativeModule module : getTurboModuleRegistry().getModules()) {
nativeModules.add(module);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -290,14 +290,14 @@ public void setSourceURLs(String deviceURL, String remoteURL) {
public <T extends NativeModule> boolean hasNativeModule(Class<T> nativeModuleInterface) {
ReactModule annotation = nativeModuleInterface.getAnnotation(ReactModule.class);
if (annotation != null) {
return mTurboModuleManager.hasNativeModule(annotation.name());
return mTurboModuleManager.hasModule(annotation.name());
}
return false;
}

public Collection<NativeModule> getNativeModules() {
Collection<NativeModule> nativeModules = new ArrayList<>();
for (NativeModule module : mTurboModuleManager.getNativeModules()) {
for (NativeModule module : mTurboModuleManager.getModules()) {
nativeModules.add(module);
}
return nativeModules;
Expand All @@ -313,7 +313,7 @@ public Collection<NativeModule> getNativeModules() {

public @Nullable NativeModule getNativeModule(String nativeModuleName) {
synchronized (mTurboModuleManager) {
return mTurboModuleManager.getNativeModule(nativeModuleName);
return mTurboModuleManager.getModule(nativeModuleName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private static List<TurboModuleInteropUtils.MethodDescriptor> 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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -178,26 +178,20 @@ 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.
*
* <p>This method: - Creates and initializes the module if it doesn't already exist. - Returns
* null after TurboModuleManager has been torn down.
*/
@Nullable
public NativeModule getNativeModule(String moduleName) {
public NativeModule getModule(String moduleName) {
ModuleHolder moduleHolder;

synchronized (mModuleCleanupLock) {
Expand All @@ -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());
Expand All @@ -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;

Expand Down Expand Up @@ -315,21 +309,8 @@ private NativeModule getOrCreateNativeModule(
}
}

@Deprecated
public Collection<TurboModule> getModules() {
final Collection<TurboModule> 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<NativeModule> getNativeModules() {
public Collection<NativeModule> getModules() {
final List<ModuleHolder> moduleHolders = new ArrayList<>();
synchronized (mModuleCleanupLock) {
moduleHolders.addAll(mModuleHolders.values());
Expand All @@ -348,30 +329,12 @@ public Collection<NativeModule> 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) {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TurboModule> 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<NativeModule> getNativeModules();
/** Get all instantiated NativeModules. */
Collection<NativeModule> 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
Expand Down

0 comments on commit ac2a4d8

Please sign in to comment.