diff --git a/Sources/Puredux/Store/StateStore.swift b/Sources/Puredux/Store/StateStore.swift index 4e585bc..a23fc78 100644 --- a/Sources/Puredux/Store/StateStore.swift +++ b/Sources/Puredux/Store/StateStore.swift @@ -36,20 +36,12 @@ public struct StateStore { } public extension StateStore { - @available(*, deprecated, renamed: "init(_:qos:reducer:)", message: "Actions Interceptor will be removed in 2.0, use AsyncAction instead.") - init(_ initialState: State, - interceptor: @escaping (Action, @escaping Dispatch) -> Void, - qos: DispatchQoS = .userInteractive, - reducer: @escaping Reducer) { - - self.init(storeObject: RootStoreNode.initRootStore( - initialState: initialState, - interceptor: interceptor, - qos: qos, - reducer: reducer - )) - } - + /** Initializes a new StateStore with provided initial state, qos, and reducer + - Parameter initialState: The initial state for the store + - Parameter qos: defines the DispatchQueue QoS that reducer will be performed on + - Parameter reducer: The function that is called on every dispatched Action and performs state mutations + - Returns: `StateStore` + */ init(_ initialState: State, qos: DispatchQoS = .userInteractive, reducer: @escaping Reducer) { @@ -76,6 +68,73 @@ extension StateStore { } public extension StateStore { + /** + Initializes a new child StateStore with initial state + - Parameter initialState: The initial state for the store + - Parameter reducer: The function that is called on every dispatched Action and performs state mutations + - Returns: `StateStore` + + ChildStore is a composition of root store and newly created local store. + + When action is dispatched to RootStore: + - action is delivered to root store's reducer + - action is not delivered to child store's reducer + - root & child stores subscribers receive updates + - AsyncAction dispatches result actions to RootStore + + When action is dispatched to ChildStore: + - action is delivered to root store's reducer + - action is delivered to child store's reducer + - root & child stores subscribers receive updates + - AsyncAction dispatches result actions to ChildStore + */ + func with(_ initialState: T, + reducer: @escaping Reducer) -> StateStore<(State, T), Action> { + + StateStore<(State, T), Action>( + storeObject: storeObject.createChildStore( + initialState: initialState, + stateMapping: { ($0, $1) }, + reducer: reducer + ) + ) + } +} + +public extension StateStore { + @available(*, deprecated, renamed: "init(_:qos:reducer:)", message: "Actions Interceptor will be removed in 2.0, use AsyncAction instead.") + init(_ initialState: State, + interceptor: @escaping (Action, @escaping Dispatch) -> Void, + qos: DispatchQoS = .userInteractive, + reducer: @escaping Reducer) { + + self.init(storeObject: RootStoreNode.initRootStore( + initialState: initialState, + interceptor: interceptor, + qos: qos, + reducer: reducer + )) + } + +} + +public extension StateStore { + @available(*, deprecated, renamed: "stateStore(_:stateMapping:qos:reducer:)", message: "Will be removed in 2.0") + func childStore( + initialState: LocalState, + stateMapping: @escaping (State, LocalState) -> ResultState, + qos: DispatchQoS = .userInteractive, + reducer: @escaping Reducer) -> StateStore { + + stateStore( + initialState, + stateMapping: stateMapping, + qos: qos, + reducer: reducer + ) + } + + @available(*, deprecated, message: "Use with(_:reducer) and store transformations instead. Will be removed in 2.0. ") func stateStore( _ initialState: LocalState, stateMapping: @escaping (State, LocalState) -> ResultState, @@ -94,6 +153,7 @@ public extension StateStore { ) } + @available(*, deprecated, message: "Use with(_:reducer) and store transformations instead. Will be removed in 2.0.") func stateStore( _ initialState: LocalState, stateMapping: @escaping (State, LocalState) -> ResultState, @@ -112,7 +172,7 @@ public extension StateStore { } extension StateStore { - + @available(*, deprecated, message: "Qos parameter will have no effect. Root store's QoS is used. Use with(_:reducer) and store transformations instead. Will be removed in 2.0.") public func stateStore( _ state: T, qos: DispatchQoS = .userInitiated, @@ -120,11 +180,6 @@ extension StateStore { ) -> StateStore<(State, T), Action> { - stateStore( - state, - stateMapping: { ($0, $1) }, - qos: qos, - reducer: reducer - ) + with(state, reducer: reducer) } } diff --git a/Sources/Puredux/Store/StoreOf.swift b/Sources/Puredux/Store/StoreOf.swift index fe43469..bb9cb17 100644 --- a/Sources/Puredux/Store/StoreOf.swift +++ b/Sources/Puredux/Store/StoreOf.swift @@ -25,22 +25,42 @@ public struct StoreOf { } public extension StoreOf { - func with( - _ state: U, - reducer: @escaping Reducer) -> StateStore<(State, U), Action> where T == StateStore { + /** + Initializes a new child StateStore with initial state + - Parameter initialState: The initial state for the store + - Parameter reducer: The function that is called on every dispatched Action and performs state mutations + - Returns: `StateStore<(Root, Local), Action>` + + ChildStore is a composition of root store and newly created local store. + + When action is dispatched to RootStore: + - action is delivered to root store's reducer + - action is not delivered to child store's reducer + - root & child stores subscribers receive updates + - AsyncAction dispatches result actions to RootStore + + When action is dispatched to ChildStore: + - action is delivered to root store's reducer + - action is delivered to child store's reducer + - root & child stores subscribers receive updates + - AsyncAction dispatches result actions to ChildStore + */ + func with( + _ initialState: Local, + reducer: @escaping Reducer) -> StateStore<(Root, Local), Action> where T == StateStore { - wrappedValue.stateStore( - state, + wrappedValue.with( + initialState, reducer: reducer ) } - func with( - _ state: U, - reducer: @escaping Reducer) -> StateStore<(State, U), Action>? where T == StateStore? { + func with( + _ initialState: Local, + reducer: @escaping Reducer) -> StateStore<(Root, Local), Action>? where T == StateStore? { - wrappedValue?.stateStore( - state, + wrappedValue?.with( + initialState, reducer: reducer ) }