Closed tunjid closed 12 months ago
In the case where there is no collector of the produced state, can there be a way to stop composing the Presenter?
The StateFlow
created by launchMolecule
is fed by a coroutine residing in the calling CoroutineScope
. This coroutine runs the composition. If you kill that coroutine (e.g. by cancelling the Job
in that CoroutineScope
), it will stop composing. But there is no way to restart composition in that case.
If the above can be accommodated, can there be an API to seed the last produced state to presenters? This is so that resuming collecting from the presenter does not overwrite the last produced state value.
If you want to stop composition and restart it, that means managing the StateFlow
by hand instead of using Molecule's API. The approach you wrote is how you'd do that: use moleculeFlow
to create a cold flow that does its own composition, and then use stateIn
to share it into a StateFlow
. stateIn
provides various options for starting/stopping the driving coroutine depending on how many collectors it has.
Gotcha, if I understand correctly:
launchMolecule
is not cancelable and resumable by design. It mirrors CoroutineScope.launch
in this regard.moleculeFlow
and add their restart semantics on top of it.I'll close the issue, thank you.
There are two questions in this:
Coroutine
is active. In the case where there is no collector of the produced state, can there be a way to stop composing thePresenter
?Consider the
ProfilePresenter
example:The initial state is produced will always be
Loading
. In the case whereData
has been produced and theProfilePresenter
is no longer being composed because theStateFlow
is not being collected from, (maybe the owning screen has been placed in the back stack), I would like to prevent the last seenData
from immediately being overwritten byLoading
as I return to the screen and the presenter produces state yet again.i.e:
The above can be worked around by using
moleculeFlow
with theFlow
builder to allow seeding as theFlow
is cold, and then creating aStateFlow
with the rightSharingStarted
argument:Though I wonder if I'm missing something more obvious. If not, a built in API that allows for it would be really nice.