Skip to content

Commit

Permalink
Merge pull request #1 from monzo/oliver/revert-ios-17-0-1-check-and-f…
Browse files Browse the repository at this point in the history
…orcePerceptionChecking

Fallback to Perception on iOS 17 beta builds
  • Loading branch information
ollieatkinson authored May 8, 2024
2 parents 7f62e5f + c4bf256 commit b27471f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
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 @@ -28,7 +28,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 @@ -95,7 +95,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 @@ -118,7 +118,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 @@ -143,7 +143,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 @@ -165,7 +165,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 @@ -219,7 +219,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 @@ -48,7 +48,7 @@ public struct WithPerceptionTracking<Content> {
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

0 comments on commit b27471f

Please sign in to comment.