Any code examples on how to handle side effects us...
# ballast
a
Any code examples on how to handle side effects using ballast e.g. making calls to an API? Also, I noticed that there's an example section in the ballast docs but I couldn't find any links to the repository. 🤷🏾‍♂️
c
It looks like I broke something in the docs, I’ll get that fixed shortly. The Navigation example has each example available for use, and the sources for the examples are in the main rep here.
For making API calls and things like that, I’d recommend using the Repository Pattern, where the actual API call is done in a separate class from the ViewModel, and all the logic and data-mapping necessary to make that call is contained within the Repository class. The ViewModel should only pass into the Repository what is necessary, and the Repository can look up the rest of the data itself. Since the
InputHandler.handleInput()
method is suspending, you can make the Repository call directly within the Handler function, waiting for its result and setting it back to the VM state. If it’s more of a “fire-and-forget” API call, you can wrap the call to the Repository in a
sideJob
block to run it in parallel to the Inputs queue, rather than suspending the queue.
a
Alright. I'll look into it. Thanks... 👍🏾
c
Documentation is now updated, and the examples now include links to the relevant sources. For example, https://copper-leaf.github.io/ballast/wiki/examples/counter/