AsyncAction

@available(*, deprecated, message: "Use SideEffect or StateUpdater instead")
public protocol AsyncAction : Action, AnyAsyncAction

Protocol that represents an async action.

An AsyncAction is just an abstraction over Action that provides a way to structure your action.

You typically use an Async action when you have to deal with operations that are asynchronous. In the most common scenario, you dispatch the initial action, execute an operation in the side effect and then dispatch a completed/failed action based on the result of the operation. If the action has a concept of progress, you can also dispatch progress actions.

This protocol has as the main goal to standardize the way async actions are managed.

It is important to note that this is just a a convenience approach to something you can do anyway. You can for instance create three actions: performOperation, OperationCompleted and OperationFailed and achieve the same result. AsyncAction just provide a way to abstract and simplify the process

Tip & Tricks

Since the Action protocol is very generic when it comes to the state type that should be updated, a pattern we want to promote is to put in your application a protocol like the following:

protocol AppAsyncAction: AsyncAction {
 func updatedStateForLoading(currentState: inout AppState)
 // same for completed, failed and progress
}

extension AppAsyncAction {
 func updatedStateForLoading(currentState: State) -> State {
   guard var state = state as? AppState else {
     fatalError("Something went wrong")
   }

   self.updatedStateForLoading(currentState: &state)
   return state
 }

 // same for completed, failed and progress
}

In this way you can save a lot of code since you can use your actions in the following way

struct A: AppAsyncAction {
 func updatedStateForLoading(currentState: inout AppState) {
   state.props = action.payload
 }

 // same for completed, failed and progress
}
  • UpdateState function that will be used when the state of the action is loading

    Declaration

    Swift

    func updatedStateForLoading(currentState: State) -> State

    Parameters

    state

    the current state of the application

    Return Value

    the new state

  • UpdateState function that will be used when the state of the action is completed

    Declaration

    Swift

    func updatedStateForCompleted(currentState: State) -> State

    Parameters

    state

    the current state of the application

    Return Value

    the new state

  • UpdateState function that will be used when the state of the action is failed

    Declaration

    Swift

    func updatedStateForFailed(currentState: State) -> State

    Parameters

    state

    the current state of the application

    Return Value

    the new state

  • UpdateState function that will be used when the state of the action is progress

    Declaration

    Swift

    func updatedStateForProgress(currentState: State) -> State

    Parameters

    state

    the current state of the application

    Return Value

    the new state

  • completedAction(_:) Default implementation

    Creates a new action in the completed state

    Default Implementation

    Creates a new action in the completed state

    Declaration

    Swift

    func completedAction(_ configuration: (inout Self) -> ()) -> Self

    Parameters

    configuration

    a block that updates the loading action with the information of the completion

    Return Value

    a new instance of the action, where the state is completed. This new action will have the loading payload inerithed from the initial loading action and the provided additional information

  • failedAction(_:) Default implementation

    Creates a new action in the failed state

    Default Implementation

    Creates a new action in the failed state

    Declaration

    Swift

    func failedAction(_ configuration: (inout Self) -> ()) -> Self

    Parameters

    configuration

    a block that updates the loading action with the information of the failure

    Return Value

    a new instance of the action, where the state is failed. This new action will have the loading payload inerithed from the initial loading action and the provided additional information

  • progressAction(percentage:) Default implementation

    Creates a new action in the progress state

    Default Implementation

    Creates a new action in the progress state

    Declaration

    Swift

    func progressAction(percentage: Double) -> Self

    Parameters

    progress

    the progress amount to set

    Return Value

    a new instance of the action, where the state is progress. This new action will have the loading payload inerithed from the initial loading action and the defined progress value