Skip to content

Commit b0830be

Browse files
committed
Synchronously increment loading counter before coroutine launch to avoid UI flickering in edge cases
1 parent 8729930 commit b0830be

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Renamed modules: `reactivestate` -> `reactivestate-android`, `reactivestate-test` -> `reactivestate-android-test`. You might only want to use `reactivestate-compose` in case you don't need to add ViewModels to Activities/Fragments.
1414
* Fixed `MutableStateFlow.compareAndSet` for `.toMutable`/`.beforeUpdate`/`.afterUpdate`.
1515
* Added `Flow.stateOnDemand` variant with `initial` value.
16+
* `CoroutineLauncher.launch` now synchronously increments the loading counter to avoid UI flickering issues in edge cases.
1617

1718
## 5.13.0
1819

reactivestate-core/src/commonMain/kotlin/com/ensody/reactivestate/CoroutineLauncher.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ public interface CoroutineLauncher {
5656
withLoading: MutableStateFlow<Int>? = loading,
5757
onError: (suspend (Throwable) -> Unit)? = null,
5858
block: suspend CoroutineScope.() -> Unit,
59-
): Job =
60-
rawLaunch(context = context, start = start) {
61-
track(withLoading = withLoading, onError = onError) {
62-
block()
59+
): Job {
60+
// Increment before coroutine launch to avoid UI flickering in edge cases
61+
withLoading?.increment()
62+
return rawLaunch(context = context, start = start) {
63+
withErrorReporting({ onError?.invoke(it) ?: onError(it) }) {
64+
try {
65+
block()
66+
} finally {
67+
withLoading?.decrement()
68+
}
6369
}
6470
}
71+
}
6572

6673
/**
6774
* Tracks a suspension [block]'s loading state and errors.

0 commit comments

Comments
 (0)