Skip to content

Commit 425e921

Browse files
committed
Add some docs about ViewStore publisher. (#699)
* Add some docs about ViewStore publisher. * clean up
1 parent 67db4e7 commit 425e921

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

Sources/ComposableArchitecture/ViewStore.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,35 @@ public final class ViewStore<State, Action> {
9999
#endif
100100
self._state = $0
101101
self.statePipe.input.send(value: $0)
102-
}
102+
}
103103
}
104104

105-
/// A producer of state.
106-
public var produced: StoreProducer<State> {
105+
/// A publisher that emits when state changes.
106+
///
107+
/// This publisher supports dynamic member lookup so that you can pluck out a specific field in
108+
/// the state:
109+
///
110+
/// ```swift
111+
/// viewStore.publisher.alert
112+
/// .sink { ... }
113+
/// ```
114+
///
115+
/// When the emission happens the ``ViewStore``'s state has been updated, and so the following
116+
/// precondition will pass:
117+
///
118+
/// ```swift
119+
/// viewStore.publisher
120+
/// .sink { precondition($0 == viewStore.state) }
121+
/// ```
122+
///
123+
/// This means you can either use the value passed to the closure or you can reach into
124+
/// `viewStore.state` directly.
125+
///
126+
/// - Note: Due to a bug in Combine (or feature?), the order you `.sink` on a publisher has no
127+
/// bearing on the order the `.sink` closures are called. This means the work performed inside
128+
/// `viewStore.publisher.sink` closures should be completely independent of each other.
129+
/// Later closures cannot assume that earlier ones have already run.
130+
public var produced: StoreProducer<State> {
107131
StoreProducer(viewStore: self)
108132
}
109133

0 commit comments

Comments
 (0)