Skip to content

Commit

Permalink
Replace callback for lambdas in TurboModules classes (#36908)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36908

We can now use Java 8, this diff i'm replacing old callbacks by lambdas
changelog: [internal] internal

Reviewed By: fkgozali

Differential Revision: D44977955

fbshipit-source-id: 0ab0d1c49a013e930041936bb08bbdb86dd30236
  • Loading branch information
mdvacca authored and facebook-github-bot committed Apr 14, 2023
1 parent 234f199 commit ce17c37
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ public class CompositeReactPackageTurboModuleManagerDelegate

protected native HybridData initHybrid();

private final List<TurboModuleManagerDelegate> mDelegates;

private CompositeReactPackageTurboModuleManagerDelegate(
ReactApplicationContext context,
List<ReactPackage> packages,
List<TurboModuleManagerDelegate> delegates) {
super(context, packages);
mDelegates = delegates;
for (TurboModuleManagerDelegate delegate : delegates) {
addTurboModuleManagerDelegate(delegate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,7 @@ private Map<String, ModuleSpec> getViewManagersMap() {
if (mViewManagers == null) {
Map<String, ModuleSpec> viewManagers = new HashMap<>();
appendMap(
viewManagers,
TraceUpdateOverlayManager.REACT_CLASS,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
return new TraceUpdateOverlayManager();
}
});
viewManagers, TraceUpdateOverlayManager.REACT_CLASS, TraceUpdateOverlayManager::new);

mViewManagers = viewManagers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;

import androidx.annotation.NonNull;
import com.facebook.react.bridge.ModuleHolder;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
Expand All @@ -36,30 +35,23 @@ public abstract class LazyReactPackage implements ReactPackage {
@Deprecated
public static ReactModuleInfoProvider getReactModuleInfoProviderViaReflection(
LazyReactPackage lazyReactPackage) {
return new ReactModuleInfoProvider() {
@Override
public Map<String, ReactModuleInfo> getReactModuleInfos() {
return Collections.emptyMap();
}
};
return Collections::emptyMap;
}
/**
* We return an iterable
*
* @param reactContext
* @return
* @param reactContext context
* @return {@link Iterable<ModuleHolder>} that contains all native modules registered for the
* context
*/
public Iterable<ModuleHolder> getNativeModuleIterator(
final ReactApplicationContext reactContext) {
final Map<String, ReactModuleInfo> reactModuleInfoMap =
getReactModuleInfoProvider().getReactModuleInfos();
final List<ModuleSpec> nativeModules = getNativeModules(reactContext);

return new Iterable<ModuleHolder>() {
@NonNull
@Override
public Iterator<ModuleHolder> iterator() {
return new Iterator<ModuleHolder>() {
return () ->
new Iterator<ModuleHolder>() {
int position = 0;

@Override
Expand Down Expand Up @@ -93,8 +85,6 @@ public void remove() {
throw new UnsupportedOperationException("Cannot remove native modules from the list");
}
};
}
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,49 +71,43 @@ public TurboModuleManager(
delegate == null ? new ArrayList<String>() : delegate.getEagerInitModuleNames();

mModuleProvider =
new ModuleProvider<TurboModule>() {
@Nullable
public TurboModule getModule(String moduleName) {
if (delegate == null || shouldRouteTurboModulesThroughInteropLayer()) {
return null;
}
moduleName -> {
if (delegate == null || shouldRouteTurboModulesThroughInteropLayer()) {
return null;
}

TurboModule module = delegate.getModule(moduleName);
if (module == null) {
CxxModuleWrapper legacyCxxModule = delegate.getLegacyCxxModule(moduleName);

if (legacyCxxModule != null) {
// TurboModuleManagerDelegate.getLegacyCxxModule() must always return TurboModules
Assertions.assertCondition(
legacyCxxModule instanceof TurboModule,
"CxxModuleWrapper \"" + moduleName + "\" is not a TurboModule");
module = (TurboModule) legacyCxxModule;
}
TurboModule module = delegate.getModule(moduleName);
if (module == null) {
CxxModuleWrapper legacyCxxModule = delegate.getLegacyCxxModule(moduleName);

if (legacyCxxModule != null) {
// TurboModuleManagerDelegate.getLegacyCxxModule() must always return TurboModules
Assertions.assertCondition(
legacyCxxModule instanceof TurboModule,
"CxxModuleWrapper \"" + moduleName + "\" is not a TurboModule");
module = (TurboModule) legacyCxxModule;
}
return module;
}
return module;
};

mLegacyModuleProvider =
new ModuleProvider<NativeModule>() {
@Nullable
public NativeModule getModule(String moduleName) {
if (delegate == null || !shouldCreateLegacyModules()) {
return null;
}
moduleName -> {
if (delegate == null || !shouldCreateLegacyModules()) {
return null;
}

NativeModule nativeModule = delegate.getLegacyModule(moduleName);
if (nativeModule != null) {
if (!shouldRouteTurboModulesThroughInteropLayer()) {
// TurboModuleManagerDelegate.getLegacyModule must never return a TurboModule
Assertions.assertCondition(
!(nativeModule instanceof TurboModule),
"NativeModule \"" + moduleName + "\" is a TurboModule");
}
return nativeModule;
NativeModule nativeModule = delegate.getLegacyModule(moduleName);
if (nativeModule != null) {
if (!shouldRouteTurboModulesThroughInteropLayer()) {
// TurboModuleManagerDelegate.getLegacyModule must never return a TurboModule
Assertions.assertCondition(
!(nativeModule instanceof TurboModule),
"NativeModule \"" + moduleName + "\" is a TurboModule");
}
return nativeModule;
}
return nativeModule;
};
}

Expand Down Expand Up @@ -148,6 +142,7 @@ private NativeModule getLegacyJavaModule(String moduleName) {
: null;
}

@SuppressWarnings("unused")
@DoNotStrip
@Nullable
private CxxModuleWrapper getLegacyCxxModule(String moduleName) {
Expand All @@ -158,6 +153,7 @@ private CxxModuleWrapper getLegacyCxxModule(String moduleName) {
: null;
}

@SuppressWarnings("unused")
@DoNotStrip
@Nullable
private CxxModuleWrapper getTurboLegacyCxxModule(String moduleName) {
Expand Down

0 comments on commit ce17c37

Please sign in to comment.