Patrick Steiger
10/03/2023, 10:57 PMCoroutineScope
to composables will cause composables to never skip recompositions since CoroutineScope
is unstable. Is there a recommended pattern here? Remembering all the lambdas is just awkward@Immutable
class StableCoroutineScope(
scope: CoroutineScope
): CoroutineScope by scope
@Composable
fun rememberStableCoroutineScope(): StableCoroutineScope {
val scope = rememberCoroutineScope()
return remember(scope) {
StableCoroutineScope(scope)
}
}
Works to avoid recompositions, but feels weird (and I’m not 100% sure on the correctness of the Immutable annotation in this context since the coroutineContext elements are potentially mutable themselves, although I don’t think that e.g mutating Job
state matters for recompositions)ascii
10/04/2023, 2:18 AM@Stable/@Immutable
are mostly to bridge the gap between Compose compiler & whatever lies outside its vision.
While there are valid cases to force something you know to be stable, it just makes testing harder. What does the lambda do exactly? Maybe it can be done some other way, e.g. LaunchedEffect.Patrick Steiger
10/04/2023, 2:39 AMonClick = {
scope.launch {
flow.emit(Something)
}
}
ascii
10/04/2023, 2:48 AMLaunchedEffect(sink.size)
.
Otherwise I'd just do remember(scope)
everywhere. Won't bother with wrapping it.Zach Klippenstein (he/him) [MOD]
10/04/2023, 4:20 PMtryEmit
exists on some flowsPatrick Steiger
10/04/2023, 4:37 PM