Convert com.facebook.react.ReactPackageHelper to Kotlin (#47683)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47683

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D66131407

fbshipit-source-id: 73d637afea71a77140051efd8001d6ceebc32a43
This commit is contained in:
Pieter De Baets 2024-11-20 04:05:26 -08:00 committed by Facebook GitHub Bot
parent e0a5bbe309
commit e80bdb88be
2 changed files with 43 additions and 56 deletions

View File

@ -1,56 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ModuleHolder;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.common.ReactConstants;
import java.util.Iterator;
import java.util.List;
class ReactPackageHelper {
/**
* A helper method to iterate over a list of Native Modules and convert them to an iterable.
*
* @param reactPackage
* @param reactApplicationContext
* @param reactInstanceManager
* @return
*/
/** package */
static Iterable<ModuleHolder> getNativeModuleIterator(
ReactPackage reactPackage, ReactApplicationContext reactApplicationContext) {
FLog.d(
ReactConstants.TAG,
reactPackage.getClass().getSimpleName()
+ " is not a LazyReactPackage, falling back to old version.");
final List<NativeModule> nativeModules =
reactPackage.createNativeModules(reactApplicationContext);
return () ->
new Iterator<ModuleHolder>() {
int position = 0;
@Override
public ModuleHolder next() {
return new ModuleHolder(nativeModules.get(position++));
}
@Override
public boolean hasNext() {
return position < nativeModules.size();
}
@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove methods ");
}
};
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react
import com.facebook.common.logging.FLog
import com.facebook.react.bridge.ModuleHolder
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.common.ReactConstants
internal object ReactPackageHelper {
/**
* A helper method to iterate over a list of Native Modules and convert them to an iterable.
*
* @param reactPackage
* @param reactApplicationContext
* @param reactInstanceManager
* @return
*/
fun getNativeModuleIterator(
reactPackage: ReactPackage,
reactApplicationContext: ReactApplicationContext
): Iterable<ModuleHolder> {
FLog.d(
ReactConstants.TAG,
"${reactPackage.javaClass.simpleName} is not a LazyReactPackage, falling back to old version.")
val nativeModules = reactPackage.createNativeModules(reactApplicationContext)
return Iterable {
object : Iterator<ModuleHolder> {
var position = 0
override fun next(): ModuleHolder = ModuleHolder(nativeModules[position++])
override fun hasNext(): Boolean = position < nativeModules.size
}
}
}
}