Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Reducer that fetches the content (if not currently loading)
fun loadContent() = quantum.setState {
// Do not try to load the content while currently loading
// Returning this (the current / input state) signals the quantum that
// this reducer was a NOOP
if(isLoading) return@setState this
// Dispatch a async loading operation with myRepository (exemplary)
myRepository.loadContent
.onSuccess(::onContentLoaded)
.onError(::onError)
.execute()
// Copy the current state but set loading flag
copy(isLoading = true)
}
fun onContentLoaded(content: Content) = setState {
// Content loaded:
// Copy current state and clear any error
copy(content = content, error = null)
}
fun onError(error: Error) = setState {
// Copy current state but publish the error
copy(error = error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment