Backport implementation of Swift's Observation framework with access to @_spi(SwiftUI) APIs.
| CI Status |
|---|
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)
The current implementation is aligned with Swift's release/6.1 branch implementation of the Observation framework.
- Full Observation framework implementation
- Cross-platform support (macOS, iOS, tvOS, watchOS)
@Observablemacro support- Thread-safe tracking
- Pure Swift fallback for platforms without toolchain support
dependencies:[.package(url:"https://github.com/OpenSwiftUIProject/OpenObservation", from:"1.0.0")]Comprehensive documentation is available on Swift Package Index.
import OpenObservation
@ObservableclassCounter{varvalue:Int=0func increment(){
value +=1}}
// Use with ObservationTracking
letcounter=Counter()withObservationTracking{print("Counter value: \(counter.value)")} onChange:{print("Counter changed!")}OPENSWIFTUI_SWIFT_TOOLCHAIN_SUPPORTED: Enable Swift toolchain runtime implementation (auto-detected)- Config
OPENSWIFTUI_SWIFT_TOOLCHAIN_PATHandOPENSWIFTUI_SWIFT_TOOLCHAIN_VERSION
- Config
OPENOBSERVATION_DEVELOPMENT: Development mode
- OpenObservation code: MIT License
- Code derived from Swift project: Apache License v2.0 with Runtime Library Exception
See LICENSE file.