Skip to content

Commit

Permalink
Pass LayoutStateFuture to ComponentContext
Browse files Browse the repository at this point in the history
Summary: We'll need this to inquire during resolving a layout whether the current LayoutStateFuture has been cancelled.

Reviewed By: pasqualeanatriello

Differential Revision: D14748110

fbshipit-source-id: ad9a529078f31cdf7421837e7f61c58a5d1f3d92
  • Loading branch information
mihaelao authored and facebook-github-bot committed Apr 5, 2019
1 parent 418066c commit 28a6e3b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
15 changes: 12 additions & 3 deletions litho-core/src/main/java/com/facebook/litho/ComponentContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public interface YogaNodeFactory {
@ThreadConfined(ThreadConfined.ANY)
private int mDefStyleAttr = 0;

private ComponentTree.LayoutStateFuture mLayoutStateFuture;

public ComponentContext(
Context context,
@Nullable String logTag,
Expand Down Expand Up @@ -104,7 +106,8 @@ public ComponentContext(
ComponentContext context,
@Nullable StateHandler stateHandler,
@Nullable KeyHandler keyHandler,
@Nullable TreeProps treeProps) {
@Nullable TreeProps treeProps,
@Nullable ComponentTree.LayoutStateFuture layoutStateFuture) {

mContext = context.getAndroidContext();
mResourceCache = context.mResourceCache;
Expand All @@ -122,6 +125,7 @@ public ComponentContext(
mStateHandler = stateHandler != null ? stateHandler : context.mStateHandler;
mKeyHandler = keyHandler != null ? keyHandler : context.mKeyHandler;
mTreeProps = treeProps != null ? treeProps : context.mTreeProps;
mLayoutStateFuture = layoutStateFuture == null ? context.mLayoutStateFuture : layoutStateFuture;
}

public ComponentContext(
Expand Down Expand Up @@ -163,7 +167,12 @@ public ComponentContext(Context context, YogaNodeFactory yogaNodeFactory) {
}

public ComponentContext(ComponentContext context) {
this(context, context.mStateHandler, context.mKeyHandler, context.mTreeProps);
this(
context,
context.mStateHandler,
context.mKeyHandler,
context.mTreeProps,
context.mLayoutStateFuture);
}

public ComponentContext(Context context) {
Expand Down Expand Up @@ -446,7 +455,7 @@ void applyStyle(InternalNode node, @AttrRes int defStyleAttr, @StyleRes int defS

static ComponentContext withComponentTree(ComponentContext context, ComponentTree componentTree) {
ComponentContext componentContext =
new ComponentContext(context, new StateHandler(), null, null);
new ComponentContext(context, new StateHandler(), null, null, null);
componentContext.mComponentTree = componentTree;

return componentContext;
Expand Down
23 changes: 19 additions & 4 deletions litho-core/src/main/java/com/facebook/litho/ComponentTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public void run() {

private final boolean mUseSharedLayoutStateFuture;
private final Object mLayoutStateFutureLock = new Object();
private final boolean mUseCancelableLayoutFutures;

@GuardedBy("mLayoutStateFutureLock")
private final List<LayoutStateFuture> mLayoutStateFutures = new ArrayList<>();
Expand Down Expand Up @@ -278,6 +279,7 @@ protected ComponentTree(Builder builder) {
mNestedTreeResolutionExperimentEnabled = builder.nestedTreeResolutionExperimentEnabled;
mUseSharedLayoutStateFuture = builder.useSharedLayoutStateFuture;
mIsPersistenceEnabled = builder.isPersistenceEnabled;
mUseCancelableLayoutFutures = ComponentsConfiguration.useCancelableLayoutFutures;

ensureLayoutThreadHandler();

Expand Down Expand Up @@ -2038,7 +2040,8 @@ public void handleMessage(Message msg) {
previousLayoutState,
treeProps,
source,
extraAttribution);
extraAttribution,
null);
}
}

Expand All @@ -2051,7 +2054,8 @@ private LayoutState calculateLayoutStateInternal(
@Nullable LayoutState previousLayoutState,
@Nullable TreeProps treeProps,
@CalculateLayoutSource int source,
@Nullable String extraAttribution) {
@Nullable String extraAttribution,
@Nullable LayoutStateFuture layoutStateFuture) {
final ComponentContext contextWithStateHandler;

synchronized (this) {
Expand All @@ -2062,7 +2066,11 @@ private LayoutState calculateLayoutStateInternal(

contextWithStateHandler =
new ComponentContext(
context, StateHandler.createNewInstance(mStateHandler), keyHandler, treeProps);
context,
StateHandler.createNewInstance(mStateHandler),
keyHandler,
treeProps,
layoutStateFuture);
}

return LayoutState.calculate(
Expand Down Expand Up @@ -2142,7 +2150,10 @@ private LayoutStateFuture(
previousLayoutState,
treeProps,
source,
extraAttribution);
extraAttribution,
ComponentTree.this.mUseSharedLayoutStateFuture
? LayoutStateFuture.this
: null);
synchronized (LayoutStateFuture.this) {
if (released) {
return null;
Expand All @@ -2163,6 +2174,10 @@ private synchronized void release() {
released = true;
}

boolean isReleased() {
return released;
}

void unregisterForResponse() {
refCount--;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestComponentContext extends ComponentContext {
}

TestComponentContext(ComponentContext c, StateHandler stateHandler) {
super(c, stateHandler, null, null);
super(c, stateHandler, null, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public class ComponentsConfiguration {
/** If true, information about RenderInfos will be passed to Flipper's layout inspector. */
public static boolean enableRenderInfoDebugging = false;

public static boolean useCancelableLayoutFutures;

public static boolean isRenderInfoDebuggingEnabled() {
return isDebugModeEnabled && enableRenderInfoDebugging;
}
Expand Down

0 comments on commit 28a6e3b

Please sign in to comment.