|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.fabric |
| 9 | + |
| 10 | +import android.annotation.SuppressLint |
| 11 | +import com.facebook.common.logging.FLog |
| 12 | +import com.facebook.jni.HybridClassBase |
| 13 | +import com.facebook.proguard.annotations.DoNotStripAny |
| 14 | +import com.facebook.react.bridge.NativeMap |
| 15 | +import com.facebook.react.bridge.ReadableNativeMap |
| 16 | +import com.facebook.react.bridge.WritableMap |
| 17 | +import com.facebook.react.common.mapbuffer.ReadableMapBuffer |
| 18 | +import com.facebook.react.uimanager.StateWrapper |
| 19 | + |
| 20 | +/** |
| 21 | + * This class holds reference to the C++ EventEmitter object. Instances of this class are created on |
| 22 | + * the Bindings.cpp, where the pointer to the C++ event emitter is set. |
| 23 | + */ |
| 24 | +@SuppressLint("MissingNativeLoadLibrary") |
| 25 | +@DoNotStripAny |
| 26 | +public class StateWrapperImpl private constructor() : HybridClassBase(), StateWrapper { |
| 27 | + |
| 28 | + private external fun initHybrid() |
| 29 | + |
| 30 | + private external fun getStateDataImpl(): ReadableNativeMap? |
| 31 | + |
| 32 | + private external fun getStateMapBufferDataImpl(): ReadableMapBuffer? |
| 33 | + |
| 34 | + public external fun updateStateImpl(map: NativeMap) |
| 35 | + |
| 36 | + public override val stateDataMapBuffer: ReadableMapBuffer? |
| 37 | + get() { |
| 38 | + if (!isValid) { |
| 39 | + FLog.e(TAG, "Race between StateWrapperImpl destruction and getState") |
| 40 | + return null |
| 41 | + } |
| 42 | + return getStateMapBufferDataImpl() |
| 43 | + } |
| 44 | + |
| 45 | + public override val stateData: ReadableNativeMap? |
| 46 | + get() { |
| 47 | + if (!isValid) { |
| 48 | + FLog.e(TAG, "Race between StateWrapperImpl destruction and getState") |
| 49 | + return null |
| 50 | + } |
| 51 | + return getStateDataImpl() |
| 52 | + } |
| 53 | + |
| 54 | + init { |
| 55 | + initHybrid() |
| 56 | + } |
| 57 | + |
| 58 | + override fun updateState(map: WritableMap) { |
| 59 | + if (!isValid) { |
| 60 | + FLog.e(TAG, "Race between StateWrapperImpl destruction and updateState") |
| 61 | + return |
| 62 | + } |
| 63 | + updateStateImpl(map as NativeMap) |
| 64 | + } |
| 65 | + |
| 66 | + override fun destroyState() { |
| 67 | + if (isValid) { |
| 68 | + resetNative() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + override fun toString(): String { |
| 73 | + if (!isValid) { |
| 74 | + return "<destroyed>" |
| 75 | + } |
| 76 | + |
| 77 | + val mapBuffer = getStateMapBufferDataImpl() |
| 78 | + if (mapBuffer != null) { |
| 79 | + return mapBuffer.toString() |
| 80 | + } |
| 81 | + |
| 82 | + return getStateDataImpl()?.toString() ?: "<unexpected null: stateDataImpl>" |
| 83 | + } |
| 84 | + |
| 85 | + private companion object { |
| 86 | + init { |
| 87 | + FabricSoLoader.staticInit() |
| 88 | + } |
| 89 | + |
| 90 | + private const val TAG = "StateWrapperImpl" |
| 91 | + } |
| 92 | +} |
0 commit comments