Integrating commands
Learn how to use Cmd.map and Cmd.batch to compose Cmds from independent MVU states
In Splitting into independent MVU states, we learned how to decompose larger apps into individual MVU states by using simple init and update functions returning only a Model, like in those signatures:
Though most apps will need more complex signatures to handle side effects such as processing data, network calls, access to the camera or storage, etc. Those apps will use commands to model those side-effects:
If we simply do the same than in the previous section, we will face the same compilation issue we saw by composing views with different Msg types.
We solved that issue with View.map previously. Since the issue is similar, the solution is also similar.
Converting a command's Msg type
Just like View.map to convert a widget's Msg type to the parent's Msg type to allow for composition, Fabulous has Cmd.map to do the same for commands.
Using Cmd.map is very similar to View.map.
Batching several Cmds together
Sometimes, an app needs to execute several side effects at the same time. To enable that, Fabulous provides a Cmd.batch function to merge several commands into a single one.
We can combine Cmd.batch with Cmd.map as we want.
To learn more about integrating commands, read The Elmish Book - Integrating Commands.
Last updated