diff --git a/Sources/DidUpdate/ObservedState.swift b/Sources/DidUpdate/ObservedState.swift index 00cb417..6b5d5f3 100644 --- a/Sources/DidUpdate/ObservedState.swift +++ b/Sources/DidUpdate/ObservedState.swift @@ -18,24 +18,9 @@ @propertyWrapper public struct ObservedState { - /// Creates `ValueProxy` structs to forward getting and setting of values and allow adding observers for specific keyPaths - @dynamicMemberLookup - public struct ObservableValues { - fileprivate var stateObject: () -> StateObject - public init(observing: @autoclosure @escaping () -> StateObject) { - self.stateObject = observing - } - - public subscript( - dynamicMember keyPath: ReferenceWritableKeyPath - ) -> ValueProxy { - stateObject().valueProxy(from: keyPath) - } - } - public var wrappedValue: StateObject - public var projectedValue: ObservableValues { - ObservableValues(observing: wrappedValue) + public var projectedValue: ObservableValues { + wrappedValue.observableValues } public init(_ stateObject: StateObject) { diff --git a/Sources/DidUpdate/Protocols/ObservableState.swift b/Sources/DidUpdate/Protocols/ObservableState.swift index a78eca6..f1bfe20 100644 --- a/Sources/DidUpdate/Protocols/ObservableState.swift +++ b/Sources/DidUpdate/Protocols/ObservableState.swift @@ -7,6 +7,21 @@ public protocol ObservableState: AnyObject { var stateObserver: StateObserver { get } } +/// Creates `ValueProxy` structs to forward getting and setting of values and allow adding observers for specific keyPaths +@dynamicMemberLookup +public struct ObservableValues { + fileprivate var stateObject: () -> StateObject + public init(observing: @autoclosure @escaping () -> StateObject) { + self.stateObject = observing + } + + public subscript( + dynamicMember keyPath: ReferenceWritableKeyPath + ) -> ValueProxy { + stateObject().valueProxy(from: keyPath) + } +} + private var stateObserverKey = "ObservableStateObserver" public extension ObservableState { private func newObserver() -> StateObserver { .init() } @@ -21,9 +36,9 @@ public extension ObservableState { } /// Wrapper to create local proxies using dynamic member subscripts - /// - Returns: ``ObservedState/ObservableValues`` struct pointing to self - var valueProxies: ObservedState.ObservableValues { - ObservedState.ObservableValues(observing: self) + /// - Returns: ``ObservableValues`` struct pointing to wrapping self + var observableValues: ObservableValues { + ObservableValues(observing: self) } internal func valueProxy(from keyPath: ReferenceWritableKeyPath) -> ValueProxy {