Convert StateWrapperImpl.java to Kotlin (#50615)

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

Convert Java to Kotlin

Changelog:
[Internal]

Reviewed By: cortinico

Differential Revision: D72752437

fbshipit-source-id: 7c437087f161538ab20ce9d123d7526b98cf4d90
This commit is contained in:
Alan Lee
2025-04-10 03:17:02 -07:00
committed by Facebook GitHub Bot
parent 07a1fb8e6b
commit ca2b2aac60
3 changed files with 94 additions and 105 deletions
@@ -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 {
@@ -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 "<destroyed>";
}
ReadableMapBuffer mapBuffer = getStateMapBufferDataImpl();
if (mapBuffer != null) {
return mapBuffer.toString();
}
ReadableNativeMap map = getStateDataImpl();
if (map == null) {
return "<unexpected null>";
}
return map.toString();
}
}
@@ -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 "<destroyed>"
}
val mapBuffer = getStateMapBufferDataImpl()
if (mapBuffer != null) {
return mapBuffer.toString()
}
return getStateDataImpl()?.toString() ?: "<unexpected null: stateDataImpl>"
}
private companion object {
init {
FabricSoLoader.staticInit()
}
private const val TAG = "StateWrapperImpl"
}
}