Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e9e71ffa authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes Ie6bb938b,I57d06aba into main

* changes:
  [kairos] guard against coroutine cancelation race
  [kairos] Fix casting error in ActivatedKairosSpec
parents 9e7c93bc 3b1ef1e9
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -41,20 +41,21 @@ fun <T> ActivatedKairosSpec(
    kairosNetwork: KairosNetwork,
    block: @Composable (T) -> Unit,
) {
    val uninit = Any()
    var state by remember { mutableStateOf<Any?>(uninit) }
    var state by remember { mutableStateOf<Any?>(Uninitialized) }
    LaunchedEffect(key1 = Unit) {
        kairosNetwork.activateSpec {
            val v = buildSpec.applySpec()
            launchEffect {
                state = v
                awaitClose { state = uninit }
                awaitClose { state = Uninitialized }
            }
        }
    }
    state.let {
        if (it !== uninit) {
        if (it !== Uninitialized) {
            @Suppress("UNCHECKED_CAST") block(it as T)
        }
    }
}

private object Uninitialized
+3 −3
Original line number Diff line number Diff line
@@ -113,11 +113,11 @@ internal class BuildScopeImpl(val stateScope: StateScopeImpl, val coroutineScope
        coroutineContext: CoroutineContext,
        block: EffectScope.(A) -> Unit,
    ): DisposableHandle {
        val subRef = AtomicReference<Maybe<Output<A>>>(null)
        val subRef = AtomicReference<Maybe<Output<A>>?>(null)
        val childScope = coroutineScope.childScope()
        lateinit var cancelHandle: DisposableHandle
        var cancelHandle: DisposableHandle? = null
        val handle = DisposableHandle {
            cancelHandle.dispose()
            cancelHandle?.dispose()
            subRef.getAndSet(Absent)?.let { output ->
                if (output is Present) {
                    @Suppress("DeferredResultUnused")