Skip to content

Commit

Permalink
Using ObservableValues from ObservableState
Browse files Browse the repository at this point in the history
And renamed `valueProxies` to `observableValues`
  • Loading branch information
PimCoumans committed Feb 24, 2023
1 parent d10437e commit 9c68b18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
19 changes: 2 additions & 17 deletions Sources/DidUpdate/ObservedState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,9 @@
@propertyWrapper
public struct ObservedState<StateObject: ObservableState> {

/// 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<Value>(
dynamicMember keyPath: ReferenceWritableKeyPath<StateObject, Value>
) -> ValueProxy<Value> {
stateObject().valueProxy(from: keyPath)
}
}

public var wrappedValue: StateObject
public var projectedValue: ObservableValues {
ObservableValues(observing: wrappedValue)
public var projectedValue: ObservableValues<StateObject> {
wrappedValue.observableValues
}

public init(_ stateObject: StateObject) {
Expand Down
21 changes: 18 additions & 3 deletions Sources/DidUpdate/Protocols/ObservableState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateObject: ObservableState> {
fileprivate var stateObject: () -> StateObject
public init(observing: @autoclosure @escaping () -> StateObject) {
self.stateObject = observing
}

public subscript<Value>(
dynamicMember keyPath: ReferenceWritableKeyPath<StateObject, Value>
) -> ValueProxy<Value> {
stateObject().valueProxy(from: keyPath)
}
}

private var stateObserverKey = "ObservableStateObserver"
public extension ObservableState {
private func newObserver() -> StateObserver { .init() }
Expand All @@ -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<Self>.ObservableValues {
ObservedState<Self>.ObservableValues(observing: self)
/// - Returns: ``ObservableValues`` struct pointing to wrapping self
var observableValues: ObservableValues<Self> {
ObservableValues(observing: self)
}

internal func valueProxy<Value>(from keyPath: ReferenceWritableKeyPath<Self, Value>) -> ValueProxy<Value> {
Expand Down

0 comments on commit 9c68b18

Please sign in to comment.