forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASNodeController+Beta.mm
138 lines (112 loc) · 2.95 KB
/
ASNodeController+Beta.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// ASNodeController+Beta.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASDisplayNodeInternal.h>
#import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
#import <AsyncDisplayKit/ASNodeController+Beta.h>
#import <AsyncDisplayKit/ASThread.h>
#define _node (_shouldInvertStrongReference ? _weakNode : _strongNode)
@implementation ASNodeController
{
ASDisplayNode *_strongNode;
__weak ASDisplayNode *_weakNode;
AS::RecursiveMutex __instanceLock__;
}
- (void)loadNode
{
ASLockScopeSelf();
self.node = [[ASDisplayNode alloc] init];
}
- (ASDisplayNode *)node
{
ASLockScopeSelf();
if (_node == nil) {
[self loadNode];
}
return _node;
}
- (void)setupReferencesWithNode:(ASDisplayNode *)node
{
ASLockScopeSelf();
if (_shouldInvertStrongReference) {
// The node should own the controller; weak reference from controller to node.
_weakNode = node;
_strongNode = nil;
} else {
// The controller should own the node; weak reference from node to controller.
_strongNode = node;
_weakNode = nil;
}
[node __setNodeController:self];
}
- (void)setNode:(ASDisplayNode *)node
{
ASLockScopeSelf();
if (node == _node) {
return;
}
[self setupReferencesWithNode:node];
[node addInterfaceStateDelegate:self];
}
- (void)setShouldInvertStrongReference:(BOOL)shouldInvertStrongReference
{
ASLockScopeSelf();
if (_shouldInvertStrongReference != shouldInvertStrongReference) {
// Because the BOOL controls which ivar we access, get the node before toggling.
ASDisplayNode *node = _node;
_shouldInvertStrongReference = shouldInvertStrongReference;
[self setupReferencesWithNode:node];
}
}
// subclass overrides
- (void)nodeDidLoad {}
- (void)nodeDidLayout {}
- (void)nodeWillCalculateLayout:(ASSizeRange)constrainedSize {}
- (void)didEnterVisibleState {}
- (void)didExitVisibleState {}
- (void)didEnterDisplayState {}
- (void)didExitDisplayState {}
- (void)didEnterPreloadState {}
- (void)didExitPreloadState {}
- (void)interfaceStateDidChange:(ASInterfaceState)newState
fromState:(ASInterfaceState)oldState {}
- (void)hierarchyDisplayDidFinish {}
- (void)didEnterHierarchy {}
- (void)didExitHierarchy {}
- (ASLockSet)lockPair {
ASLockSet lockSet = ASLockSequence(^BOOL(ASAddLockBlock addLock) {
if (!addLock(_node)) {
return NO;
}
if (!addLock(self)) {
return NO;
}
return YES;
});
return lockSet;
}
#pragma mark NSLocking
- (void)lock
{
__instanceLock__.lock();
}
- (void)unlock
{
__instanceLock__.unlock();
}
- (BOOL)tryLock
{
return __instanceLock__.try_lock();
}
@end
@implementation ASDisplayNode (ASNodeController)
- (ASNodeController *)nodeController
{
return _weakNodeController ?: _strongNodeController;
}
@end