Skip to content

Commit

Permalink
Add ability to gate Specs Duplicate State Update detection via Compon…
Browse files Browse the repository at this point in the history
…entsConfiguration

Summary: In this diff I do the integration of the gating for the state update detection.

Reviewed By: adityasharat

Differential Revision: D51073754

fbshipit-source-id: d83aa172d4a30042aa4d8697e9ec82a2a3709b56
  • Loading branch information
Fabio Carballo authored and facebook-github-bot committed Nov 15, 2023
1 parent b3ceb97 commit 9b20471
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ private static LithoConfiguration mergeConfigurationWithNewLogTagAndLogger(
logger != null ? logger : lithoConfiguration.logger,
lithoConfiguration.renderUnitIdGenerator,
lithoConfiguration.visibilityBoundsTransformer,
lithoConfiguration.debugEventListener);
lithoConfiguration.debugEventListener,
lithoConfiguration.isSpecsDuplicateStateUpdateDetectionEnabled);
}

public static LithoConfiguration buildDefaultLithoConfiguration(
Expand Down Expand Up @@ -118,6 +119,7 @@ public static LithoConfiguration buildDefaultLithoConfiguration(
loggerToUse,
null,
transformer,
null);
null,
ComponentsConfiguration.defaultInstance.specsApiStateUpdateDuplicateDetectionEnabled);
}
}
21 changes: 18 additions & 3 deletions litho-core/src/main/java/com/facebook/litho/ComponentTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ protected ComponentTree(Builder builder) {
logger,
renderUnitIdGenerator,
builder.visibilityBoundsTransformer,
builder.componentTreeDebugEventListener);
builder.componentTreeDebugEventListener,
builder.config.specsApiStateUpdateDuplicateDetectionEnabled);

if (ComponentsConfiguration.enableFixForNestedComponentTree) {
mContext =
Expand Down Expand Up @@ -1138,6 +1139,10 @@ public LithoConfiguration getLithoConfiguration() {
return mContext.mLithoConfiguration;
}

private boolean isSpecsDuplicateStateUpdateDetectionEnabled() {
return mContext.mLithoConfiguration.isSpecsDuplicateStateUpdateDetectionEnabled;
}

/** Returns whether incremental mount is enabled or not in this component. */
public boolean isIncrementalMountEnabled() {
return mContext.mLithoConfiguration.incrementalMountEnabled;
Expand Down Expand Up @@ -1308,7 +1313,12 @@ public void updateStateSync(

if (mTreeState != null) {
isStateEnqueued =
mTreeState.queueStateUpdate(componentKey, stateUpdate, false, isLayoutState);
mTreeState.queueStateUpdate(
componentKey,
stateUpdate,
false,
isLayoutState,
!isSpecsDuplicateStateUpdateDetectionEnabled());
}
}

Expand Down Expand Up @@ -1342,7 +1352,12 @@ public void updateStateAsync(

if (mTreeState != null) {
isStateUpdateEnqueued =
mTreeState.queueStateUpdate(componentKey, stateUpdate, false, isLayoutState);
mTreeState.queueStateUpdate(
componentKey,
stateUpdate,
false,
isLayoutState,
!isSpecsDuplicateStateUpdateDetectionEnabled());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ data class LithoConfiguration(
@JvmField val logger: ComponentsLogger? = null,
@JvmField val renderUnitIdGenerator: RenderUnitIdGenerator?,
@JvmField val visibilityBoundsTransformer: VisibilityBoundsTransformer?,
@JvmField val debugEventListener: ComponentTreeDebugEventListener?
@JvmField val debugEventListener: ComponentTreeDebugEventListener?,
@JvmField val isSpecsDuplicateStateUpdateDetectionEnabled: Boolean
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ import com.facebook.rendercore.incrementalmount.IncrementalMountExtensionConfigs
*/
data class ComponentsConfiguration
internal constructor(
/**
* This determines if the [ComponentTree] attached to this configuration, will attempt to detect
* and ignore duplicate state updates coming from usages in the Specs API.
*/
@JvmField val specsApiStateUpdateDuplicateDetectionEnabled: Boolean = false,
val useCancellableLayoutFutures: Boolean = true,
val useInterruptibleResolution: Boolean = true,
val shouldCacheLayouts: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private List<? extends Class<? extends LithoTestRunConfiguration>> getGlobalConf
return Arrays.asList(
LayoutCachingTestRunConfiguration.class,
SkipRootCheckingTestRunConfiguration.class,
CustomBindersTestRunConfiguration.class);
CustomBindersTestRunConfiguration.class,
SpecsStateUpdateDetectionConfiguration.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.litho.testing.testrunner

import com.facebook.litho.config.ComponentsConfiguration
import org.junit.runners.model.FrameworkMethod

class SpecsStateUpdateDetectionConfiguration : LithoTestRunConfiguration {

private val defaultInstance = ComponentsConfiguration.defaultInstance

override fun beforeTest(method: FrameworkMethod) {
ComponentsConfiguration.defaultInstance =
defaultInstance.copy(specsApiStateUpdateDuplicateDetectionEnabled = true)
}

override fun afterTest(method: FrameworkMethod) {
ComponentsConfiguration.defaultInstance = defaultInstance
}
}

0 comments on commit 9b20471

Please sign in to comment.