ActionPlan

public struct ActionPlan<State> : RunnableAction

Encapsulates external business logic outside of a reducer into a special kind of action.

   enum UserAction {

     static func loadUser(byId id: String) -> ActionPlan<AppState> {
       ActionPlan<AppState> { store 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
             }
           }
       }
     }
   }

   // Inside a view:

   func body(props: Props) -> some View {
     UserInfo(user: props.user)
       .onAppear { dispatch(UserAction.loadUser(byId: self.id)) }
   }
```.
  • Initiate an action plan that returns a publisher of actions.

    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.

  • Initiate an asynchronous action plan that completes after the first emitted void value from its publisher.

    Use this method to wrap asynchronous code in a publisher like Future<Void, Never>.

    Declaration

    Swift

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

    Parameters

    body

    The body of the action plan.

  • Initiate a synchronous action plan.

    The plan expects to complete once the body has returned.

    Declaration

    Swift

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

    Parameters

    body

    The body of the action plan.

  • Declaration

    Swift

    @inlinable
    public func run<T>(store: StoreProxy<T>) -> AnyPublisher<Action, Never>