Reducer

public struct Reducer<State, Event, Environment>

A reducer is responsible for taking an event and deciding how the state should be changed and whether any effects should be executed.

  • An empty reducer. Useful for SwiftUI’s previews.

    Declaration

    Swift

    public static var empty: Reducer<State, Event, Environment> { get }

    Return Value

    A reducer.

  • Declaration

    Swift

    public typealias Reduce = (inout State, Event, Environment) -> Effect<Event>?

Initialization

  • Construct a Reducer.

    Declaration

    Swift

    public init(_ reduce: @escaping Reduce)

    Parameters

    reduce

    Reduce closure.

Optional

  • Transform the current reducer into one that accepts an optional state.

    The reducer will only be ran when state is non-nil.

    Declaration

    Swift

    public var optional: Reducer<State?, Event, Environment> { get }

Pull

  • Transforms or “Pulls” a local reducer into a global reducer.

    The main benefit of this function is that it enables the ability to breakdown a large reducer into several smaller ones.

    Declaration

    Swift

    public func pull<AppState, AppEvent, AppEnvironment>(
        state: WritableKeyPath<AppState, State>,
        event getEvent: @escaping (AppEvent) -> Event?,
        appEvent setAppEvent: @escaping (Event) -> AppEvent?,
        environment: @escaping (AppEnvironment) -> Environment
    ) -> Reducer<AppState, AppEvent, AppEnvironment>

    Parameters

    state

    A key path that sets and gets the local state from the app state.

    getEvent

    A function for optionally transforming an app event into a local event.

    setAppEvent

    A function for optionally transforming a local event that is emitted by the reducer into an app event.

    environment

    A function for transforming an app environment into a local environment.

    Return Value

    A Reducer<AppState, AppEvent, AppEnvironment>.

  • Transforms or “Pulls” a local reducer into a global reducer.

    The main benefit of this function is that it enables the ability to breakdown a large reducer into several smaller ones.

    Declaration

    Swift

    public func pull<AppState, AppEvent, AppEnvironment>(
        state getState: @escaping (AppState) -> State,
        appState setAppState: @escaping (inout AppState, State) -> Void,
        event getEvent: @escaping (AppEvent) -> Event?,
        appEvent setAppEvent: @escaping (Event) -> AppEvent?,
        environment: @escaping (AppEnvironment) -> Environment
    ) -> Reducer<AppState, AppEvent, AppEnvironment>

    Parameters

    getState

    A key path that sets and gets the local state from the app state.

    setAppState

    A key path that sets and gets the local state from the app state.

    getEvent

    A function for transforming an app event into a local event.

    setAppEvent

    A function for optionally transforming a local event into an app event.

    environment

    A function for transforming an app environment into a local environment.

    Return Value

    A Reducer<AppState, AppEvent, AppEnvironment>.

Combine

  • Combines two reducers of the same State, Event and Environment into one.

    The left hand reducer will be reduced first followed by the right hand reducer. Both reducer’s event streams will be merged.

    Declaration

    Swift

    public static func <>(
        lhs: Reducer<State, Event, Environment>,
        rhs: Reducer<State, Event, Environment>
    ) -> Reducer<State, Event, Environment>

    Parameters

    lhs

    The left hand reducer.

    rhs

    The right hand reducer.

    Return Value

    A new reducer