Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to Perception on iOS 17 beta builds #66

Merged
27 changes: 27 additions & 0 deletions Sources/Perception/Internal/BetaChecking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

// NB: This boolean is used to work around a crash experienced by beta users of Observation when
// `Observable` was still a marker protocol and we attempt to dynamically cast to
// `any Observable`.
let isObservationBeta: Bool = {
#if os(iOS) || os(tvOS) || os(watchOS)
let os = ProcessInfo.processInfo.operatingSystemVersion
#if os(iOS) || os(tvOS)
if (os.majorVersion, os.minorVersion, os.patchVersion) != (17, 0, 0) {
return false
}
#elseif os(watchOS)
if (os.majorVersion, os.minorVersion, os.patchVersion) != (10, 0, 0) {
return false
}
#endif
var size = 0
sysctlbyname("kern.osversion", nil, &size, nil, 0)
var version = [CChar](repeating: 0, count: size)
let ret = sysctlbyname("kern.osversion", &version, &size, nil, 0)
// NB: Beta builds end with a lowercase character (_e.g._, '21A5277j')
return ret == 0 ? String(cString: version).last?.isLowercase == true : false
#else
return false
#endif
}()
10 changes: 5 additions & 5 deletions Sources/Perception/PerceptionRegistrar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct PerceptionRegistrar: Sendable {
/// ``Perception/Perceptible()`` macro to indicate observably
/// of a type.
public init(isPerceptionCheckingEnabled: Bool = Perception.isPerceptionCheckingEnabled) {
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) {
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta {
#if canImport(Observation)
self._rawValue = AnySendable(ObservationRegistrar())
#else
Expand Down Expand Up @@ -90,7 +90,7 @@ extension PerceptionRegistrar {
self.perceptionCheck(file: file, line: line)
#endif
#if canImport(Observation)
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) {
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta {
func `open`<T: Observable>(_ subject: T) {
self.registrar.access(
subject,
Expand All @@ -112,7 +112,7 @@ extension PerceptionRegistrar {
_ mutation: () throws -> T
) rethrows -> T {
#if canImport(Observation)
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *),
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta,
let subject = subject as? any Observable
{
func `open`<S: Observable>(_ subject: S) throws -> T {
Expand All @@ -134,7 +134,7 @@ extension PerceptionRegistrar {
keyPath: KeyPath<Subject, Member>
) {
#if canImport(Observation)
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *),
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta,
let subject = subject as? any Observable
{
func `open`<S: Observable>(_ subject: S) {
Expand All @@ -155,7 +155,7 @@ extension PerceptionRegistrar {
keyPath: KeyPath<Subject, Member>
) {
#if canImport(Observation)
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *),
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta,
let subject = subject as? any Observable
{
func `open`<S: Observable>(_ subject: S) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Perception/PerceptionTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public func withPerceptionTracking<T>(
onChange: @autoclosure () -> @Sendable () -> Void
) -> T {
#if canImport(Observation)
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) {
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta {
return withObservationTracking(apply, onChange: onChange())
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Sources/Perception/WithPerceptionTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
let content: () -> Content

public var body: Content {
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) {
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta {
return self.instrumentedBody()
} else {
// NB: View will not re-render when 'id' changes unless we access it in the view.
Expand Down
Loading