11import Combine
22import Foundation
33
4- /// The primary container of an application's state.
5- ///
6- /// The store both contains and mutates the state through a provided reducer as it's sent actions.
7- /// Use the didChange publisher to be notified of changes.
4+ /// Stores and mutates the state of an application.
85public final class Store < State> : StateStorable {
96
10- /// The current state of the store. Use actions to mutate it.
7+ /// The current state of the store.
118 public private( set) var state : State {
129 didSet { didChange. send ( ) }
1310 }
1411
15- /// Subscribe for state changes. It emits the latest action sent to the store .
12+ /// Publishes when the state has changed .
1613 public let didChange = StorePublisher ( )
1714
1815 @usableFromInline
1916 internal var reduce : SendAction = { _ in }
2017
21- /// Creates a new store for the given state and reducer.
18+ /// Initiates a new store for the given state and reducer.
2219 ///
2320 /// - Parameters
24- /// - state: The initial state of the store. A typically use case is to restore a previous application session with a persisted state object.
25- /// - reducer: A reducer that will mutate the store's state as actions are dispatched to it.
21+ /// - state: The initial state of the store.
22+ /// - reducer: A reducer that mutates the state as actions are dispatched to it.
2623 /// - middleware: A middleware plugin.
2724 public init < R, M> ( state: State , reducer: R , middleware: M ) where R: Reducer , R. State == State , M: Middleware , M. State == State {
2825 let storeReducer = StoreReducer ( rootReducer: reducer)
@@ -41,24 +38,25 @@ public final class Store<State>: StateStorable {
4138 send ( StoreAction< State> . prepare)
4239 }
4340
44- /// Creates a new store for the given state and reducer.
41+ /// Initiates a new store for the given state and reducer.
4542 ///
4643 /// - Parameters
47- /// - state: The initial state of the store. A typically use case is to restore a previous application session with a persisted state object.
48- /// - reducer: A reducer that will mutate the store's state as actions are dispatched to it.
44+ /// - state: The initial state of the store.
45+ /// - reducer: A reducer that mutates the state as actions are dispatched to it.
4946 public convenience init < R> ( state: State , reducer: R ) where R: Reducer , R. State == State {
5047 self . init ( state: state, reducer: reducer, middleware: NoopMiddleware ( ) )
5148 }
5249
53- /// Create a proxy of the store for a given type that it adheres to.
50+ /// Create a proxy of the store for a given type or protocol.
51+ ///
5452 /// - Parameters:
5553 /// - stateType: The type of state for the proxy. This must be a type that the store adheres to.
5654 /// - done: A closure called with an async action has completed.
5755 /// - Returns: A proxy object if the state type matches, otherwise nil.
5856 @inlinable public func proxy< T> ( for stateType: T . Type , done: ( ( ) -> Void ) ? = nil ) -> StoreProxy < T > ? {
5957 guard state is T else { return nil }
6058 return StoreProxy < T > (
61- getState: { [ unowned self ] in self . state as! T } ,
59+ getState: { self . state as! T } ,
6260 didChange: didChange,
6361 dispatcher: self ,
6462 done: done
@@ -68,8 +66,9 @@ public final class Store<State>: StateStorable {
6866
6967extension Store : ActionDispatcher {
7068
71- /// Sends an action to the store to mutate its state.
72- /// - Parameter action: The action to mutate the state.
69+ /// Sends an action to mutate the state.
70+ ///
71+ /// - Parameter action: The action to perform.
7372 @inlinable public func send( _ action: Action ) {
7473 if let action = action as? RunnableAction {
7574 _ = action. run ( store: self )
0 commit comments