API: Key Structures and Protocols

  • Combine-based state management. Enables dispatching of actions, executing reducers, performing side-effects, and listening for the latest state.

    Implements the Publisher protocol, allowing direct subscription for the latest state.

    import ReCombine
    import Combine
    
    struct CounterView {
        struct Increment: Action {}
        struct Decrement: Action {}
    
        struct State {
            var count = 0
        }
    
        static func reducer(state: State, action: Action) -> State {
            var state = state
            switch action {
                case _ as Increment:
                    state.count += 1
                    return state
                case _ as Decrement:
                    state.count -= 1
                    return state
                default:
                    return state
            }
        }
    
        static let effect = Effect(dispatch: false)  { (actions: AnyPublisher<Action, Never>) in
            actions.ofTypes(Increment.self, Decrement.self).print("Action Dispatched").eraseToAnyPublisher()
        }
    }
    
    let store = Store(reducer: CounterView.reducer, initialState: CounterView.State(), effects: [CounterView.effect])
    
    See more

    Declaration

    Swift

    open class Store<S> : Publisher
  • Protocol that all action’s must implement.

    Example implementation:

    struct GetPostSuccess: Action {
        let post: Post
    }
    

    Declaration

    Swift

    public protocol Action
  • Configures an Effect from a source function and a dispatch option.

    Effects are used for side-effects in ReCombine applications. See the Architecture section of the official documentation for more.

    let successEffect = Effect(dispatch: false) { (actions: AnyPublisher<Action, Never>) -> AnyPublisher<Action, Never> in
       actions
        .ofType(GetPostsSuccess.self)
        .handleEvents(receiveOutput: { _ in print("Got it") })
        .eraseActionType()
        .eraseToAnyPublisher()
    }
    
    See more

    Declaration

    Swift

    public struct Effect