Skip to content

Commit

Permalink
Fallback to Perception on iOS 17 beta builds (#66)
Browse files Browse the repository at this point in the history
* revert perception checking iOS 17.0.1 and add a boolean to allow consumers to force perception checking

* Update PerceptionChecking.swift

* Update PerceptionRegistrar.swift

* Remove configuration and use a runtime check

* Remove unused watchkit import

* Documentation

* sp

* cleanup

* Update Sources/Perception/Internal/BetaChecking.swift

* Update BetaChecking.swift

* fix

* check return value before creating cString

---------

Co-authored-by: Stephen Celis <[email protected]>
  • Loading branch information
ollieatkinson and stephencelis authored Jun 3, 2024
1 parent c78c53e commit a7d774a
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 @@ -18,7 +18,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 @@ -85,7 +85,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 @@ -107,7 +107,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 @@ -129,7 +129,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 @@ -150,7 +150,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 @@ -210,7 +210,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 @@ -44,7 +44,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

0 comments on commit a7d774a

Please sign in to comment.