Skip to content

Commit

Permalink
Copy yogaChildren in accessor method. Avoid using accessor method int…
Browse files Browse the repository at this point in the history
…ernally.
  • Loading branch information
maicki committed Dec 13, 2018
1 parent c6fc05d commit 11f8a13
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Source/ASDisplayNode+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ AS_EXTERN void ASDisplayNodePerformBlockOnEveryYogaChild(ASDisplayNode * _Nullab
@interface ASDisplayNode (Yoga)

// TODO: Make this and yogaCalculatedLayout atomic (lock).
@property (nullable, nonatomic) NSArray *yogaChildren;
@property (nonatomic, copy) NSArray *yogaChildren;

- (void)addYogaChild:(ASDisplayNode *)child;
- (void)removeYogaChild:(ASDisplayNode *)child;
Expand Down
7 changes: 4 additions & 3 deletions Source/ASDisplayNode+Yoga.mm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ - (void)setYogaChildren:(NSArray *)yogaChildren

- (NSArray *)yogaChildren
{
return _yogaChildren;
return [_yogaChildren copy] ?: @[];
}

- (void)addYogaChild:(ASDisplayNode *)child
Expand Down Expand Up @@ -168,8 +168,9 @@ - (void)setupYogaCalculatedLayout

YGNodeRef yogaNode = self.style.yogaNode;
uint32_t childCount = YGNodeGetChildCount(yogaNode);
ASDisplayNodeAssert(childCount == self.yogaChildren.count,
@"Yoga tree should always be in sync with .yogaNodes array! %@", self.yogaChildren);
ASDisplayNodeAssert(childCount == _yogaChildren.count,
@"Yoga tree should always be in sync with .yogaNodes array! %@",
_yogaChildren);

ASLayout *rawSublayouts[childCount];
int i = 0;
Expand Down
4 changes: 3 additions & 1 deletion Source/Layout/ASYogaUtilities.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ void ASDisplayNodePerformBlockOnEveryYogaChild(ASDisplayNode *node, void(^block)
return;
}
block(node);
for (ASDisplayNode *child in [node yogaChildren]) {
// We use the accessor here despite the copy, because the block may modify the yoga tree e.g.
// replacing a node.
for (ASDisplayNode *child in node.yogaChildren) {
ASDisplayNodePerformBlockOnEveryYogaChild(child, block);
}
}
Expand Down

0 comments on commit 11f8a13

Please sign in to comment.