Skip to content

Commit

Permalink
Fix the Initialization Hierarchy (#3207)
Browse files Browse the repository at this point in the history
* Fix the initializers setup

* Update Swift example

* Remove incorrect comment

* Add one to ASTableNode
  • Loading branch information
Adlai Holler authored Mar 28, 2017
1 parent 27c9406 commit fcbbea5
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 88 deletions.
2 changes: 2 additions & 0 deletions Source/ASCollectionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ NS_ASSUME_NONNULL_BEGIN
*/
@interface ASCollectionNode : ASDisplayNode <ASRangeControllerUpdateRangeProtocol>

- (instancetype)init NS_UNAVAILABLE;

/**
* Initializes an ASCollectionNode
*
Expand Down
25 changes: 7 additions & 18 deletions Source/ASCollectionNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ - (void)setCollectionViewClass:(Class)collectionViewClass
}
}

- (instancetype)init
{
ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER();
UICollectionViewLayout *nilLayout = nil;
self = [self initWithCollectionViewLayout:nilLayout]; // Will throw an exception for lacking a UICV Layout.
return nil;
}

- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout
{
return [self initWithFrame:CGRectZero collectionViewLayout:layout layoutFacilitator:nil];
Expand All @@ -143,17 +135,14 @@ - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionVi

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout layoutFacilitator:(id<ASCollectionViewLayoutFacilitatorProtocol>)layoutFacilitator
{
__weak __typeof__(self) weakSelf = self;
ASDisplayNodeViewBlock collectionViewBlock = ^UIView *{
// Variable will be unused if event logging is off.
__unused __typeof__(self) strongSelf = weakSelf;
return [[[strongSelf collectionViewClass] alloc] _initWithFrame:frame collectionViewLayout:layout layoutFacilitator:layoutFacilitator eventLog:ASDisplayNodeGetEventLog(strongSelf)];
};

if (self = [super initWithViewBlock:collectionViewBlock didLoadBlock:nil]) {
return self;
if (self = [super init]) {
__weak __typeof__(self) weakSelf = self;
[self setViewBlock:^{
__typeof__(self) strongSelf = weakSelf;
return [[[strongSelf collectionViewClass] alloc] _initWithFrame:frame collectionViewLayout:layout layoutFacilitator:layoutFacilitator eventLog:ASDisplayNodeGetEventLog(strongSelf)];
}];
}
return nil;
return self;
}

#pragma mark ASDisplayNode
Expand Down
24 changes: 23 additions & 1 deletion Source/ASDisplayNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ extern NSInteger const ASDefaultDrawingPriority;
* @return An ASDisplayNode instance whose view will be a subclass that enables asynchronous rendering, and passes
* through -layout and touch handling methods.
*/
- (instancetype)init;
- (instancetype)init NS_DESIGNATED_INITIALIZER;


/**
Expand Down Expand Up @@ -183,6 +183,28 @@ extern NSInteger const ASDefaultDrawingPriority;
*/
- (void)onDidLoad:(ASDisplayNodeDidLoadBlock)body;

/**
* Set the block that should be used to load this node's view.
*
* @param viewBlock The block that creates a view for this node.
*
* @precondition The node is not yet loaded.
*
* @note You will usually NOT call this. See the limitations documented in @c initWithViewBlock:
*/
- (void)setViewBlock:(ASDisplayNodeViewBlock)viewBlock;

/**
* Set the block that should be used to load this node's layer.
*
* @param viewBlock The block that creates a layer for this node.
*
* @precondition The node is not yet loaded.
*
* @note You will usually NOT call this. See the limitations documented in @c initWithLayerBlock:
*/
- (void)setLayerBlock:(ASDisplayNodeLayerBlock)layerBlock;

/**
* @abstract Returns whether the node is synchronous.
*
Expand Down
63 changes: 31 additions & 32 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,11 @@ - (instancetype)init

- (instancetype)initWithViewClass:(Class)viewClass
{
if (!(self = [super init]))
if (!(self = [self init]))
return nil;

ASDisplayNodeAssert([viewClass isSubclassOfClass:[UIView class]], @"should initialize with a subclass of UIView");

[self _initializeInstance];
_viewClass = viewClass;
_flags.synchronous = ![viewClass isSubclassOfClass:[_ASDisplayView class]];

Expand All @@ -330,13 +329,12 @@ - (instancetype)initWithViewClass:(Class)viewClass

- (instancetype)initWithLayerClass:(Class)layerClass
{
if (!(self = [super init])) {
if (!(self = [self init])) {
return nil;
}

ASDisplayNodeAssert([layerClass isSubclassOfClass:[CALayer class]], @"should initialize with a subclass of CALayer");

[self _initializeInstance];
_layerClass = layerClass;
_flags.synchronous = ![layerClass isSubclassOfClass:[_ASDisplayLayer class]];
_flags.layerBacked = YES;
Expand All @@ -346,60 +344,61 @@ - (instancetype)initWithLayerClass:(Class)layerClass

- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock
{
return [self _initWithViewBlock:viewBlock didLoadBlock:nil];
}
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{
return [self _initWithViewBlock:viewBlock didLoadBlock:didLoadBlock];
return [self initWithViewBlock:viewBlock didLoadBlock:nil];
}

- (instancetype)_initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{
if (!(self = [super init])) {
if (!(self = [self init])) {
return nil;
}

ASDisplayNodeAssertNotNil(viewBlock, @"should initialize with a valid block that returns a UIView");

[self _initializeInstance];
_viewBlock = viewBlock;
_flags.synchronous = YES;

[self setViewBlock:viewBlock];
if (didLoadBlock != nil) {
_onDidLoadBlocks = [NSMutableArray arrayWithObject:didLoadBlock];
[self onDidLoad:didLoadBlock];
}

return self;
}

- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock
{
return [self _initWithLayerBlock:layerBlock didLoadBlock:nil];
return [self initWithLayerBlock:layerBlock didLoadBlock:nil];
}

- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{
return [self _initWithLayerBlock:layerBlock didLoadBlock:didLoadBlock];
}

- (instancetype)_initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock
{
if (!(self = [super init])) {
if (!(self = [self init])) {
return nil;
}

ASDisplayNodeAssertNotNil(layerBlock, @"should initialize with a valid block that returns a CALayer");

[self _initializeInstance];
_layerBlock = layerBlock;
_flags.synchronous = YES;
_flags.layerBacked = YES;
[self setLayerBlock:layerBlock];
if (didLoadBlock != nil) {
_onDidLoadBlocks = [NSMutableArray arrayWithObject:didLoadBlock];
[self onDidLoad:didLoadBlock];
}

return self;
}

- (void)setViewBlock:(ASDisplayNodeViewBlock)viewBlock
{
ASDisplayNodeAssertFalse(self.nodeLoaded);
ASDisplayNodeAssertNotNil(viewBlock, @"should initialize with a valid block that returns a UIView");

_viewBlock = viewBlock;
_flags.synchronous = YES;
}

- (void)setLayerBlock:(ASDisplayNodeLayerBlock)layerBlock
{
ASDisplayNodeAssertFalse(self.nodeLoaded);
ASDisplayNodeAssertNotNil(layerBlock, @"should initialize with a valid block that returns a CALayer");

_layerBlock = layerBlock;
_flags.synchronous = YES;
_flags.layerBacked = YES;
}

- (void)onDidLoad:(ASDisplayNodeDidLoadBlock)body
{
ASDN::MutexLocker l(__instanceLock__);
Expand Down
5 changes: 4 additions & 1 deletion Source/ASScrollNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ @implementation ASScrollNode

- (instancetype)init
{
return [super initWithViewBlock:^UIView *{ return [[ASScrollView alloc] init]; }];
if (self = [super init]) {
[self setViewBlock:^UIView *{ return [[ASScrollView alloc] init]; }];
}
return self;
}

- (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
Expand Down
2 changes: 1 addition & 1 deletion Source/ASTableNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface ASTableNode : ASDisplayNode <ASRangeControllerUpdateRangeProtocol>

- (instancetype)init; // UITableViewStylePlain
- (instancetype)initWithStyle:(UITableViewStyle)style;
- (instancetype)initWithStyle:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER;

@property (strong, nonatomic, readonly) ASTableView *view;

Expand Down
38 changes: 10 additions & 28 deletions Source/ASTableNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,40 +72,22 @@ @implementation ASTableNode

#pragma mark Lifecycle

- (instancetype)_initWithTableView:(ASTableView *)tableView
{
// Avoid a retain cycle. In this case, the ASTableView is creating us, and strongly retains us.
ASTableView * __weak weakTableView = tableView;
if (self = [super initWithViewBlock:^UIView *{ return weakTableView; }]) {
__unused __weak ASTableView *view = [self view];
return self;
}
return nil;
}

- (instancetype)_initWithFrame:(CGRect)frame style:(UITableViewStyle)style dataControllerClass:(Class)dataControllerClass
{
__weak __typeof__(self) weakSelf = self;
ASDisplayNodeViewBlock tableViewBlock = ^UIView *{
// Variable will be unused if event logging is off.
__unused __typeof__(self) strongSelf = weakSelf;
return [[ASTableView alloc] _initWithFrame:frame style:style dataControllerClass:dataControllerClass eventLog:ASDisplayNodeGetEventLog(strongSelf)];
};

if (self = [super initWithViewBlock:tableViewBlock]) {
return self;
}
return nil;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self _initWithFrame:CGRectZero style:style dataControllerClass:nil];
if (self = [super init]) {
__weak __typeof__(self) weakSelf = self;
[self setViewBlock:^{
// Variable will be unused if event logging is off.
__unused __typeof__(self) strongSelf = weakSelf;
return [[ASTableView alloc] _initWithFrame:CGRectZero style:style dataControllerClass:nil eventLog:ASDisplayNodeGetEventLog(strongSelf)];
}];
}
return self;
}

- (instancetype)init
{
return [self _initWithFrame:CGRectZero style:UITableViewStylePlain dataControllerClass:nil];
return [self initWithStyle:UITableViewStylePlain];
}

#pragma mark ASDisplayNode
Expand Down
4 changes: 0 additions & 4 deletions Source/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ - (void)prepareForReuse
#pragma mark -
#pragma mark ASTableView

@interface ASTableNode ()
- (instancetype)_initWithTableView:(ASTableView *)tableView;
@end

@interface ASTableView () <ASRangeControllerDataSource, ASRangeControllerDelegate, ASDataControllerSource, _ASTableViewCellDelegate, ASCellNodeInteractionDelegate, ASDelegateProxyInterceptor, ASBatchFetchingScrollView, ASDataControllerEnvironmentDelegate>
{
ASTableViewProxy *_proxyDataSource;
Expand Down
4 changes: 2 additions & 2 deletions Source/Private/ASDisplayNodeInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ FOUNDATION_EXPORT NSString * const ASRenderingEngineDidDisplayNodesScheduledBefo
- (void)displayImmediately;

/// Alternative initialiser for backing with a custom view class. Supports asynchronous display with _ASDisplayView subclasses.
- (nullable instancetype)initWithViewClass:(Class)viewClass;
- (instancetype)initWithViewClass:(Class)viewClass;

/// Alternative initialiser for backing with a custom layer class. Supports asynchronous display with _ASDisplayLayer subclasses.
- (nullable instancetype)initWithLayerClass:(Class)layerClass;
- (instancetype)initWithLayerClass:(Class)layerClass;

@property (nonatomic, assign) CGFloat contentsScaleForDisplay;

Expand Down
5 changes: 4 additions & 1 deletion examples/Swift/Sample/TailLoadingCellNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ final class SpinnerNode: ASDisplayNode {
}

override init() {
super.init(viewBlock: { UIActivityIndicatorView(activityIndicatorStyle: .Gray) }, didLoadBlock: nil)
super.init()
setViewBlock {
UIActivityIndicatorView(activityIndicatorStyle: .Gray)
}

// Set spinner node to default size of the activitiy indicator view
self.style.preferredSize = CGSizeMake(20.0, 20.0)
Expand Down

0 comments on commit fcbbea5

Please sign in to comment.