Skip to content

Commit

Permalink
Add method to cancel layout transitions in progress
Browse files Browse the repository at this point in the history
- Also make sure a transition isn't invalidated right after it passed the validation test and before it proceeds
  • Loading branch information
nguyenhuy committed Mar 21, 2016
1 parent 1a57e2c commit 9e76d7b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions AsyncDisplayKit/ASDisplayNode+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ ASDISPLAYNODE_EXTERN_C_END
*/
- (BOOL)placeholderShouldPersist;

/**
* @abstract Cancels all performing layout transitions. Can be called on any thread.
*/
- (void)cancelLayoutTransitionsInProgress;

@end
28 changes: 18 additions & 10 deletions AsyncDisplayKit/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -593,14 +593,7 @@ - (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
return _layout;
}

if ([self _hasTransitionsInProgress]) {
// Invalidate transition sentinel to cancel transitions in progress
[self _invalidateTransitionSentinel];
// Tell subnodes to exit layout pending state and clear related properties
ASDisplayNodePerformBlockOnEverySubnode(self, ^(ASDisplayNode * _Nonnull node) {
node.hierarchyState &= (~ASHierarchyStateLayoutPending);
});
}
[self cancelLayoutTransitionsInProgress];

ASLayout *previousLayout = _layout;
ASSizeRange previousConstrainedSize = _constrainedSize;
Expand Down Expand Up @@ -698,12 +691,14 @@ - (void)transitionLayoutWithSizeRange:(ASSizeRange)constrainedSize
}

ASPerformBlockOnMainThread(^{
// Grab _propertyLock here to make sure this transition isn't invalidated
// right after it passed the validation test and before it proceeds
ASDN::MutexLocker l(_propertyLock);

if ([self _shouldAbortTransitionWithID:transitionID]) {
return;
}

ASDN::MutexLocker l(_propertyLock);

ASLayout *previousLayout = _layout;
ASSizeRange previousConstrainedSize = _constrainedSize;
[self applyLayout:newLayout constrainedSize:constrainedSize layoutContext:nil];
Expand Down Expand Up @@ -769,6 +764,19 @@ - (void)calculatedLayoutDidChange
// subclass override
}

- (void)cancelLayoutTransitionsInProgress
{
ASDN::MutexLocker l(_propertyLock);
if ([self _hasTransitionsInProgress]) {
// Invalidate transition sentinel to cancel transitions in progress
[self _invalidateTransitionSentinel];
// Tell subnodes to exit layout pending state and clear related properties
ASDisplayNodePerformBlockOnEverySubnode(self, ^(ASDisplayNode * _Nonnull node) {
node.hierarchyState &= (~ASHierarchyStateLayoutPending);
});
}
}

#pragma mark - Layout Transition

- (BOOL)usesImplicitHierarchyManagement
Expand Down

0 comments on commit 9e76d7b

Please sign in to comment.