Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateStore API updates & docs #40

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 76 additions & 21 deletions Sources/Puredux/Store/StateStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,12 @@ public struct StateStore<State, Action> {
}

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<Action>) -> Void,
qos: DispatchQoS = .userInteractive,
reducer: @escaping Reducer<State, Action>) {

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<State, Action>`
*/
init(_ initialState: State,
qos: DispatchQoS = .userInteractive,
reducer: @escaping Reducer<State, Action>) {
Expand All @@ -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<State, 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<T>(_ initialState: T,
reducer: @escaping Reducer<T, Action>) -> 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<Action>) -> Void,
qos: DispatchQoS = .userInteractive,
reducer: @escaping Reducer<State, Action>) {

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

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<LocalState, ResultState>(
_ initialState: LocalState,
stateMapping: @escaping (State, LocalState) -> ResultState,
Expand All @@ -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<LocalState, ResultState>(
_ initialState: LocalState,
stateMapping: @escaping (State, LocalState) -> ResultState,
Expand All @@ -112,19 +172,14 @@ 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<T>(
_ state: T,
qos: DispatchQoS = .userInitiated,
reducer: @escaping Reducer<T, Action>

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

stateStore(
state,
stateMapping: { ($0, $1) },
qos: qos,
reducer: reducer
)
with(state, reducer: reducer)
}
}
40 changes: 30 additions & 10 deletions Sources/Puredux/Store/StoreOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,42 @@ public struct StoreOf<T> {
}

public extension StoreOf {
func with<U, State, Action>(
_ state: U,
reducer: @escaping Reducer<U, Action>) -> StateStore<(State, U), Action> where T == StateStore<State, Action> {
/**
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<Root, Local, Action>(
_ initialState: Local,
reducer: @escaping Reducer<Local, Action>) -> StateStore<(Root, Local), Action> where T == StateStore<Root, Action> {

wrappedValue.stateStore(
state,
wrappedValue.with(
initialState,
reducer: reducer
)
}

func with<U, State, Action>(
_ state: U,
reducer: @escaping Reducer<U, Action>) -> StateStore<(State, U), Action>? where T == StateStore<State, Action>? {
func with<Root, Local, Action>(
_ initialState: Local,
reducer: @escaping Reducer<Local, Action>) -> StateStore<(Root, Local), Action>? where T == StateStore<Root, Action>? {

wrappedValue?.stateStore(
state,
wrappedValue?.with(
initialState,
reducer: reducer
)
}
Expand Down
Loading