Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core][Android] Stop exporting methods and constants from native proxy #28014

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import android.util.SparseArray;

import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableType;

import java.lang.ref.WeakReference;
import java.util.HashMap;
Expand Down Expand Up @@ -96,11 +94,8 @@ public Map<String, Object> getConstants() {
kotlinModuleRegistry.installJSIInterop();

Map<String, Object> constants = new HashMap<>(3);
constants.put(MODULES_CONSTANTS_KEY, mKotlinInteropModuleRegistry.exportedModulesConstants());
constants.put(EXPORTED_METHODS_KEY, mKotlinInteropModuleRegistry.exportMethods((name, info) -> {
assignExportedMethodsKeys(name, (List<Map<String, Object>>) info);
return null;
}));
constants.put(MODULES_CONSTANTS_KEY, new HashMap<>());
constants.put(EXPORTED_METHODS_KEY, new HashMap<>());
constants.put(VIEW_MANAGERS_METADATA_KEY, mKotlinInteropModuleRegistry.viewManagersMetadata());

CoreLoggerKt.getLogger().info("✅ Constants were exported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.uimanager.ViewManager
import expo.modules.adapters.react.NativeModulesProxy
import expo.modules.kotlin.views.ViewManagerType
import expo.modules.kotlin.defaultmodules.NativeModulesProxyModuleName
import expo.modules.kotlin.exception.CodedException
import expo.modules.kotlin.exception.UnexpectedException
import expo.modules.kotlin.tracing.trace
import expo.modules.kotlin.views.GroupViewManagerWrapper
import expo.modules.kotlin.views.SimpleViewManagerWrapper
import expo.modules.kotlin.views.ViewManagerType
import expo.modules.kotlin.views.ViewManagerWrapperDelegate
import expo.modules.kotlin.views.ViewWrapperDelegateHolder
import java.lang.ref.WeakReference

private typealias ModuleName = String
private typealias ModuleConstants = Map<String, Any?>
private typealias ModuleMethodInfo = Map<String, Any?>

class KotlinInteropModuleRegistry(
modulesProvider: ModulesProvider,
legacyModuleRegistry: expo.modules.core.ModuleRegistry,
Expand All @@ -44,33 +39,6 @@ class KotlinInteropModuleRegistry(
}
}

fun exportedModulesConstants(): Map<ModuleName, ModuleConstants> =
trace("KotlinInteropModuleRegistry.exportedModulesConstants") {
registry
// prevent infinite recursion - exclude NativeProxyModule constants
.filter { holder -> holder.name != NativeModulesProxyModuleName }
.associate { holder ->
holder.name to holder.definition.constantsProvider()
}
}

fun exportMethods(exportKey: (String, List<ModuleMethodInfo>) -> Unit = { _, _ -> }): Map<ModuleName, List<ModuleMethodInfo>> =
trace("KotlinInteropModuleRegistry.exportMethods") {
registry.associate { holder ->
val methodsInfo = holder
.definition
.asyncFunctions
.map { (name, method) ->
mapOf(
"name" to name,
"argumentsCount" to method.argsCount
)
}
exportKey(holder.name, methodsInfo)
holder.name to methodsInfo
}
}

fun exportViewManagers(): List<ViewManager<*, *>> =
trace("KotlinInteropModuleRegistry.exportViewManagers") {
registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,6 @@ class KotlinInteropModuleRegistryTest {
interopModuleRegistry.hasModule("test-2")
}

@Test
fun `should export constants`() {
Truth.assertThat(interopModuleRegistry.exportedModulesConstants())
.containsAtLeast(
"test-1",
mapOf("c1" to 123, "c2" to "123"),
"test-2",
emptyMap<String, Any>()
)
}

@Test
fun `should export view manages`() {
val rnManagers = interopModuleRegistry.exportViewManagers()
val expoManagersNames = interopModuleRegistry.viewManagersMetadata().keys

Truth.assertThat(rnManagers).hasSize(1)
Truth.assertThat(rnManagers.first().name).isEqualTo("ViewManagerAdapter_test-2")
Truth.assertThat(expoManagersNames).containsExactly("test-2")
}

@Test
fun `call method should reject if something goes wrong`() {
val mockedPromise = PromiseMock()
Expand Down
Loading