Skip to content

Commit

Permalink
Merge lazy logic into ReactPackageTurboModuleManagerDelegate (faceboo…
Browse files Browse the repository at this point in the history
…k#41468)

Summary:

We want to unify all TurboModuleManagerDelegates everywhere. Why: Unifying the delegates will help us remove the TurboModuleManagerDelegateBuilder from the ReactHostDelegate properly.

Therefore, we need to inline the lazy logic into ReactPackageTurboModuleManagerDelegate.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D51093697
  • Loading branch information
RSNara authored and facebook-github-bot committed Nov 16, 2023
1 parent 5730f56 commit 360acea
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 102 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,25 @@ interface ModuleProvider {
private final boolean mEnableTurboModuleSyncVoidMethods =
ReactFeatureFlags.unstable_enableTurboModuleSyncVoidMethods;

private final boolean mIsLazy = ReactFeatureFlags.enableTurboModuleStableAPI;

// Lazy Props
private List<ReactPackage> mPackages;
private ReactApplicationContext mReactContext;

protected ReactPackageTurboModuleManagerDelegate() {
super();
}

protected ReactPackageTurboModuleManagerDelegate(
ReactApplicationContext reactApplicationContext, List<ReactPackage> packages) {
super();
if (mIsLazy) {
mPackages = packages;
mReactContext = reactApplicationContext;
return;
}

final ReactApplicationContext applicationContext = reactApplicationContext;
for (ReactPackage reactPackage : packages) {
if (reactPackage instanceof BaseReactPackage) {
Expand Down Expand Up @@ -130,21 +142,65 @@ protected ReactPackageTurboModuleManagerDelegate(

@Override
public boolean unstable_shouldEnableLegacyModuleInterop() {
if (mIsLazy) {
return false;
}

return mShouldEnableLegacyModuleInterop;
}

@Override
public boolean unstable_shouldRouteTurboModulesThroughLegacyModuleInterop() {
if (mIsLazy) {
return false;
}

return mShouldRouteTurboModulesThroughLegacyModuleInterop;
}

public boolean unstable_enableSyncVoidMethods() {
if (mIsLazy) {
return false;
}

return mEnableTurboModuleSyncVoidMethods;
}

@Nullable
@Override
public TurboModule getModule(String moduleName) {
if (mIsLazy) {
/*
* Returns first TurboModule found with the name received as a parameter. There's no
* warning or error if there are more than one TurboModule registered with the same name in
* different packages. This method relies on the order of insertion of ReactPackage into
* mPackages. Usually the size of mPackages is very small (2 or 3 packages in the majority of
* the cases)
*/
for (ReactPackage reactPackage : mPackages) {
if (reactPackage instanceof BaseReactPackage) {
BaseReactPackage baseReactPackage = (BaseReactPackage) reactPackage;
try {
TurboModule nativeModule =
(TurboModule) baseReactPackage.getModule(moduleName, mReactContext);
if (nativeModule != null) {
return nativeModule;
}
} catch (IllegalArgumentException ex) {
/*
TurboReactPackages can throw an IllegalArgumentException when a module isn't found. If
this happens, it's safe to ignore the exception because a later TurboReactPackage could
provide the module.
*/
}
} else {
throw new IllegalArgumentException(
"ReactPackage must be an instance of TurboReactPackage");
}
}
return null;
}

NativeModule resolvedModule = null;

for (final ModuleProvider moduleProvider : mModuleProviders) {
Expand Down Expand Up @@ -180,11 +236,15 @@ public TurboModule getModule(String moduleName) {

@Override
public boolean unstable_isLazyTurboModuleDelegate() {
return false;
return mIsLazy;
}

@Override
public boolean unstable_isModuleRegistered(String moduleName) {
if (mIsLazy) {
throw new UnsupportedOperationException("unstable_isModuleRegistered is not supported");
}

for (final ModuleProvider moduleProvider : mModuleProviders) {
final ReactModuleInfo moduleInfo = mPackageModuleInfos.get(moduleProvider).get(moduleName);
if (moduleInfo != null && moduleInfo.isTurboModule()) {
Expand All @@ -196,6 +256,10 @@ public boolean unstable_isModuleRegistered(String moduleName) {

@Override
public boolean unstable_isLegacyModuleRegistered(String moduleName) {
if (mIsLazy) {
return false;
}

for (final ModuleProvider moduleProvider : mModuleProviders) {
final ReactModuleInfo moduleInfo = mPackageModuleInfos.get(moduleProvider).get(moduleName);
if (moduleInfo != null && !moduleInfo.isTurboModule()) {
Expand All @@ -208,6 +272,10 @@ public boolean unstable_isLegacyModuleRegistered(String moduleName) {
@Nullable
@Override
public NativeModule getLegacyModule(String moduleName) {
if (mIsLazy) {
throw new UnsupportedOperationException("Legacy Modules are not supported");
}

if (!unstable_shouldEnableLegacyModuleInterop()) {
return null;
}
Expand Down Expand Up @@ -247,6 +315,11 @@ public NativeModule getLegacyModule(String moduleName) {

@Override
public List<String> getEagerInitModuleNames() {
if (mIsLazy) {
throw new UnsupportedOperationException(
"Running delegate in lazy mode. Please override getEagerInitModuleNames() and return a list of module names that need to be initialized eagerly.");
}

List<String> moduleNames = new ArrayList<>();
for (final ModuleProvider moduleProvider : mModuleProviders) {
for (final ReactModuleInfo moduleInfo : mPackageModuleInfos.get(moduleProvider).values()) {
Expand Down

0 comments on commit 360acea

Please sign in to comment.