Actions

  • A dispatchable action that provides information to a reducer to mutate the state of the application.

    Typically this is done with enum types, however, it could be added to protocols or structs if a more complex solution is needed. Structs are also a could choice if actions need to be codable.

      enum TodoList : Action {
        case setItems(items: [TodoItem])
        case addItem(withText: String)
        case removeItems(at: IndexSet)
        case moveItems(at: IndexSet, to: Int)
      }
    
      // You can also create new protocols that represent an entire feature's actions.
      // This can allows views to update off of a granular action or any actions
      // of a given feature.
      protocol FeatureLevelAction : Action {}
    
      enum SubfeatureAction: FeatureLevelAction {
        ...
      }
    

    Declaration

    Swift

    public protocol Action
  • 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))
       }
    ```.
    
    See more

    Declaration

    Swift

    public struct ActionPlan<State> : RunnableAction
  • An object that dispatches actions to a store.

    Once an action is sent, the sender shouldn’t expect anything to occur. Instead, it should rely solely on changes to the state of the application to respond.

    See more

    Declaration

    Swift

    public protocol ActionDispatcher
  • Subscribes to a publisher of actions, and sends them to an action dispatcher.

    See more

    Declaration

    Swift

    final public class ActionSubscriber : Subscriber
  • A special kind of action that performs internal logic outside of a reducer.

    An ActionPlan<_> is a concrete type of RunnbableAction, and is good enough for most cases.

    See more

    Declaration

    Swift

    public protocol RunnableAction : Action
  • A noop action used by reducers that may not have their own actions.

    See more

    Declaration

    Swift

    public struct EmptyAction : Action
  • A closure that dispatches an action.

    Declaration

    Swift

    public typealias SendAction = (Action) -> Void

    Parameters

    action

    Dispatches the given action synchronously.