diff --git a/Sources/PerceptionCore/PerceptionRegistrar.swift b/Sources/PerceptionCore/PerceptionRegistrar.swift index d9924b4..335537f 100644 --- a/Sources/PerceptionCore/PerceptionRegistrar.swift +++ b/Sources/PerceptionCore/PerceptionRegistrar.swift @@ -24,7 +24,11 @@ public struct PerceptionRegistrar: Sendable { /// of a type. public init(isPerceptionCheckingEnabled: Bool = PerceptionCore.isPerceptionCheckingEnabled) { if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta { - self._rawValue = AnySendable(ObservationRegistrar()) + #if canImport(Observation) + self._rawValue = AnySendable(ObservationRegistrar()) + #else + self._rawValue = AnySendable(_PerceptionRegistrar()) + #endif } else { self._rawValue = AnySendable(_PerceptionRegistrar()) } @@ -33,47 +37,51 @@ public struct PerceptionRegistrar: Sendable { #endif } - @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) - private var registrar: ObservationRegistrar { - self._rawValue.base as! ObservationRegistrar - } + #if canImport(Observation) + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + private var registrar: ObservationRegistrar { + self._rawValue.base as! ObservationRegistrar + } + #endif private var perceptionRegistrar: _PerceptionRegistrar { self._rawValue.base as! _PerceptionRegistrar } } -@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) -extension PerceptionRegistrar { - public func access( - _ subject: Subject, - keyPath: KeyPath, - fileID: StaticString = #fileID, - filePath: StaticString = #filePath, - line: UInt = #line, - column: UInt = #column - ) { - self.registrar.access(subject, keyPath: keyPath) - } +#if canImport(Observation) + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + extension PerceptionRegistrar { + public func access( + _ subject: Subject, + keyPath: KeyPath, + fileID: StaticString = #fileID, + filePath: StaticString = #filePath, + line: UInt = #line, + column: UInt = #column + ) { + self.registrar.access(subject, keyPath: keyPath) + } - public func withMutation( - of subject: Subject, keyPath: KeyPath, _ mutation: () throws -> T - ) rethrows -> T { - try self.registrar.withMutation(of: subject, keyPath: keyPath, mutation) - } + public func withMutation( + of subject: Subject, keyPath: KeyPath, _ mutation: () throws -> T + ) rethrows -> T { + try self.registrar.withMutation(of: subject, keyPath: keyPath, mutation) + } - public func willSet( - _ subject: Subject, keyPath: KeyPath - ) { - self.registrar.willSet(subject, keyPath: keyPath) - } + public func willSet( + _ subject: Subject, keyPath: KeyPath + ) { + self.registrar.willSet(subject, keyPath: keyPath) + } - public func didSet( - _ subject: Subject, keyPath: KeyPath - ) { - self.registrar.didSet(subject, keyPath: keyPath) + public func didSet( + _ subject: Subject, keyPath: KeyPath + ) { + self.registrar.didSet(subject, keyPath: keyPath) + } } -} +#endif extension PerceptionRegistrar { @_disfavoredOverload @@ -93,17 +101,19 @@ extension PerceptionRegistrar { column: column ) #endif - if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta { - func `open`(_ subject: T) { - self.registrar.access( - subject, - keyPath: unsafeDowncast(keyPath, to: KeyPath.self) - ) - } - if let subject = subject as? any Observable { - return open(subject) + #if canImport(Observation) + if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta { + func `open`(_ subject: T) { + self.registrar.access( + subject, + keyPath: unsafeDowncast(keyPath, to: KeyPath.self) + ) + } + if let subject = subject as? any Observable { + return open(subject) + } } - } + #endif self.perceptionRegistrar.access(subject, keyPath: keyPath) } @@ -113,18 +123,20 @@ extension PerceptionRegistrar { keyPath: KeyPath, _ mutation: () throws -> T ) rethrows -> T { - if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta, - let subject = subject as? any Observable - { - func `open`(_ subject: S) throws -> T { - return try self.registrar.withMutation( - of: subject, - keyPath: unsafeDowncast(keyPath, to: KeyPath.self), - mutation - ) + #if canImport(Observation) + if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta, + let subject = subject as? any Observable + { + func `open`(_ subject: S) throws -> T { + return try self.registrar.withMutation( + of: subject, + keyPath: unsafeDowncast(keyPath, to: KeyPath.self), + mutation + ) + } + return try open(subject) } - return try open(subject) - } + #endif return try self.perceptionRegistrar.withMutation(of: subject, keyPath: keyPath, mutation) } diff --git a/Sources/PerceptionCore/PerceptionTracking.swift b/Sources/PerceptionCore/PerceptionTracking.swift index 53b36e7..1a31f19 100644 --- a/Sources/PerceptionCore/PerceptionTracking.swift +++ b/Sources/PerceptionCore/PerceptionTracking.swift @@ -209,9 +209,11 @@ public func withPerceptionTracking( _ apply: () -> T, onChange: @autoclosure () -> @Sendable () -> Void ) -> T { - if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta { - return withObservationTracking(apply, onChange: onChange()) - } + #if canImport(Observation) + if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta { + return withObservationTracking(apply, onChange: onChange()) + } + #endif let (result, accessList) = generateAccessList(apply) if let accessList { PerceptionTracking._installTracking(accessList, onChange: onChange())