Swiftgram/Source/ASDisplayNode+Yoga.h
Kevin 1410b29b63 Lock up to yogaRoot during layout to avoid deadlocks. (#1356)
* Lock up to yogaRoot during layout to avoid dead lock.

1) lock to root for tree
2) lock self to change parent (& consequently root)
3) Implement ASLocking (tryLock) on ASNodeController
4) add lockPair to try-lock node & controller together
5) lock controllers if they exist in lockToRoot...

Disable some asserts due to lock to root. :(

LL# No commands remaining.

* Add macro so non-Yoga still builds :)

* wut
2019-03-03 08:05:01 -08:00

99 lines
2.7 KiB
Objective-C

//
// ASDisplayNode+Yoga.h
// Texture
//
// Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASAvailability.h>
#if YOGA
NS_ASSUME_NONNULL_BEGIN
@class ASLayout;
AS_EXTERN void ASDisplayNodePerformBlockOnEveryYogaChild(ASDisplayNode * _Nullable node, void(^block)(ASDisplayNode *node));
@interface ASDisplayNode (Yoga)
@property (copy) NSArray *yogaChildren;
- (void)addYogaChild:(ASDisplayNode *)child;
- (void)removeYogaChild:(ASDisplayNode *)child;
- (void)insertYogaChild:(ASDisplayNode *)child atIndex:(NSUInteger)index;
- (void)semanticContentAttributeDidChange:(UISemanticContentAttribute)attribute;
@property BOOL yogaLayoutInProgress;
// TODO: Make this atomic (lock).
@property (nullable, nonatomic) ASLayout *yogaCalculatedLayout;
// Will walk up the Yoga tree and returns the root node
- (ASDisplayNode *)yogaRoot;
/**
* @discussion Attempts(spinning) to lock all node up to root node when yoga is enabled.
* This will lock self when yoga is not enabled;
*/
- (ASLockSet)lockToRootIfNeededForLayout;
@end
// These methods are intended to be used internally to Texture, and should not be called directly.
@interface ASDisplayNode (YogaInternal)
/// For internal usage only
- (BOOL)shouldHaveYogaMeasureFunc;
/// For internal usage only
- (ASLayout *)calculateLayoutYoga:(ASSizeRange)constrainedSize;
/// For internal usage only
- (void)calculateLayoutFromYogaRoot:(ASSizeRange)rootConstrainedSize;
/// For internal usage only
- (void)invalidateCalculatedYogaLayout;
/**
* @discussion return true only when yoga enabled and the node is in yoga tree and the node is
* not leaf that implemented measure function.
*/
- (BOOL)locked_shouldLayoutFromYogaRoot;
@end
@interface ASDisplayNode (YogaDebugging)
- (NSString *)yogaTreeDescription;
@end
@interface ASLayoutElementStyle (Yoga)
- (YGNodeRef)yogaNodeCreateIfNeeded;
- (void)destroyYogaNode;
@property (readonly) YGNodeRef yogaNode;
@property ASStackLayoutDirection flexDirection;
@property YGDirection direction;
@property ASStackLayoutJustifyContent justifyContent;
@property ASStackLayoutAlignItems alignItems;
@property YGPositionType positionType;
@property ASEdgeInsets position;
@property ASEdgeInsets margin;
@property ASEdgeInsets padding;
@property ASEdgeInsets border;
@property CGFloat aspectRatio;
@property YGWrap flexWrap;
@end
NS_ASSUME_NONNULL_END
// When Yoga is enabled, there are several points where we want to lock the tree to the root but otherwise (without Yoga)
// will want to simply lock self.
#define ASScopedLockSelfOrToRoot() ASScopedLockSet lockSet = [self lockToRootIfNeededForLayout]
#else
#define ASScopedLockSelfOrToRoot() ASLockScopeSelf()
#endif