Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Nov 14, 2024
1 parent 43d58d7 commit 6d75c39
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 56 deletions.
118 changes: 65 additions & 53 deletions Sources/PerceptionCore/PerceptionRegistrar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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: Observable, Member>(
_ subject: Subject,
keyPath: KeyPath<Subject, Member>,
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: Observable, Member>(
_ subject: Subject,
keyPath: KeyPath<Subject, Member>,
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) {
self.registrar.access(subject, keyPath: keyPath)
}

public func withMutation<Subject: Observable, Member, T>(
of subject: Subject, keyPath: KeyPath<Subject, Member>, _ mutation: () throws -> T
) rethrows -> T {
try self.registrar.withMutation(of: subject, keyPath: keyPath, mutation)
}
public func withMutation<Subject: Observable, Member, T>(
of subject: Subject, keyPath: KeyPath<Subject, Member>, _ mutation: () throws -> T
) rethrows -> T {
try self.registrar.withMutation(of: subject, keyPath: keyPath, mutation)
}

public func willSet<Subject: Observable, Member>(
_ subject: Subject, keyPath: KeyPath<Subject, Member>
) {
self.registrar.willSet(subject, keyPath: keyPath)
}
public func willSet<Subject: Observable, Member>(
_ subject: Subject, keyPath: KeyPath<Subject, Member>
) {
self.registrar.willSet(subject, keyPath: keyPath)
}

public func didSet<Subject: Observable, Member>(
_ subject: Subject, keyPath: KeyPath<Subject, Member>
) {
self.registrar.didSet(subject, keyPath: keyPath)
public func didSet<Subject: Observable, Member>(
_ subject: Subject, keyPath: KeyPath<Subject, Member>
) {
self.registrar.didSet(subject, keyPath: keyPath)
}
}
}
#endif

extension PerceptionRegistrar {
@_disfavoredOverload
Expand All @@ -93,17 +101,19 @@ extension PerceptionRegistrar {
column: column
)
#endif
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *), !isObservationBeta {
func `open`<T: Observable>(_ subject: T) {
self.registrar.access(
subject,
keyPath: unsafeDowncast(keyPath, to: KeyPath<T, Member>.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`<T: Observable>(_ subject: T) {
self.registrar.access(
subject,
keyPath: unsafeDowncast(keyPath, to: KeyPath<T, Member>.self)
)
}
if let subject = subject as? any Observable {
return open(subject)
}
}
}
#endif
self.perceptionRegistrar.access(subject, keyPath: keyPath)
}

Expand All @@ -113,18 +123,20 @@ extension PerceptionRegistrar {
keyPath: KeyPath<Subject, Member>,
_ mutation: () throws -> T
) rethrows -> T {
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 {
return try self.registrar.withMutation(
of: subject,
keyPath: unsafeDowncast(keyPath, to: KeyPath<S, Member>.self),
mutation
)
#if canImport(Observation)
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 {
return try self.registrar.withMutation(
of: subject,
keyPath: unsafeDowncast(keyPath, to: KeyPath<S, Member>.self),
mutation
)
}
return try open(subject)
}
return try open(subject)
}
#endif
return try self.perceptionRegistrar.withMutation(of: subject, keyPath: keyPath, mutation)
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/PerceptionCore/PerceptionTracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,11 @@ public func withPerceptionTracking<T>(
_ 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())
Expand Down

0 comments on commit 6d75c39

Please sign in to comment.