Middleware

public protocol Middleware

Middleware perform actions on the the store when actions are dispatched to it.

Before an action is given to a reducer, middleware have an opportunity to handle it themselves. They may dispatch their own actions, transform the current action, or block an incoming ones from continuing.

Middleware can also be used to set up external hooks from services.

For a reducer’s own state and actions, implement the reduce(state:action:). For subreducers, implement the reduceNext(state:action:) method.

  • Undocumented

    Declaration

    Swift

    associatedtype State
  • Perform any middleware actions within this function.

    Declaration

    Swift

    func run(store: StoreProxy<State>, action: Action)

    Parameters

    store

    The store object. Use store.next when the middleware is complete.

    action

    The latest dispatched action to process.

  • compile(store:) Default implementation

    Compiles the middleware into a SendAction closure.

    Default Implementation

    Declaration

    Swift

    func compile(store: StoreProxy<State>) -> SendAction

    Parameters

    store

    A reference to the store used by the middleware.

    Return Value

    The SendAction that performs the middleware.

  • callAsFunction(store:) Extension method

    Apply the middleware to a store proxy.

    Declaration

    Swift

    @inlinable
    public func callAsFunction(store: StoreProxy<State>) -> SendAction

    Parameters

    store

    The store proxy.

    Return Value

    A SendAction function that performs the middleware for the provided store proxy.

  • +(_:_:) Extension method

    Compose two middleware together.

    Declaration

    Swift

    public static func + <M>(previousMiddleware: Self, _ nextMiddleware: M) -> CombinedMiddleware<State, Self, M>
    where M: Middleware, M.State == State

    Parameters

    previousMiddleware

    The middleware to be called first.

    nextMiddleware

    The next middleware to call.

    Return Value

    The combined middleware.