Skip to content

Commit 186f8d4

Browse files
Added support to projected value
1 parent 741665c commit 186f8d4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Sources/SharedObject/SharedObject.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public struct SharedObject<ObjectType, ID>: DynamicProperty where ObjectType: Ob
2121
nonmutating set { object.object = newValue }
2222
}
2323

24+
/// A projection of the shared object that creates bindings to its properties using dynamic member lookup.
25+
public var projectedValue: SharedObject.Wrapper {
26+
.init(object.object)
27+
}
28+
2429
/// Retrieves the shared object with the given id or creates a shared object with an initial wrapped value.
2530
public init(wrappedValue: ObjectType, _ id: ID) {
2631
object = .init(wrappedValue: wrappedValue, id: id)
@@ -53,4 +58,23 @@ public struct SharedObject<ObjectType, ID>: DynamicProperty where ObjectType: Ob
5358
}
5459
}
5560
}
61+
62+
/// A wrapper of the underlying observable object that can create bindings to its properties using dynamic member lookup.
63+
@dynamicMemberLookup
64+
public struct Wrapper {
65+
66+
private let object: ObjectType
67+
68+
init(_ object: ObjectType) {
69+
self.object = object
70+
}
71+
72+
subscript<Subject>(dynamicMember keyPath: ReferenceWritableKeyPath<ObjectType, Subject>) -> Binding<Subject> {
73+
.init {
74+
object[keyPath: keyPath]
75+
} set: { newValue in
76+
object[keyPath: keyPath] = newValue
77+
}
78+
}
79+
}
5680
}

0 commit comments

Comments
 (0)