I have 2 UI events that update UI state triggering...
# compose
e
I have 2 UI events that update UI state triggering simultaneously in one go. For example, I want to update my state on focus of a certain UI element, and on blur (focus lost) of another. This can happen easily if the user clicks/taps/tabs from one text field to another. Both UI elements update the state when they are focused/blurred, so in 1 user action 2 state updates are triggered. That led to a race condition: both events would call a callback with a new state, and the last one would set the state to the final value, overwriting the state set by the first one. I do this by passing state into my composable functions and a callback. I
copy
the state and pass it to the callback. I've now refactored the callback to not take a state
S
, but take a
S.() -> S
lambda instead. This leads to lambdas in lambdas everywhere. While it's not a big issue (and Kotlin's last-lambda-argument passing helps a lot here), I wonder: Is there a different idiomatic Kotlin+compose way to handle this situation? And how would you name the callback? First I had something like
onNewState: (S) -> Unit
, but now it's more verbose:
onNewStateTransform: S.() -> S
. Any better name suggestions?
z
@Ralston Da Silva do we have a best practice to recommend for coalescing focus events like this?
e
Hey @Zach Klippenstein (he/him) [MOD] or @Ralston Da Silva, I'd appreciate any additional thoughts you might have on the topic. Thank you.