ActionPlan

public struct ActionPlan<State> : RunnableAction

Encapsulates multiple actions into a packaged up “action plan”

   enum UserAction {

     static func loadUser(byId id: String) -> ActionPlan<AppState> {
       ActionPlan<AppState> { store, completed in
         guard !store.state.users.hasValue(id) else { return nil }
         store.send(UserAction.setLoading(true))
         return UserService.getUser(id)
           .first()
           .flatMap { user in
               [
                 UserAction.setUser(user)
                 UserAction.setLoading(false)
               ].publisher
             }
           }
           .send(to: store, receivedCompletion: completed)
       }
     }

   }

   // Somewhere inside a view:

   func loadUser() {
     dispatch(UserAction.loadUser(byId: self.id))
   }
```.
  • The body of a publishable action plan.

    Declaration

    Swift

    public typealias Body = (StoreProxy<State>, @escaping ActionSubscriber.ReceivedCompletion) -> AnyCancellable?

    Parameters

    StoreProxy

    Dispatch actions or retreive the current state from the store.

    Return Value

    A publisher that can send actions to the store.

  • Create a new action plan that returns an optional publisher.

    Declaration

    Swift

    @inlinable
    public init(_ body: @escaping Body)

    Parameters

    body

    The body of the action plan.

  • Create a new action plan that returns an optional publisher.

    Declaration

    Swift

    @inlinable
    public init<P>(_ body: @escaping (StoreProxy<State>) -> P) where P : Publisher, P.Failure == Never, P.Output == Action

    Parameters

    body

    The body of the action plan.

  • Create a new action plan.

    Declaration

    Swift

    @inlinable
    public init(_ body: @escaping (StoreProxy<State>) -> Void)

    Parameters

    body

    The body of the action plan.

  • Declaration

    Swift

    public func run<T>(store: Store<T>) -> AnyCancellable?
  • Manually run the action plan.

    this can be useful to run an action plan inside a containing action plan.

    Declaration

    Swift

    @inlinable
    public func run(_ store: StoreProxy<State>, completed: @escaping ActionSubscriber.ReceivedCompletion = {}) -> AnyCancellable?

    Return Value

    A publisher that can send actions to the store.

  • Send an action plan that can be cancelled.

    This is useful for action plans that return a publisher that require a cancellable step. For example, a web request that should be cancelled if the user navigates away from the relevant view.

    struct MyView: View {
    
      @MappedDispatch() private var dispatch
    
      @State private var username: String = ""
      @State private var password: String = ""
    
      @State private var signUpCancellable: AnyCancellable? = nil
    
      var body: some View {
        Group {
          /// ...signup form
          Button(action: self.signUp) { Text("Sign Up") }
        }
        .onDisappear { self.signUpCancellable?.cancel() }
      }
    
      func signUp() {
        signUpCancellable = signUpActionPlan(username: username, password: password).sendAsCancellable(dispatch)
      }
    
    }
    

    Declaration

    Swift

    @inlinable
    public func sendAsCancellable(_ dispatcher: ActionDispatcher) -> AnyCancellable

    Parameters

    dispatcher

    The send function that dispatches an action.

    Return Value

    AnyCancellable to cancel the action plan.

  • Dispatches another action plan after this one has completed. This allows action plans to be chained together to perform their actions synchronously.

    Declaration

    Swift

    @inlinable
    public func then(_ actionPlans: ActionPlan<State>...) -> ActionPlan<State>

    Parameters

    actionPlans

    One or mroe action plans to chain after this one.

    Return Value

    A new action plan that chains the source plan with the provided ones in the parameters.

  • Calls the provided block once the action plan has completed. The current state is provided to the block.

    Declaration

    Swift

    @inlinable
    public func then(_ block: @escaping (State) -> Void) -> ActionPlan<State>

    Parameters

    block

    A block of code to execute once the action plan has completed.

    Return Value

    A new action plan.

  • Calls the provided block once the action plan has completed.

    Declaration

    Swift

    @inlinable
    public func then(_ block: @escaping () -> Void) -> ActionPlan<State>

    Parameters

    block

    A block of code to execute once the action plan has completed.

    Return Value

    A new action plan.