Skip to content

Commit

Permalink
Don't use Observation on non-Darwin platforms
Browse files Browse the repository at this point in the history
Workaround for swiftlang/swift#75670
  • Loading branch information
lhoward committed Dec 1, 2024
1 parent 201e5fc commit 76567c8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Sources/SwiftOCA/OCC/ControlClasses/Root.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import AsyncExtensions
import Foundation
#if canImport(Darwin)
import Observation
#endif

open class OcaRoot: CustomStringConvertible, @unchecked Sendable,
_OcaObjectKeyPathRepresentable, Observable
Expand All @@ -26,7 +28,9 @@ open class OcaRoot: CustomStringConvertible, @unchecked Sendable,
public internal(set) weak var connectionDelegate: Ocp1Connection?

fileprivate var subscriptionCancellable: Ocp1Connection.SubscriptionCancellable?
#if canImport(Darwin)
fileprivate let _$observationRegistrar = Observation.ObservationRegistrar()
#endif

// 1.1
open class var classID: OcaClassID { OcaClassID("1") }
Expand Down Expand Up @@ -189,6 +193,7 @@ extension _OcaObjectKeyPathRepresentable where Self: OcaRoot {
}
}

#if canImport(Darwin)
nonisolated func access(
keyPath: KeyPath<Self, some Any>
) {
Expand All @@ -201,6 +206,7 @@ extension _OcaObjectKeyPathRepresentable where Self: OcaRoot {
) rethrows -> T {
try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}
#endif
}

public extension OcaRoot {
Expand Down
5 changes: 4 additions & 1 deletion Sources/SwiftOCA/OCC/PropertyTypes/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import AsyncAlgorithms
import AsyncExtensions
import Foundation
#if canImport(Darwin)
import Observation
#endif

public struct OcaPropertyResolutionFlags: OptionSet, Sendable {
public typealias RawValue = UInt32
Expand Down Expand Up @@ -241,14 +243,15 @@ public struct OcaProperty<Value: Codable & Sendable>: Codable, Sendable,
_enclosingInstance object: T,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<T, PropertyValue>
) {
#if canImport(Darwin)
object.access(keyPath: wrappedKeyPath)

_send = { @Sendable [subject] object, newValue in
guard let object else { return }
(object as! T).withMutation(keyPath: wrappedKeyPath) {
subject.send(newValue)
}
}
#endif
}

public static subscript<T: OcaRoot>(
Expand Down
13 changes: 12 additions & 1 deletion Sources/SwiftOCA/OCP.1/Ocp1Connection+Connect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import AsyncExtensions
import Foundation
import Logging
import SystemPackage
#if canImport(Darwin)
import Observation
#endif

private extension Ocp1Error {
var ocp1ConnectionState: Ocp1ConnectionState? {
Expand Down Expand Up @@ -101,6 +104,7 @@ private extension Ocp1ConnectionState {

// MARK: - connection state observation

#if canImport(Darwin)
extension Ocp1Connection {
nonisolated func access(
keyPath: KeyPath<Ocp1Connection, some Any>
Expand All @@ -115,6 +119,7 @@ extension Ocp1Connection {
try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation)
}
}
#endif

// MARK: - monitor task management

Expand Down Expand Up @@ -173,10 +178,14 @@ extension Ocp1Connection {

/// wrapper to update the connection state, logging the old and new connection states
private func _updateConnectionState(_ connectionState: Ocp1ConnectionState) {
logger.trace("_updateConnectionState: \(currentConnectionState) => \(connectionState)")
#if canImport(Darwin)
withMutation(keyPath: \.currentConnectionState) {
logger.trace("_updateConnectionState: \(currentConnectionState) => \(connectionState)")
_connectionState.send(connectionState)
}
#else
_connectionState.send(connectionState)
#endif
}

/// update the device connection state, called from the connection (for
Expand Down Expand Up @@ -279,7 +288,9 @@ extension Ocp1Connection {
}

public var currentConnectionState: Ocp1ConnectionState {
#if canImport(Darwin)
access(keyPath: \.currentConnectionState)
#endif
return _connectionState.value
}

Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftOCA/OCP.1/Ocp1Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import Atomics
@preconcurrency
import Foundation
import Logging
#if canImport(Darwin)
import Observation
#endif

package let Ocp1MaximumDatagramPduSize = 1500

Expand Down Expand Up @@ -154,6 +156,10 @@ public struct Ocp1ConnectionStatistics: Sendable {

private let CommandHandleBase = OcaUint32(100)

#if !canImport(Darwin)
protocol Observable {}
#endif

@OcaConnection
open class Ocp1Connection: Observable, CustomStringConvertible {
public nonisolated static let MinimumPduSize = 7
Expand Down Expand Up @@ -203,7 +209,9 @@ open class Ocp1Connection: Observable, CustomStringConvertible {
var subscriptions = [OcaEvent: EventSubscriptions]()
var logger = Logger(label: "com.padl.SwiftOCA")
var connectionID = 0
#if canImport(Darwin)
let _$observationRegistrar = Observation.ObservationRegistrar()
#endif

private var nextCommandHandle = CommandHandleBase

Expand Down

0 comments on commit 76567c8

Please sign in to comment.