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
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
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
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
Parameters
state
the current state of the application
Return Value
the new state
-
completedAction(_:)
Default implementationCreates a new action in the
completed
stateDefault Implementation
Creates a new action in the
completed
stateDeclaration
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 implementationCreates a new action in the
failed
stateDefault Implementation
Creates a new action in the
failed
stateDeclaration
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 implementationCreates a new action in the
progress
stateDefault Implementation
Creates a new action in the
progress
stateDeclaration
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
-
updatedState(currentState:)
Extension methodCreates a new state starting from the current state and the dispatched action.
AsyncAction
implements a default behaviour that invokesupdatedStateForLoading(currentState:)
,updatedStateForCompleted(currentState:)
,updatedStateForFailed(currentState:)
orupdateStateForProgress(currentState:)
based on the action’s stateParameters
state
the current state
Return Value
the new state
-
debugDescription
Extension methodDeclaration
Swift
public var debugDescription: String { get }