Skip to content

Commit

Permalink
Adds InternalNode#freeze() API
Browse files Browse the repository at this point in the history
Summary:
Litho needs a new API which is called immediately before yoga begins layout calculations so that the InternalNode gets the opportunity to finalise itself; i.e. perform the last mutations and in effect avoid any more mutations to the hierarchy.

See D26373731 where the mutations from `Layout#collectResults` is moved back into the InternalNode.

Changelog: [Internal]  Adds new API to YogaNodeJNIBase

Reviewed By: SidharthGuglani

Differential Revision: D26373730

fbshipit-source-id: 471346d3444986ada91e86c95f5f9fb98bcd2fa6
  • Loading branch information
adityasharat authored and facebook-github-bot committed Feb 16, 2021
1 parent 651c527 commit de36e85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 12 additions & 6 deletions java/com/facebook/yoga/YogaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
import javax.annotation.Nullable;

public abstract class YogaNode {

/** The interface the {@link #getData()} object can optionally implement. */
public interface Inputs {

/** Requests the data object to disable mutations of its inputs. */
void freeze();
}

public abstract void reset();

public abstract int getChildCount();
Expand All @@ -25,12 +33,10 @@ public abstract class YogaNode {
public abstract YogaNode removeChildAt(int i);

/**
* @returns the {@link YogaNode} that owns this {@link YogaNode}.
* The owner is used to identify the YogaTree that a {@link YogaNode} belongs
* to.
* This method will return the parent of the {@link YogaNode} when the
* {@link YogaNode} only belongs to one YogaTree or null when the
* {@link YogaNode} is shared between two or more YogaTrees.
* @returns the {@link YogaNode} that owns this {@link YogaNode}. The owner is used to identify
* the YogaTree that a {@link YogaNode} belongs to. This method will return the parent of the
* {@link YogaNode} when the {@link YogaNode} only belongs to one YogaTree or null when the
* {@link YogaNode} is shared between two or more YogaTrees.
*/
@Nullable
public abstract YogaNode getOwner();
Expand Down
14 changes: 13 additions & 1 deletion java/com/facebook/yoga/YogaNodeJNIBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,17 @@ public void calculateLayout(float width, float height) {
long[] nativePointers = null;
YogaNodeJNIBase[] nodes = null;

freeze();

ArrayList<YogaNodeJNIBase> n = new ArrayList<>();
n.add(this);
for (int i = 0; i < n.size(); ++i) {
List<YogaNodeJNIBase> children = n.get(i).mChildren;
if (children != null) {
n.addAll(children);
for (YogaNodeJNIBase child : children) {
child.freeze();
n.add(child);
}
}
}

Expand All @@ -215,6 +220,13 @@ public void calculateLayout(float width, float height) {
YogaNative.jni_YGNodeCalculateLayoutJNI(mNativePointer, width, height, nativePointers, nodes);
}

private void freeze() {
Object data = getData();
if (data instanceof Inputs) {
((Inputs) data).freeze();
}
}

public void dirty() {
YogaNative.jni_YGNodeMarkDirtyJNI(mNativePointer);
}
Expand Down

0 comments on commit de36e85

Please sign in to comment.