openswiftuiproject/openobservation
Backport implementation of Swift's Observation framework with access to `@_spi(SwiftUI)` APIs.
Purpose
The official Observation framework in Swift Toolchain doesn't ship with package.swiftinterface, preventing direct use of @_spi(SwiftUI) APIs. There are two solutions to this problem, but this project provides solution 2:
- Manual workaround (not included): Construct a
package.swiftinterfaceand add it to the toolchain (⚠️ No API stability, may break between toolchain versions)
- OpenObservation approach (this project): Reimplement Observation framework, allowing OpenSwiftUI to import it via
@_spi(OpenSwiftUI)
Implementation Status
The current implementation is aligned with Swift's release/6.1 branch implementation of the Observation framework.
Features
- Full Observation framework implementation
- Cross-platform support (macOS, iOS, tvOS, watchOS)
@Observablemacro support- Thread-safe tracking
- Pure Swift fallback for platforms without toolchain support
Installation
dependencies: [
.package(url: "https://github.com/OpenSwiftUIProject/OpenObservation", from: "1.0.0")
]Documentation
Comprehensive documentation is available on Swift Package Index.
Usage
import OpenObservation
@Observable
class Counter {
var value: Int = 0
func increment() {
value += 1
}
}
// Use with ObservationTracking
let counter = Counter()
withObservationTracking {
print("Counter value: \(counter.value)")
} onChange: {
print("Counter changed!")
}Configuration
OPENSWIFTUI_SWIFT_TOOLCHAIN_SUPPORTED: Enable Swift toolchain runtime implementation (auto-detected)
- Config OPENSWIFTUI_SWIFT_TOOLCHAIN_PATH and OPENSWIFTUI_SWIFT_TOOLCHAIN_VERSION
OPENOBSERVATION_DEVELOPMENT: Development mode
License
- OpenObservation code: MIT License
- Code derived from Swift project: Apache License v2.0 with Runtime Library Exception
See LICENSE file.
Package Metadata
Repository: openswiftuiproject/openobservation
Default branch: main
README: README.md