Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NSLocking conformance to ASNodeController #1179

Merged
merged 1 commit into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any risk of deadlock if the node is reaching out to the node controller around the same time?

_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