-
Notifications
You must be signed in to change notification settings - Fork 3
Action
Abhi Muktheeswarar edited this page Jul 6, 2021
·
3 revisions
interface Action
An action represents what we want to do, what we want to achieve. Actions are the only way to update the state. An Action
can be triggered by the user on clicking a button in the UI, or from a SideEffect
, or Middleware
.
sealed interface MovieAction : Action {
object GetMoviesAction : MovieAction
data class MoviesLoadedAction(val movies: List<Movie>) : MovieAction
data class ErrorLoadingMoviesAction(val error: Exception) : MovieAction
}
We recommend Action
to be an immutable data class or object
.
For example:
- On opening the app, we dispatch
GetMoviesAction
. - A
SideEffect
on receiving theGetMoviesAction
, makes an API call. - Once the API response is received, the
SideEffect
dispatchesMoviesLoadedAction
if the network call is a success, otherwise dispatchesErrorLoadingMoviesAction
.
typealias Dispatch = (Action) -> Unit
Dispatch is just a simple function that accepts an Action
to dispatch it to a StateReserve
.
Flywheel comes with a few predefined actions:
-
EventAction
: For one-off events like showing a toast. -
NavigateAction
: To handle navigation-related flows. -
SkipReducer
: can be used along with askipMiddleware
to prevent theAction
from reaching a reducer.
All the above actions are just an empty interface. It is provided to distinguish or do something based on types.