diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index f9e542d9100..d6a73c31079 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -2389,13 +2389,13 @@ public final class com/facebook/react/fabric/FabricUIManagerProviderImpl : com/f public fun createUIManager (Lcom/facebook/react/bridge/ReactApplicationContext;)Lcom/facebook/react/bridge/UIManager; } -public class com/facebook/react/fabric/StateWrapperImpl : com/facebook/jni/HybridClassBase, com/facebook/react/uimanager/StateWrapper { +public final class com/facebook/react/fabric/StateWrapperImpl : com/facebook/jni/HybridClassBase, com/facebook/react/uimanager/StateWrapper { public fun destroyState ()V public fun getStateData ()Lcom/facebook/react/bridge/ReadableNativeMap; public fun getStateDataMapBuffer ()Lcom/facebook/react/common/mapbuffer/ReadableMapBuffer; public fun toString ()Ljava/lang/String; public fun updateState (Lcom/facebook/react/bridge/WritableMap;)V - public fun updateStateImpl (Lcom/facebook/react/bridge/NativeMap;)V + public final fun updateStateImpl (Lcom/facebook/react/bridge/NativeMap;)V } public final class com/facebook/react/fabric/events/EventBeatManager : com/facebook/jni/HybridClassBase, com/facebook/react/uimanager/events/BatchEventDispatchedListener { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/StateWrapperImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/StateWrapperImpl.java deleted file mode 100644 index cd2cbfecec8..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/StateWrapperImpl.java +++ /dev/null @@ -1,103 +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.fabric; - -import android.annotation.SuppressLint; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import com.facebook.common.logging.FLog; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.jni.HybridClassBase; -import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.NativeMap; -import com.facebook.react.bridge.ReadableNativeMap; -import com.facebook.react.bridge.WritableMap; -import com.facebook.react.common.mapbuffer.ReadableMapBuffer; -import com.facebook.react.uimanager.StateWrapper; - -/** - * This class holds reference to the C++ EventEmitter object. Instances of this class are created on - * the Bindings.cpp, where the pointer to the C++ event emitter is set. - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -@SuppressLint("MissingNativeLoadLibrary") -@DoNotStrip -public class StateWrapperImpl extends HybridClassBase implements StateWrapper { - static { - FabricSoLoader.staticInit(); - } - - private static final String TAG = "StateWrapperImpl"; - - private StateWrapperImpl() { - initHybrid(); - } - - private native void initHybrid(); - - private native ReadableNativeMap getStateDataImpl(); - - private native ReadableMapBuffer getStateMapBufferDataImpl(); - - public native void updateStateImpl(@NonNull NativeMap map); - - @Override - @Nullable - public ReadableMapBuffer getStateDataMapBuffer() { - if (!isValid()) { - FLog.e(TAG, "Race between StateWrapperImpl destruction and getState"); - return null; - } - return getStateMapBufferDataImpl(); - } - - @Override - @Nullable - public ReadableNativeMap getStateData() { - if (!isValid()) { - FLog.e(TAG, "Race between StateWrapperImpl destruction and getState"); - return null; - } - return getStateDataImpl(); - } - - @Override - public void updateState(@NonNull WritableMap map) { - if (!isValid()) { - FLog.e(TAG, "Race between StateWrapperImpl destruction and updateState"); - return; - } - updateStateImpl((NativeMap) map); - } - - @Override - public void destroyState() { - if (isValid()) { - resetNative(); - } - } - - @Override - public String toString() { - if (!isValid()) { - return ""; - } - - ReadableMapBuffer mapBuffer = getStateMapBufferDataImpl(); - if (mapBuffer != null) { - return mapBuffer.toString(); - } - - ReadableNativeMap map = getStateDataImpl(); - if (map == null) { - return ""; - } - - return map.toString(); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/StateWrapperImpl.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/StateWrapperImpl.kt new file mode 100644 index 00000000000..4c51ff3a762 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/StateWrapperImpl.kt @@ -0,0 +1,92 @@ +/* + * 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.fabric + +import android.annotation.SuppressLint +import com.facebook.common.logging.FLog +import com.facebook.jni.HybridClassBase +import com.facebook.proguard.annotations.DoNotStripAny +import com.facebook.react.bridge.NativeMap +import com.facebook.react.bridge.ReadableNativeMap +import com.facebook.react.bridge.WritableMap +import com.facebook.react.common.mapbuffer.ReadableMapBuffer +import com.facebook.react.uimanager.StateWrapper + +/** + * This class holds reference to the C++ EventEmitter object. Instances of this class are created on + * the Bindings.cpp, where the pointer to the C++ event emitter is set. + */ +@SuppressLint("MissingNativeLoadLibrary") +@DoNotStripAny +public class StateWrapperImpl private constructor() : HybridClassBase(), StateWrapper { + + private external fun initHybrid() + + private external fun getStateDataImpl(): ReadableNativeMap? + + private external fun getStateMapBufferDataImpl(): ReadableMapBuffer? + + public external fun updateStateImpl(map: NativeMap) + + public override val stateDataMapBuffer: ReadableMapBuffer? + get() { + if (!isValid) { + FLog.e(TAG, "Race between StateWrapperImpl destruction and getState") + return null + } + return getStateMapBufferDataImpl() + } + + public override val stateData: ReadableNativeMap? + get() { + if (!isValid) { + FLog.e(TAG, "Race between StateWrapperImpl destruction and getState") + return null + } + return getStateDataImpl() + } + + init { + initHybrid() + } + + override fun updateState(map: WritableMap) { + if (!isValid) { + FLog.e(TAG, "Race between StateWrapperImpl destruction and updateState") + return + } + updateStateImpl(map as NativeMap) + } + + override fun destroyState() { + if (isValid) { + resetNative() + } + } + + override fun toString(): String { + if (!isValid) { + return "" + } + + val mapBuffer = getStateMapBufferDataImpl() + if (mapBuffer != null) { + return mapBuffer.toString() + } + + return getStateDataImpl()?.toString() ?: "" + } + + private companion object { + init { + FabricSoLoader.staticInit() + } + + private const val TAG = "StateWrapperImpl" + } +}