-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathNodeFactory.swift
46 lines (35 loc) · 1.19 KB
/
NodeFactory.swift
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
import Foundation
import AsyncDisplayKit
fileprivate var _storageKey: Void?
extension ASDisplayNode {
private var storage: NSMapTable<NSString, ASDisplayNode> {
if let associated = objc_getAssociatedObject(self, &_storageKey) as? NSMapTable<NSString, ASDisplayNode> {
return associated
} else {
let associated = NSMapTable<NSString, ASDisplayNode>.strongToWeakObjects()
objc_setAssociatedObject(self, &_storageKey, associated, .OBJC_ASSOCIATION_RETAIN)
return associated
}
}
/**
[Still experimental]
Creates an instance of node at once, and it will be associated with the receiver node.
It requires to enable `automaticallyManagesSubnodes`.
*/
public func _makeNode(
file: StaticString = #file,
line: UInt = #line,
column: UInt = #column,
_ make: () -> ASDisplayNode
) -> ASDisplayNode {
assert(automaticallyManagesSubnodes, "Use _makeNode, must to use `automaticallyManagesSubnodes`.")
let key = "\(file),\(line),\(column)" as NSString
if let node = storage.object(forKey: key) {
return node
} else {
let newNode = make()
storage.setObject(newNode, forKey: key)
return newNode
}
}
}