Replies: 1 comment
-
Thanks for the suggestions and a detailed explanation. So my opinion on this discussion for now is:
Workarounds for now:
is ButtonClicked -> updateState {
copy(email = email.validate()).run { // notice we replaced the scope
if (!email.isValid) return@updateState this
authorizeAsync(..)
copy(isLoading = true)
}
} This code looks simpler, behaves in the same way, has less nesting and has no problems of the previous code. The downside is that it's harder to understand outside of the IDE. My resolution ideas:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Problem
FlowMVI provides useful functions to prevent state racing through
updateState
andwithState
functions, but sometimes the way it's declared can lead to potential problem overlooking. The problem is that the state is handled in the receiver of the lambda, not as a parameter.For example:
Possible solution
To avoid such problems, but within parameters in the chain of inner lambdas, Intellij Idea has dedicated inspection that warns when you have the same parameters names. For the receivers, there's no such validation or check (except of DSLs contexts) and it makes
updateState
andwithState
potentially problematic even in simple cases.Introducing the separate functions without such problem can help, but does not solve problem fully as we can't be sure that someone else will use the right function. In addition, it adds a new layer of complexity, as there's already
useState
,withState
andupdateState
that you should understand.What would I do?
DeprecationLevel.WARNING
level or withRequiresOptIn
(it will make able people to opt-in the error / warning on the project level and will not break existing code).Overall, I think everything except of DSLs builders that requires particular contexts to run, should be handled using parameters as it's more obvious in case I described and, for example, when looking on code on platforms like GitHub, but not in IDE.
Beta Was this translation helpful? Give feedback.
All reactions