@@ -46,7 +46,7 @@ public struct ObservationTracking {
46
46
}
47
47
}
48
48
49
- public static func withTracking< T> ( _ apply: ( ) -> T , onChange: @Sendable @ escaping ( ) -> Void ) -> T {
49
+ public static func withTracking< T> ( _ apply: ( ) -> T , onChange: @autoclosure ( ) -> @ Sendable ( ) -> Void ) -> T {
50
50
var accessList : AccessList ?
51
51
let result = withUnsafeMutablePointer ( to: & accessList) { ptr in
52
52
ThreadLocal . value = UnsafeMutableRawPointer ( ptr)
@@ -55,6 +55,7 @@ public struct ObservationTracking {
55
55
}
56
56
if let list = accessList {
57
57
let state = ManagedCriticalState ( [ ObjectIdentifier: Int] ( ) )
58
+ let onChange = onChange ( )
58
59
let values = list. entries. mapValues { $0. addObserver {
59
60
onChange ( )
60
61
let values = state. withCriticalRegion { $0 }
@@ -66,4 +67,17 @@ public struct ObservationTracking {
66
67
}
67
68
return result
68
69
}
70
+
71
+ public static func _installTracking( _ storage: UnsafeRawPointer , onChange: @escaping @Sendable ( ) -> Void ) {
72
+ let list = unsafeBitCast ( storage, to: AccessList . self)
73
+ let state = ManagedCriticalState ( [ ObjectIdentifier: Int] ( ) )
74
+ let values = list. entries. mapValues { $0. addObserver {
75
+ onChange ( )
76
+ let values = state. withCriticalRegion { $0 }
77
+ for (id, token) in values {
78
+ list. entries [ id] ? . removeObserver ( token)
79
+ }
80
+ } }
81
+ state. withCriticalRegion { $0 = values }
82
+ }
69
83
}
0 commit comments