|
| 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.uimanager; |
| 9 | + |
| 10 | +import androidx.annotation.Nullable; |
| 11 | +import com.facebook.common.logging.FLog; |
| 12 | +import com.facebook.react.bridge.ReadableMap; |
| 13 | +import com.facebook.react.bridge.WritableMap; |
| 14 | + |
| 15 | +/** |
| 16 | + * This is a helper base class for ViewGroups that use Fabric State. |
| 17 | + * |
| 18 | + * <p>Reason to use this: UpdateState calls from the View layer to the Fabric core can fail, and |
| 19 | + * optionally Fabric will call a "failure callback" if that happens. This class abstracts that and |
| 20 | + * makes it easier ensure that State in Fabric is always up-to-date. |
| 21 | + * |
| 22 | + * <p>1. Whenever ViewManager.updateState is called, call View.setStateWrapper. 2. Instead of |
| 23 | + * calling StateWrapper.updateState directly, call View.setState and it will automatically keep |
| 24 | + * retrying the UpdateState call until it succeeds; or you call setState again; or the View layer is |
| 25 | + * updated with a newer StateWrapper. |
| 26 | + */ |
| 27 | +@Deprecated( |
| 28 | + since = |
| 29 | + "Deprecated class since v0.73.0, please use com.facebook.react.uimanager.StateWrapper instead.", |
| 30 | + forRemoval = true) |
| 31 | +public class FabricViewStateManager { |
| 32 | + private static final String TAG = "FabricViewStateManager"; |
| 33 | + |
| 34 | + @Deprecated |
| 35 | + public interface HasFabricViewStateManager { |
| 36 | + FabricViewStateManager getFabricViewStateManager(); |
| 37 | + } |
| 38 | + |
| 39 | + @Deprecated |
| 40 | + public interface StateUpdateCallback { |
| 41 | + WritableMap getStateUpdate(); |
| 42 | + } |
| 43 | + |
| 44 | + @Nullable private StateWrapper mStateWrapper = null; |
| 45 | + |
| 46 | + @Deprecated |
| 47 | + public void setStateWrapper(StateWrapper stateWrapper) { |
| 48 | + mStateWrapper = stateWrapper; |
| 49 | + } |
| 50 | + |
| 51 | + @Deprecated |
| 52 | + public boolean hasStateWrapper() { |
| 53 | + return mStateWrapper != null; |
| 54 | + } |
| 55 | + |
| 56 | + private void setState( |
| 57 | + @Nullable final StateWrapper stateWrapper, |
| 58 | + final StateUpdateCallback stateUpdateCallback, |
| 59 | + final int numTries) { |
| 60 | + // The StateWrapper will change, breaking the async loop, whenever the UpdateState MountItem |
| 61 | + // is executed. |
| 62 | + // The caller is responsible for detecting if data is up-to-date, and doing nothing, or |
| 63 | + // detecting if state is stale and calling setState again. |
| 64 | + if (stateWrapper == null) { |
| 65 | + FLog.e(TAG, "setState called without a StateWrapper"); |
| 66 | + return; |
| 67 | + } |
| 68 | + if (stateWrapper != mStateWrapper) { |
| 69 | + return; |
| 70 | + } |
| 71 | + // We bail out after an arbitrary number of tries. In practice this should never go higher |
| 72 | + // than 2 or 3, but there's nothing guaranteeing that. |
| 73 | + if (numTries > 60) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + @Nullable WritableMap stateUpdate = stateUpdateCallback.getStateUpdate(); |
| 78 | + if (stateUpdate == null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // TODO: State update cannot fail; remove `failureRunnable` and custom retrying logic. |
| 83 | + stateWrapper.updateState(stateUpdate); |
| 84 | + } |
| 85 | + |
| 86 | + @Deprecated |
| 87 | + public void setState(final StateUpdateCallback stateUpdateCallback) { |
| 88 | + setState(mStateWrapper, stateUpdateCallback, 0); |
| 89 | + } |
| 90 | + |
| 91 | + @Deprecated |
| 92 | + public @Nullable ReadableMap getStateData() { |
| 93 | + return mStateWrapper != null ? mStateWrapper.getStateData() : null; |
| 94 | + } |
| 95 | +} |
0 commit comments