Skip to content

Commit

Permalink
Change the name
Browse files Browse the repository at this point in the history
  • Loading branch information
Adlai-Holler committed Jan 11, 2019
1 parent fda582d commit 48b5d00
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Source/ASDisplayNode+Subclasses.h
Expand Up @@ -76,14 +76,14 @@ AS_CATEGORY_IMPLEMENTABLE
* node initialization behavior. This method will be called by [ASDisplayNode init].
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)globalInit;
- (void)baseDidInit;

/**
* An empty method that you can implement in a category to add global
* node deallocation behavior. This method will be called by [ASDisplayNode dealloc].
*/
AS_CATEGORY_IMPLEMENTABLE
- (void)globalDealloc;
- (void)baseWillDealloc;

#pragma mark - Layout
/** @name Layout */
Expand Down
6 changes: 3 additions & 3 deletions Source/ASDisplayNode.h
Expand Up @@ -97,10 +97,10 @@ AS_EXTERN NSInteger const ASDefaultDrawingPriority;
@interface ASDisplayNode : NSObject <ASLocking> {
@public
/**
* A field for clients to use as they please, for example in
* implementing category methods. Example usage can be found in CatDealsCollectionView.
* A field for clients to use as they please, for example in implementing category methods on
* the base class. Example usage can be found in CatDealsCollectionView.
*/
void *_userInfo;
void *_context;
}

/** @name Initializing a node object */
Expand Down
9 changes: 4 additions & 5 deletions Source/ASDisplayNode.mm
Expand Up @@ -259,8 +259,8 @@ + (void)initialize
// Note: addMethod will not do anything if a method already exists.
if (self == ASDisplayNode.class) {
IMP noArgsImp = (IMP)StubImplementationWithNoArgs;
class_addMethod(self, @selector(globalInit), noArgsImp, "v@:");
class_addMethod(self, @selector(globalDealloc), noArgsImp, "v@:");
class_addMethod(self, @selector(baseDidInit), noArgsImp, "v@:");
class_addMethod(self, @selector(baseWillDealloc), noArgsImp, "v@:");
class_addMethod(self, @selector(didLoad), noArgsImp, "v@:");
class_addMethod(self, @selector(layoutDidFinish), noArgsImp, "v@:");
class_addMethod(self, @selector(didEnterPreloadState), noArgsImp, "v@:");
Expand Down Expand Up @@ -342,7 +342,7 @@ - (void)_initializeInstance
_automaticallyRelayoutOnSafeAreaChanges = NO;
_automaticallyRelayoutOnLayoutMarginsChanges = NO;

[self globalInit];
[self baseDidInit];
ASDisplayNodeLogEvent(self, @"init");
}

Expand Down Expand Up @@ -467,6 +467,7 @@ - (void)onDidLoad:(ASDisplayNodeDidLoadBlock)body
- (void)dealloc
{
_flags.isDeallocating = YES;
[self baseWillDealloc];

// Synchronous nodes may not be able to call the hierarchy notifications, so only enforce for regular nodes.
ASDisplayNodeAssert(checkFlag(Synchronous) || !ASInterfaceStateIncludesVisible(_interfaceState), @"Node should always be marked invisible before deallocating. Node: %@", self);
Expand All @@ -487,8 +488,6 @@ - (void)dealloc

// TODO: Remove this? If supernode isn't already nil, this method isn't dealloc-safe anyway.
[self _setSupernode:nil];

[self globalDealloc];
}

#pragma mark - Loading
Expand Down
Expand Up @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface ASDisplayNode (CatDeals)

@property (nullable, nonatomic) NSString *catsLoggingID;
@property (nullable, copy) NSString *catsLoggingID;

@end

Expand Down
39 changes: 21 additions & 18 deletions examples/CatDealsCollectionView/Sample/ASDisplayNode+CatDeals.mm
Expand Up @@ -7,52 +7,55 @@

#import "ASDisplayNode+CatDeals.h"

struct NodeProps {
#import <AsyncDisplayKit/ASThread.h>

// A place to store info on any display node in this app.
struct CatDealsNodeContext {
NSString *loggingID = nil;
};

#define _props ((NodeProps &)*((NodeProps *)_userInfo))
// Convenience to cast _context into our struct reference.
NS_INLINE CatDealsNodeContext &GetNodeContext(ASDisplayNode *node) {
return *static_cast<CatDealsNodeContext *>(node->_context);
}

@implementation ASDisplayNode (CatDeals)

- (void)globalInit
- (void)baseDidInit
{
_userInfo = new NodeProps{};
_context = new CatDealsNodeContext;
}

- (void)globalDealloc
- (void)baseWillDealloc
{
delete &_props;
delete &GetNodeContext(self);
}

- (void)setCatsLoggingID:(NSString *)catsLoggingID
{
_props.loggingID = catsLoggingID;
NSString *copy = [catsLoggingID copy];
ASLockScopeSelf();
GetNodeContext(self).loggingID = copy;
}

- (NSString *)catsLoggingID
{
return _props.loggingID;
ASLockScopeSelf();
return GetNodeContext(self).loggingID;
}

- (void)didEnterVisibleState
{
if (_props.loggingID) {
NSLog(@"Visible: %@", _props.loggingID);
if (NSString *loggingID = self.catsLoggingID) {
NSLog(@"Visible: %@", loggingID);
}
}

- (void)didExitVisibleState
{
if (_props.loggingID) {
NSLog(@"NotVisible: %@", _props.loggingID);
if (NSString *loggingID = self.catsLoggingID) {
NSLog(@"NotVisible: %@", loggingID);
}
}

// A method wrapper for the _props macro. Useful in debugger `p [self catProps]`.
- (NodeProps &)catProps
{
return _props;
}

@end

0 comments on commit 48b5d00

Please sign in to comment.