Skip to content

Commit

Permalink
Run block in LaunchRegistration off the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowsky committed Feb 23, 2025
1 parent c5b0f99 commit bbcf050
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.animate
import androidx.compose.animation.core.tween
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
Expand All @@ -46,8 +46,11 @@ import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

internal val defaultCartesianDiffAnimationSpec: AnimationSpec<Float> =
tween(durationMillis = Animation.DIFF_DURATION)
Expand Down Expand Up @@ -164,9 +167,11 @@ private fun LaunchRegistration(
if (isInPreview) {
runBlocking(getCoroutineContext(isPreview = true)) { block() }
} else {
DisposableEffect(chartID, animateIn) {
val disposable = block()
onDispose { disposable() }
LaunchedEffect(chartID, animateIn) {
withContext(getCoroutineContext(isPreview = false)) {
val disposable = block()
currentCoroutineContext().job.invokeOnCompletion { disposable() }
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.animate
import androidx.compose.animation.core.tween
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
Expand All @@ -45,8 +45,11 @@ import kotlin.uuid.Uuid
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

/** Stores a [CartesianChart]’s data. */
public class CartesianChartModel {
Expand Down Expand Up @@ -231,9 +234,11 @@ private fun LaunchRegistration(
if (isInPreview) {
runBlocking(getCoroutineContext(isPreview = true)) { block() }
} else {
DisposableEffect(chartID, animateIn) {
val disposable = block()
onDispose { disposable() }
LaunchedEffect(chartID, animateIn) {
withContext(getCoroutineContext(isPreview = false)) {
val disposable = block()
currentCoroutineContext().job.invokeOnCompletion { disposable() }
}
}
}
}
Expand Down

0 comments on commit bbcf050

Please sign in to comment.