https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

João Eudes Lima

01/04/2021, 2:14 AM
image.png
I could implement this in a more beautiful way, but the StateFlowImpl class is private (I didn't understand why it is private)
g

gildor

01/04/2021, 4:47 AM
It's private because it's implementation detail, you should use public builders, also you always can implement own with delegation
Your implementation is incorrect, it will not work, you never subscribe on state flow
Not quite sure what is your final goal with this (do you really need initial value or not (because every state already have default value. Looks that you just need:
Copy code
listSources.asFlow().flattenMerge().collect { doSomethingWiht(it) }
but maybe I misunderstood you case, becayse example above will cause inifinity cycle (on every event update you update it again which will cause one more update)
but your description “way to maintain multiple StateFlow related to an event? Reactive, to modifications to any of the states” is pretty clear and flattenMerge should work for it, but looks that you want modify it, which is not so clear for me
b

bezrukov

01/04/2021, 5:59 AM
according to the first screenshot, combine + stateIn enough:
Copy code
val risk = combine(odd, bankMoney, comission, typeBet) { odd, money, comission, type -> 
    calcRisk(...)
}.stateIn(scope, 0)
j

João Eudes Lima

01/04/2021, 1:18 PM
@gildor, yeah hahah it didn't work as I expected, sorry, I just wanted to give you the idea of ​​what it would be like
@bezrukov I didn’t know this combine function with varargs, just the method inside Flow, Thanks, I imagine that’s the solution
@bezrukov "combine" do not respond with the "single ()" method, any solution?
g

gildor

01/05/2021, 4:46 AM
why do you need single()?
could you show full example what you are trying to achieve
you can use https://play.kotlinlang.org/ to show example and demonstrate what doesn’t work for you
j

João Eudes Lima

01/05/2021, 6:49 PM
@gildor because i need get the value only once
g

gildor

01/06/2021, 2:12 AM
yes, but what is not working? Maybe some of your flows in combine doesn’t emit first value
3 Views