Skip to content

Commit b345e06

Browse files
committed
Add a specialized API for SwiftUI fast path access
1 parent 5fb2530 commit b345e06

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

stdlib/public/Observation/Sources/Observation/ObservationTracking.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct ObservationTracking {
4646
}
4747
}
4848

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 {
5050
var accessList: AccessList?
5151
let result = withUnsafeMutablePointer(to: &accessList) { ptr in
5252
ThreadLocal.value = UnsafeMutableRawPointer(ptr)
@@ -55,6 +55,7 @@ public struct ObservationTracking {
5555
}
5656
if let list = accessList {
5757
let state = ManagedCriticalState([ObjectIdentifier: Int]())
58+
let onChange = onChange()
5859
let values = list.entries.mapValues { $0.addObserver {
5960
onChange()
6061
let values = state.withCriticalRegion { $0 }
@@ -66,4 +67,17 @@ public struct ObservationTracking {
6667
}
6768
return result
6869
}
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+
}
6983
}

0 commit comments

Comments
 (0)