Skip to content

Commit

Permalink
StoreFactory deprecations (#30)
Browse files Browse the repository at this point in the history
* renamed methods

* added deprecations
  • Loading branch information
KazaiMazai authored Aug 23, 2024
1 parent 9b123c2 commit b3ec776
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
96 changes: 57 additions & 39 deletions Sources/Puredux/Store/StateStore.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Sergey Kazakov on 04/04/2024.
//
Expand All @@ -12,26 +12,26 @@ public typealias StoreObject = StateStore

public struct StateStore<State, Action> {
let storeObject: any StoreProtocol<State, Action>

public func store() -> Store<State, Action> {
weakStore()
}

public func dispatch(_ action: Action) {
storeObject.dispatch(action)
}

public func subscribe(observer: Observer<State>) {
storeObject.subscribe(observer: observer)
}
}

extension StateStore {
init(initialState: State,
public extension StateStore {
init(_ initialState: State,
interceptor: @escaping (Action, @escaping Dispatch<Action>) -> Void = { _, _ in },
qos: DispatchQoS = .userInteractive,
reducer: @escaping Reducer<State, Action>) {

storeObject = RootStoreNode.initRootStore(
initialState: initialState,
interceptor: interceptor,
Expand All @@ -47,34 +47,46 @@ extension StateStore {
dispatcher: { [weak storeObject] in storeObject?.dispatch($0) },
subscribe: { [weak storeObject] in storeObject?.subscribe(observer: $0) })
}

func strongStore() -> Store<State, Action> {
Store<State, Action>(
dispatcher: dispatch,
subscribe: subscribe
)
}
}

func createChildStore<LocalState, ResultState>(initialState: LocalState,
stateMapping: @escaping (State, LocalState) -> ResultState,
qos: DispatchQoS,
reducer: @escaping Reducer<LocalState, Action>) -> StateStore<ResultState, Action> {

StateStore<ResultState, Action>(
storeObject: storeObject.createChildStore(
initialState: initialState,
stateMapping: stateMapping,
qos: qos,
reducer: reducer
)
)
}
public extension StateStore {

func stateStore<LocalState, ResultState>(
_ initialState: LocalState,
stateMapping: @escaping (State, LocalState) -> ResultState,
qos: DispatchQoS,
reducer: @escaping Reducer<LocalState, Action>

func createChildStore<LocalState, ResultState, LocalAction>(initialState: LocalState,
stateMapping: @escaping (State, LocalState) -> ResultState,
actionsMapping: ActionsMapping<Action, LocalAction>,
reducer: @escaping Reducer<LocalState, LocalAction>) -> StateStore<ResultState, LocalAction> {
) -> StateStore<ResultState, Action> {

StateStore<ResultState, Action>(
storeObject: storeObject.createChildStore(
initialState: initialState,
stateMapping: stateMapping,
qos: qos,
reducer: reducer
)
)
}
}

extension StateStore {

func stateStore<LocalState, ResultState, LocalAction>(
_ initialState: LocalState,
stateMapping: @escaping (State, LocalState) -> ResultState,
actionsMapping: ActionsMapping<Action, LocalAction>,
reducer: @escaping Reducer<LocalState, LocalAction>

) -> StateStore<ResultState, LocalAction> {

StateStore<ResultState, LocalAction>(
storeObject: storeObject.createChildStore(
initialState: initialState,
Expand All @@ -84,25 +96,31 @@ extension StateStore {
)
)
}

func appending<T>(_ state: T,
qos: DispatchQoS = .userInitiated,
reducer: @escaping Reducer<T, Action>) -> StateStore<(State, T), Action> {

createChildStore(
initialState: state,

public func stateStore<T>(
_ state: T,
qos: DispatchQoS = .userInitiated,
reducer: @escaping Reducer<T, Action>

) -> StateStore<(State, T), Action> {

stateStore(
state,
stateMapping: { ($0, $1) },
qos: qos,
reducer: reducer
)
}

func appending<T, A>(_ state: T,
actionsMapping: ActionsMapping<Action, A>,
reducer: @escaping Reducer<T, A>) -> StateStore<(State, T), A> {

createChildStore(
initialState: state,
func stateStore<T, A>(
_ state: T,
actionsMapping: ActionsMapping<Action, A>,
reducer: @escaping Reducer<T, A>

) -> StateStore<(State, T), A> {

stateStore(
state,
stateMapping: { ($0, $1) },
actionsMapping: actionsMapping,
reducer: reducer
Expand Down
16 changes: 10 additions & 6 deletions Sources/Puredux/Store/StoreFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation

@available(*, deprecated, message: "Use StateStore directly")
public final class StoreFactory<State, Action> {
let rootStateStore: StateStore<State, Action>

Expand All @@ -26,13 +27,14 @@ public final class StoreFactory<State, Action> {
/// - Scope Store - scoped store proxy to the root store
/// - Child Store - child store with `(Root, Local) -> Composition` state mapping and it's own lifecycle
///
@available(*, deprecated, message: "Use StateStore(init:) directly")
public init(initialState: State,
interceptor: @escaping (Action, @escaping Dispatch<Action>) -> Void = { _, _ in },
qos: DispatchQoS = .userInteractive,
reducer: @escaping Reducer<State, Action>) {

rootStateStore = StateStore(
initialState: initialState,
initialState,
interceptor: interceptor,
qos: qos,
reducer: reducer)
Expand All @@ -48,7 +50,7 @@ public extension StoreFactory {
/// Store is a proxy for the factory root store.
/// All dispatched Actions and subscribtions are forwarded to the factory root store object.
/// Store is thread safe. Actions can be dispatched from any thread. Can be subscribed from any thread.
///
@available(*, deprecated, message: "Use StateStore's store() method instead")
func rootStore() -> Store<State, Action> {
rootStateStore.weakStore()
}
Expand All @@ -60,7 +62,7 @@ public extension StoreFactory {
/// Store is a proxy for the root store object.
/// All dispatched Actions and subscribtions are forwarded to the root store.
/// Store is thread safe. Actions can be dispatched from any thread. Can be subscribed from any thread.
///
@available(*, deprecated, message: "Use store's scope(to:) method instead")
func scopeStore<LocalState>(to localState: @escaping (State) -> LocalState) -> Store<LocalState, Action> {
rootStateStore.weakStore().scope(to: localState)
}
Expand All @@ -73,7 +75,7 @@ public extension StoreFactory {
/// All dispatched Actions and subscribtions are forwarded to the root store.
/// Store is thread safe. Actions can be dispatched from any thread. Can be subscribed from any thread.
/// When the result local state is nill, subscribers are not triggered.
///
@available(*, deprecated, message: "Use store's scope(to:) method instead")
func scopeStore<LocalState>(toOptional localState: @escaping (State) -> LocalState?) -> Store<LocalState, Action> {
rootStateStore.weakStore().scope(toOptional: localState)
}
Expand Down Expand Up @@ -104,6 +106,7 @@ public extension StoreFactory {
/// - local state update triggers child stores' subscribers.
/// - Interceptor dispatches additional actions to ChildStore
///
@available(*, deprecated, message: "Use StateStore's stateStore(...) method instead")
func childStore<ChildState, LocalState>(
initialState: ChildState,
stateMapping: @escaping (State, ChildState) -> LocalState,
Expand All @@ -112,8 +115,8 @@ public extension StoreFactory {

StateStore<LocalState, Action> {

rootStateStore.createChildStore(
initialState: initialState,
rootStateStore.stateStore(
initialState,
stateMapping: stateMapping,
qos: qos,
reducer: reducer
Expand Down Expand Up @@ -147,6 +150,7 @@ public extension StoreFactory {
/// - local state update triggers child stores' subscribers.
/// - Interceptor dispatches additional actions to ChildStore
///
@available(*, deprecated, message: "Use StateStore's stateStore(...) method instead")
func childStore<ChildState>(
initialState: ChildState,
qos: DispatchQoS = .userInteractive,
Expand Down

0 comments on commit b3ec776

Please sign in to comment.