Skip to content

Commit 4167b3c

Browse files
authored
Add a SharedReader.constant helper. (#3127)
1 parent b529ff5 commit 4167b3c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Sources/ComposableArchitecture/SharedState/Shared.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ extension Shared {
353353
keyPath: self.keyPath.appending(path: keyPath)!
354354
)
355355
}
356-
356+
357+
/// Constructs a read-only version of the shared value.
357358
public var reader: SharedReader<Value> {
358359
SharedReader(reference: self.reference, keyPath: self.keyPath)
359360
}

Sources/ComposableArchitecture/SharedState/SharedReader.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ public struct SharedReader<Value> {
3737
public init(_ base: Shared<Value>) {
3838
self = base.reader
3939
}
40+
41+
/// Constructs a read-only shared value that remains constant.
42+
///
43+
/// This can be useful for providing ``SharedReader`` values to features in previews and tests:
44+
///
45+
/// ```swift
46+
/// #Preview {
47+
/// FeatureView(
48+
/// store: Store(
49+
/// initialState: Feature.State(count: .constant(42))
50+
/// ) {
51+
/// Feature()
52+
/// }
53+
/// )
54+
/// )
55+
/// ```
56+
public static func constant(_ value: Value) -> Self {
57+
Shared(value).reader
58+
}
4059

4160
public var wrappedValue: Value {
4261
func open<Root>(_ reference: some Reference<Root>) -> Value {

0 commit comments

Comments
 (0)