-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] State Collection On Multiple Screens #654
Comments
Hello,
is the same as val myMflow = flowOf {
emit(1)
delay(10_000) // wait for 10 seconds
emit(2)
}
launch {
myFlow.collect { println("first: $it") }
}
// "Sharing" myFlow variable and calling collect again
launch {
delay (1_000) // wait for 1 second
myFlow.collect { println("second: $it") }
} The output is:
So you are not really "reusing" and sharing The same is happening with FlowRedux state machines: val myStateMachine = SomeFlowReduxStateMachine(initalState = MyInitialState).
launch {
myStateMachine.state.collect { state -> println("first: $state")
delay (100) // just to mimic user input happened somewhen later:
myStateMachine.dispatch(MyActionThatTriggersStateTransitionToSecondState)
}
launch {
delay(2_000) // wait for 2 seconds so that MyActionThatTriggersStateTransitionToSecondState is dispatched:
myStateMachine.state.collect { state -> println("second: $state")
} Guess what the output is?
Again, the reason is that So sharing the To answer your original question:
Yes, it is save, because you are creating a new How would you like to overcome this? Probably write a wrapper around your FlowReduxStateMachine that uses I think @gabrielittner has such a FlowReduxStateMachine implementation that wraps around Also, one more thing to watch out for: Maybe in a 2.0 release version the class // potential FlowRedux 2.0 public API, just in my head, not synced or aligned with any other maintainer yet
class MyFlowReduxStateMachineFactory : FlowReduxStateMachineFactory<MyState, MyAction> {
init {
spec {
inState { ... } // all the existing stuff from today
}
}
// instead of a val state : MyState and suspend dispatch(...) function FlowReduxStateMachineFactory only has a `newInstance()` function
fun newInstance() : Pair<Flow<MyState>, DispatchFunction> = ...
} The usage would be: val myStateMachineFactory = MyFlowReduxStateMachineFactory()
state, dispatch = myStateMachineFactory.newInstance()
launch {
state.collect { state -> println(state) }
dispatch(MyAction)
} In that case |
Is it save co collect state on multiple screens from the same (injected) instance of state machine (potentially on different threads) and using both
collect
andrememberState
?e.g.
The text was updated successfully, but these errors were encountered: