Skip to content

Cache UIManagerConstants #71

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ tasks.withType<KotlinCompile>().configureEach {
}

dependencies {
implementation("com.tencent:mmkv-static:1.2.14")
api(libs.androidx.appcompat)
api(libs.androidx.appcompat.resources)
api(libs.androidx.autofill)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.react.modules.core.PermissionListener;
import com.facebook.react.util.AndroidVersion;
import org.jetbrains.annotations.NotNull;
import com.facebook.react.uimanager.UIManagerConstantsCache;

/** Base Activity for React Native applications. */
public abstract class ReactActivity extends AppCompatActivity
Expand Down Expand Up @@ -58,6 +59,7 @@ protected ReactActivityDelegate createReactActivityDelegate() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UIManagerConstantsCache.getInstance().init(this);
mDelegate.onCreate(savedInstanceState);
if (AndroidVersion.isAtLeastTargetSdk36(this)) {
getOnBackPressedDispatcher().addCallback(this, mBackPressedCallback);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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.bridge;

import static com.facebook.react.bridge.ReactMarkerConstants.CONVERT_CONSTANTS_END;
import static com.facebook.react.bridge.ReactMarkerConstants.CONVERT_CONSTANTS_START;
import static com.facebook.react.bridge.ReactMarkerConstants.GET_CONSTANTS_END;
import static com.facebook.react.bridge.ReactMarkerConstants.GET_CONSTANTS_START;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;

import androidx.annotation.Nullable;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.facebook.react.uimanager.UIManagerConstantsCache;

/**
* This is part of the glue which wraps a java BaseJavaModule in a C++ NativeModule. This could all
* be in C++, but it's android-specific initialization code, and writing it this way is easier to
* read and means fewer JNI calls.
*/
@DoNotStrip
class JavaModuleWrapper {

interface NativeMethod {
void invoke(JSInstance jsInstance, ReadableArray parameters);

String getType();
}

@DoNotStrip
public static class MethodDescriptor {
@DoNotStrip Method method;
@DoNotStrip String signature;
@DoNotStrip String name;
@DoNotStrip String type;
}

private final JSInstance mJSInstance;
private final ModuleHolder mModuleHolder;
private final ArrayList<NativeMethod> mMethods;
private final ArrayList<MethodDescriptor> mDescs;

public JavaModuleWrapper(JSInstance jsInstance, ModuleHolder moduleHolder) {
mJSInstance = jsInstance;
mModuleHolder = moduleHolder;
mMethods = new ArrayList<>();
mDescs = new ArrayList<>();
}

@DoNotStrip
public BaseJavaModule getModule() {
return (BaseJavaModule) mModuleHolder.getModule();
}

@DoNotStrip
public String getName() {
return mModuleHolder.getName();
}

@DoNotStrip
private void findMethods() {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "findMethods");

Class<? extends NativeModule> classForMethods = mModuleHolder.getModule().getClass();
Class<? extends NativeModule> superClass =
(Class<? extends NativeModule>) classForMethods.getSuperclass();
if (TurboModule.class.isAssignableFrom(superClass)) {
// For java module that is based on generated flow-type spec, inspect the
// spec abstract class instead, which is the super class of the given java
// module.
classForMethods = superClass;
}
Method[] targetMethods = classForMethods.getDeclaredMethods();

for (Method targetMethod : targetMethods) {
ReactMethod annotation = targetMethod.getAnnotation(ReactMethod.class);
if (annotation != null) {
String methodName = targetMethod.getName();
MethodDescriptor md = new MethodDescriptor();
JavaMethodWrapper method =
new JavaMethodWrapper(this, targetMethod, annotation.isBlockingSynchronousMethod());
md.name = methodName;
md.type = method.getType();
if (BaseJavaModule.METHOD_TYPE_SYNC.equals(md.type)) {
md.signature = method.getSignature();
md.method = targetMethod;
}
mMethods.add(method);
mDescs.add(md);
}
}
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}

@DoNotStrip
public List<MethodDescriptor> getMethodDescriptors() {
if (mDescs.isEmpty()) {
findMethods();
}
return mDescs;
}

@DoNotStrip
public @Nullable NativeMap getConstants() {
final String moduleName = getName();
SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "JavaModuleWrapper.getConstants")
.arg("moduleName", moduleName)
.flush();
ReactMarker.logMarker(GET_CONSTANTS_START, moduleName);


Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "module.getModule");
BaseJavaModule baseJavaModule = getModule();
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);

Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "module.getConstants");
Map<String, Object> map = baseJavaModule.getConstants();
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);

Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "create WritableNativeMap");
ReactMarker.logMarker(CONVERT_CONSTANTS_START, moduleName);
try {
if (moduleName == "UIManager") {
NativeMap res = UIManagerConstantsCache.getInstance().getUIManagerConstantsAsWritableMap();
if (res != null) {
return res;
}
}
return Arguments.makeNativeMap(map);
} finally {
ReactMarker.logMarker(CONVERT_CONSTANTS_END, moduleName);
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);

ReactMarker.logMarker(GET_CONSTANTS_END, moduleName);
SystraceMessage.endSection(TRACE_TAG_REACT_JAVA_BRIDGE).flush();
}
}

@DoNotStrip
public void invoke(int methodId, ReadableNativeArray parameters) {
if (methodId >= mMethods.size()) {
return;
}

mMethods.get(methodId).invoke(mJSInstance, parameters);
}
}

This file was deleted.

Loading
Loading