Skip to content

Commit

Permalink
Let ASNodeController conform to NSLocking (TextureGroup#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
maicki authored and mikezucc committed Nov 7, 2018
1 parent 2a62124 commit f4ec172
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
- Fix mismatch in UIAccessibilityAction selector method [Michael Schneider](https://github.com/maicki) [#1169](https://github.com/TextureGroup/Texture/pull/1169)
- [ASDisplayNode] Expose default Texture-set accessibility values as properties. [Jia Wern Lim](https://github.com/jiawernlim) [#1170](https://github.com/TextureGroup/Texture/pull/1170)
- ASTableNode init method match checks from ASCollectionNode [Michael Schneider](https://github.com/maicki) [#1171]
- Add NSLocking conformance to ASNodeController [Michael Schneider](https://github.com/maicki)[#1179] (https://github.com/TextureGroup/Texture/pull/1179)

## 2.7
- Fix pager node for interface coalescing. [Max Wang](https://github.com/wsdwsd0829) [#877](https://github.com/TextureGroup/Texture/pull/877)
Expand Down
3 changes: 2 additions & 1 deletion Source/ASNodeController+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h> // for ASInterfaceState protocol

/* ASNodeController is currently beta and open to change in the future */
@interface ASNodeController<__covariant DisplayNodeType : ASDisplayNode *> : NSObject <ASInterfaceStateDelegate>
@interface ASNodeController<__covariant DisplayNodeType : ASDisplayNode *>
: NSObject <ASInterfaceStateDelegate, NSLocking>

@property (nonatomic, strong /* may be weak! */) DisplayNodeType node;

Expand Down
24 changes: 22 additions & 2 deletions Source/ASNodeController+Beta.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//

#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
#import <AsyncDisplayKit/ASNodeController+Beta.h>
#import <AsyncDisplayKit/ASThread.h>
#import <AsyncDisplayKit/ASWeakProxy.h>

#define _node (_shouldInvertStrongReference ? _weakNode : _strongNode)

@implementation ASNodeController
{
ASDisplayNode *_strongNode;
__weak ASDisplayNode *_weakNode;
ASDN::RecursiveMutex __instanceLock__;
}

- (void)loadNode
{
ASLockScopeSelf();
self.node = [[ASDisplayNode alloc] init];
}

- (ASDisplayNode *)node
{
ASLockScopeSelf();
if (_node == nil) {
[self loadNode];
}
Expand All @@ -35,6 +39,7 @@ - (ASDisplayNode *)node

- (void)setupReferencesWithNode:(ASDisplayNode *)node
{
ASLockScopeSelf();
if (_shouldInvertStrongReference) {
// The node should own the controller; weak reference from controller to node.
_weakNode = node;
Expand All @@ -51,11 +56,13 @@ - (void)setupReferencesWithNode:(ASDisplayNode *)node

- (void)setNode:(ASDisplayNode *)node
{
ASLockScopeSelf();
[self setupReferencesWithNode:node];
}

- (void)setShouldInvertStrongReference:(BOOL)shouldInvertStrongReference
{
ASLockScopeSelf();
if (_shouldInvertStrongReference != shouldInvertStrongReference) {
// Because the BOOL controls which ivar we access, get the node before toggling.
ASDisplayNode *node = _node;
Expand All @@ -82,11 +89,24 @@ - (void)interfaceStateDidChange:(ASInterfaceState)newState

- (void)hierarchyDisplayDidFinish {}

#pragma mark NSLocking

- (void)lock
{
__instanceLock__.lock();
}

- (void)unlock
{
__instanceLock__.unlock();
}

@end

@implementation ASDisplayNode (ASNodeController)

- (ASNodeController *)nodeController {
- (ASNodeController *)nodeController
{
return _weakNodeController ?: _strongNodeController;
}

Expand Down

0 comments on commit f4ec172

Please sign in to comment.