@@ -24,7 +24,11 @@ public struct PerceptionRegistrar: Sendable {
24
24
/// of a type.
25
25
public init ( isPerceptionCheckingEnabled: Bool = PerceptionCore . isPerceptionCheckingEnabled) {
26
26
if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta {
27
- self . _rawValue = AnySendable ( ObservationRegistrar ( ) )
27
+ #if canImport(Observation)
28
+ self . _rawValue = AnySendable ( ObservationRegistrar ( ) )
29
+ #else
30
+ self . _rawValue = AnySendable ( _PerceptionRegistrar ( ) )
31
+ #endif
28
32
} else {
29
33
self . _rawValue = AnySendable ( _PerceptionRegistrar ( ) )
30
34
}
@@ -33,47 +37,51 @@ public struct PerceptionRegistrar: Sendable {
33
37
#endif
34
38
}
35
39
36
- @available ( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * )
37
- private var registrar : ObservationRegistrar {
38
- self . _rawValue. base as! ObservationRegistrar
39
- }
40
+ #if canImport(Observation)
41
+ @available ( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * )
42
+ private var registrar : ObservationRegistrar {
43
+ self . _rawValue. base as! ObservationRegistrar
44
+ }
45
+ #endif
40
46
41
47
private var perceptionRegistrar : _PerceptionRegistrar {
42
48
self . _rawValue. base as! _PerceptionRegistrar
43
49
}
44
50
}
45
51
46
- @available ( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * )
47
- extension PerceptionRegistrar {
48
- public func access< Subject: Observable , Member> (
49
- _ subject: Subject ,
50
- keyPath: KeyPath < Subject , Member > ,
51
- fileID: StaticString = #fileID,
52
- filePath: StaticString = #filePath,
53
- line: UInt = #line,
54
- column: UInt = #column
55
- ) {
56
- self . registrar. access ( subject, keyPath: keyPath)
57
- }
52
+ #if canImport(Observation)
53
+ @available ( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * )
54
+ extension PerceptionRegistrar {
55
+ public func access< Subject: Observable , Member> (
56
+ _ subject: Subject ,
57
+ keyPath: KeyPath < Subject , Member > ,
58
+ fileID: StaticString = #fileID,
59
+ filePath: StaticString = #filePath,
60
+ line: UInt = #line,
61
+ column: UInt = #column
62
+ ) {
63
+ self . registrar. access ( subject, keyPath: keyPath)
64
+ }
58
65
59
- public func withMutation< Subject: Observable , Member, T> (
60
- of subject: Subject , keyPath: KeyPath < Subject , Member > , _ mutation: ( ) throws -> T
61
- ) rethrows -> T {
62
- try self . registrar. withMutation ( of: subject, keyPath: keyPath, mutation)
63
- }
66
+ public func withMutation< Subject: Observable , Member, T> (
67
+ of subject: Subject , keyPath: KeyPath < Subject , Member > , _ mutation: ( ) throws -> T
68
+ ) rethrows -> T {
69
+ try self . registrar. withMutation ( of: subject, keyPath: keyPath, mutation)
70
+ }
64
71
65
- public func willSet< Subject: Observable , Member> (
66
- _ subject: Subject , keyPath: KeyPath < Subject , Member >
67
- ) {
68
- self . registrar. willSet ( subject, keyPath: keyPath)
69
- }
72
+ public func willSet< Subject: Observable , Member> (
73
+ _ subject: Subject , keyPath: KeyPath < Subject , Member >
74
+ ) {
75
+ self . registrar. willSet ( subject, keyPath: keyPath)
76
+ }
70
77
71
- public func didSet< Subject: Observable , Member> (
72
- _ subject: Subject , keyPath: KeyPath < Subject , Member >
73
- ) {
74
- self . registrar. didSet ( subject, keyPath: keyPath)
78
+ public func didSet< Subject: Observable , Member> (
79
+ _ subject: Subject , keyPath: KeyPath < Subject , Member >
80
+ ) {
81
+ self . registrar. didSet ( subject, keyPath: keyPath)
82
+ }
75
83
}
76
- }
84
+ #endif
77
85
78
86
extension PerceptionRegistrar {
79
87
@_disfavoredOverload
@@ -93,17 +101,19 @@ extension PerceptionRegistrar {
93
101
column: column
94
102
)
95
103
#endif
96
- if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta {
97
- func `open`< T: Observable > ( _ subject: T ) {
98
- self . registrar. access (
99
- subject,
100
- keyPath: unsafeDowncast ( keyPath, to: KeyPath< T, Member> . self )
101
- )
102
- }
103
- if let subject = subject as? any Observable {
104
- return open ( subject)
104
+ #if canImport(Observation)
105
+ if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta {
106
+ func `open`< T: Observable > ( _ subject: T ) {
107
+ self . registrar. access (
108
+ subject,
109
+ keyPath: unsafeDowncast ( keyPath, to: KeyPath< T, Member> . self )
110
+ )
111
+ }
112
+ if let subject = subject as? any Observable {
113
+ return open ( subject)
114
+ }
105
115
}
106
- }
116
+ #endif
107
117
self . perceptionRegistrar. access ( subject, keyPath: keyPath)
108
118
}
109
119
@@ -113,18 +123,20 @@ extension PerceptionRegistrar {
113
123
keyPath: KeyPath < Subject , Member > ,
114
124
_ mutation: ( ) throws -> T
115
125
) rethrows -> T {
116
- if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta,
117
- let subject = subject as? any Observable
118
- {
119
- func `open`< S: Observable > ( _ subject: S ) throws -> T {
120
- return try self . registrar. withMutation (
121
- of: subject,
122
- keyPath: unsafeDowncast ( keyPath, to: KeyPath< S, Member> . self ) ,
123
- mutation
124
- )
126
+ #if canImport(Observation)
127
+ if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta,
128
+ let subject = subject as? any Observable
129
+ {
130
+ func `open`< S: Observable > ( _ subject: S ) throws -> T {
131
+ return try self . registrar. withMutation (
132
+ of: subject,
133
+ keyPath: unsafeDowncast ( keyPath, to: KeyPath< S, Member> . self ) ,
134
+ mutation
135
+ )
136
+ }
137
+ return try open ( subject)
125
138
}
126
- return try open ( subject)
127
- }
139
+ #endif
128
140
return try self . perceptionRegistrar. withMutation ( of: subject, keyPath: keyPath, mutation)
129
141
}
130
142
@@ -133,17 +145,19 @@ extension PerceptionRegistrar {
133
145
_ subject: Subject ,
134
146
keyPath: KeyPath < Subject , Member >
135
147
) {
136
- if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta,
137
- let subject = subject as? any Observable
138
- {
139
- func `open`< S: Observable > ( _ subject: S ) {
140
- return self . registrar. willSet (
141
- subject,
142
- keyPath: unsafeDowncast ( keyPath, to: KeyPath< S, Member> . self )
143
- )
148
+ #if canImport(Observation)
149
+ if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta,
150
+ let subject = subject as? any Observable
151
+ {
152
+ func `open`< S: Observable > ( _ subject: S ) {
153
+ return self . registrar. willSet (
154
+ subject,
155
+ keyPath: unsafeDowncast ( keyPath, to: KeyPath< S, Member> . self )
156
+ )
157
+ }
158
+ return open ( subject)
144
159
}
145
- return open ( subject)
146
- }
160
+ #endif
147
161
return self . perceptionRegistrar. willSet ( subject, keyPath: keyPath)
148
162
}
149
163
@@ -152,17 +166,19 @@ extension PerceptionRegistrar {
152
166
_ subject: Subject ,
153
167
keyPath: KeyPath < Subject , Member >
154
168
) {
155
- if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta,
156
- let subject = subject as? any Observable
157
- {
158
- func `open`< S: Observable > ( _ subject: S ) {
159
- return self . registrar. didSet (
160
- subject,
161
- keyPath: unsafeDowncast ( keyPath, to: KeyPath< S, Member> . self )
162
- )
169
+ #if canImport(Observation)
170
+ if #available( iOS 17 , macOS 14 , tvOS 17 , watchOS 10 , * ) , !isObservationBeta,
171
+ let subject = subject as? any Observable
172
+ {
173
+ func `open`< S: Observable > ( _ subject: S ) {
174
+ return self . registrar. didSet (
175
+ subject,
176
+ keyPath: unsafeDowncast ( keyPath, to: KeyPath< S, Member> . self )
177
+ )
178
+ }
179
+ return open ( subject)
163
180
}
164
- return open ( subject)
165
- }
181
+ #endif
166
182
return self . perceptionRegistrar. didSet ( subject, keyPath: keyPath)
167
183
}
168
184
}
0 commit comments