Skip to content

Commit

Permalink
Internals refactoring for StateStore (#17)
Browse files Browse the repository at this point in the history
* StateStore init and appending

* replaced StoreFactory internal implementation

* renamed a few things
  • Loading branch information
KazaiMazai authored Apr 7, 2024
1 parent 07cdd57 commit b99bc46
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
53 changes: 39 additions & 14 deletions Sources/Puredux/Store/StateStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public struct StateStore<State, Action> {
}
}

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,
qos: qos,
reducer: reducer)
}
}

extension StateStore {
func weakStore() -> Store<State, Action> {
Store<State, Action>(
Expand All @@ -39,19 +53,30 @@ extension StateStore {
subscribe: subscribe)
}

func createChildStore<LocalState, ResultState>(
initialState: LocalState,
stateMapping: @escaping (State, LocalState) -> ResultState,
qos: DispatchQoS,
reducer: @escaping Reducer<LocalState, Action>) -> StateStore<ResultState, Action> {
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
)
)
}

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

createChildStore(
initialState: state,
stateMapping: { ($0, $1) },
qos: qos,
reducer: reducer
)
}
}
22 changes: 10 additions & 12 deletions Sources/Puredux/Store/StoreFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public final class StoreFactory<State, Action> {
let rootStoreNode: RootStoreNode<State, Action>
let rootStateStore: StateStore<State, Action>

/// Initializes a new StoreFactory with provided initial state, actions interceptor, qos, and reducer
///
Expand All @@ -31,7 +31,7 @@ public final class StoreFactory<State, Action> {
qos: DispatchQoS = .userInteractive,
reducer: @escaping Reducer<State, Action>) {

rootStoreNode = RootStoreNode.initRootStore(
rootStateStore = StateStore(
initialState: initialState,
interceptor: interceptor,
qos: qos,
Expand All @@ -50,7 +50,7 @@ public extension StoreFactory {
/// Store is thread safe. Actions can be dispatched from any thread. Can be subscribed from any thread.
///
func rootStore() -> Store<State, Action> {
rootStoreNode.weakRefStore()
rootStateStore.weakStore()
}

/// Initializes a new Store with state mapping to local substate.
Expand All @@ -62,7 +62,7 @@ public extension StoreFactory {
/// Store is thread safe. Actions can be dispatched from any thread. Can be subscribed from any thread.
///
func scopeStore<LocalState>(to localState: @escaping (State) -> LocalState) -> Store<LocalState, Action> {
rootStoreNode.weakRefStore().scope(to: localState)
rootStateStore.weakStore().scope(to: localState)
}

/// Initializes a new Store with state mapping to local substate.
Expand All @@ -75,7 +75,7 @@ public extension StoreFactory {
/// When the result local state is nill, subscribers are not triggered.
///
func scopeStore<LocalState>(toOptional localState: @escaping (State) -> LocalState?) -> Store<LocalState, Action> {
rootStoreNode.weakRefStore().scope(toOptional: localState)
rootStateStore.weakStore().scope(toOptional: localState)
}

/// Initializes a new child StateStore with initial state
Expand Down Expand Up @@ -112,13 +112,11 @@ public extension StoreFactory {

StateStore<LocalState, Action> {

StateStore(
storeObject: rootStoreNode.createChildStore(
initialState: initialState,
stateMapping: stateMapping,
qos: qos,
reducer: reducer
)
rootStateStore.createChildStore(
initialState: initialState,
stateMapping: stateMapping,
qos: qos,
reducer: reducer
)
}

Expand Down

0 comments on commit b99bc46

Please sign in to comment.