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

Commit ad777fc2 authored by Steve Elliott's avatar Steve Elliott Committed by Android (Google) Code Review
Browse files

Merge "[kairos] Add logging to facilitate debugging" into main

parents fea9a83d a5529853
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ internal class LocalFrpNetwork(
    override suspend fun <R> transact(block: suspend FrpTransactionScope.() -> R): R {
        val result = CompletableDeferred<R>(coroutineContext[Job])
        @Suppress("DeferredResultUnused")
        network.transaction {
        network.transaction("FrpNetwork.transact") {
            val buildScope =
                BuildScopeImpl(
                    stateScope = StateScopeImpl(evalScope = this, endSignal = endSignal),
@@ -151,7 +151,7 @@ internal class LocalFrpNetwork(
    override suspend fun activateSpec(spec: FrpSpec<*>) {
        val job =
            network
                .transaction {
                .transaction("FrpNetwork.activateSpec") {
                    val buildScope =
                        BuildScopeImpl(
                            stateScope = StateScopeImpl(evalScope = this, endSignal = endSignal),
@@ -166,7 +166,8 @@ internal class LocalFrpNetwork(
    override fun <In, Out> coalescingMutableTFlow(
        coalesce: (old: Out, new: In) -> Out,
        getInitialValue: () -> Out,
    ): CoalescingMutableTFlow<In, Out> = CoalescingMutableTFlow(coalesce, network, getInitialValue)
    ): CoalescingMutableTFlow<In, Out> =
        CoalescingMutableTFlow(null, coalesce, network, getInitialValue)

    override fun <T> mutableTFlow(): MutableTFlow<T> = MutableTFlow(network)

+5 −3
Original line number Diff line number Diff line
@@ -467,12 +467,12 @@ fun <A> TState<TFlow<A>>.switchPromptly(): TFlow<A> {
@ExperimentalFrpApi
class CoalescingMutableTFlow<In, Out>
internal constructor(
    internal val name: String?,
    internal val coalesce: (old: Out, new: In) -> Out,
    internal val network: Network,
    private val getInitialValue: () -> Out,
    internal val impl: InputNode<Out> = InputNode(),
) : TFlow<Out>() {
    internal val name: String? = null
    internal val storage = AtomicReference(false to getInitialValue())

    override fun toString(): String = "${this::class.simpleName}@$hashString"
@@ -490,7 +490,7 @@ internal constructor(
        val (scheduled, _) = storage.getAndUpdate { (_, old) -> true to coalesce(old, value) }
        if (!scheduled) {
            @Suppress("DeferredResultUnused")
            network.transaction {
            network.transaction("CoalescingMutableTFlow${name?.let { "($name)" }.orEmpty()}.emit") {
                impl.visit(this, storage.getAndSet(false to getInitialValue()).second)
            }
        }
@@ -524,7 +524,9 @@ internal constructor(internal val network: Network, internal val impl: InputNode
            val newEmit =
                async(start = CoroutineStart.LAZY) {
                    jobOrNull?.join()
                    network.transaction { impl.visit(this, value) }.await()
                    network
                        .transaction("MutableTFlow($name).emit") { impl.visit(this, value) }
                        .await()
                }
            jobOrNull = storage.getAndSet(newEmit)
            newEmit.await()
+2 −1
Original line number Diff line number Diff line
@@ -457,6 +457,7 @@ internal constructor(internal val network: Network, initialValue: Deferred<T>) :

    private val input: CoalescingMutableTFlow<Deferred<T>, Deferred<T>?> =
        CoalescingMutableTFlow(
            name = null,
            coalesce = { _, new -> new },
            network = network,
            getInitialValue = { null },
@@ -474,7 +475,7 @@ internal constructor(internal val network: Network, initialValue: Deferred<T>) :
                .cached()
        state = TStateSource(name, operatorName, initialValue, calm)
        @Suppress("DeferredResultUnused")
        network.transaction {
        network.transaction("MutableTState.init") {
            calm.activate(evalScope = this, downstream = Schedulable.S(state))?.let {
                (connection, needsEval) ->
                state.upstreamConnection = connection
+9 −5
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ internal class BuildScopeImpl(val stateScope: StateScopeImpl, val coroutineScope
        builder: suspend S.() -> Unit,
    ): TFlow<A> {
        var job: Job? = null
        val stopEmitter = newStopEmitter()
        val stopEmitter = newStopEmitter("buildTFlow")
        // Create a child scope that will be kept alive beyond the end of this transaction.
        val childScope = coroutineScope.childScope()
        lateinit var emitter: Pair<T, S>
@@ -131,7 +131,8 @@ internal class BuildScopeImpl(val stateScope: StateScopeImpl, val coroutineScope
    ): TFlow<Out> =
        buildTFlow(
            constructFlow = { inputNode ->
                val flow = CoalescingMutableTFlow(coalesce, network, getInitialValue, inputNode)
                val flow =
                    CoalescingMutableTFlow(null, coalesce, network, getInitialValue, inputNode)
                flow to
                    object : FrpCoalescingProducerScope<In> {
                        override fun emit(value: In) {
@@ -165,7 +166,9 @@ internal class BuildScopeImpl(val stateScope: StateScopeImpl, val coroutineScope
            subRef.getAndSet(None)?.let { output ->
                if (output is Just) {
                    @Suppress("DeferredResultUnused")
                    network.transaction { scheduleDeactivation(output.value) }
                    network.transaction("observeEffect cancelled") {
                        scheduleDeactivation(output.value)
                    }
                }
            }
        }
@@ -266,8 +269,9 @@ internal class BuildScopeImpl(val stateScope: StateScopeImpl, val coroutineScope
        return changes to FrpDeferredValue(initOut)
    }

    private fun newStopEmitter(): CoalescingMutableTFlow<Unit, Unit> =
    private fun newStopEmitter(name: String): CoalescingMutableTFlow<Unit, Unit> =
        CoalescingMutableTFlow(
            name = name,
            coalesce = { _, _: Unit -> },
            network = network,
            getInitialValue = {},
@@ -293,7 +297,7 @@ internal class BuildScopeImpl(val stateScope: StateScopeImpl, val coroutineScope
    }

    private fun mutableChildBuildScope(): BuildScopeImpl {
        val stopEmitter = newStopEmitter()
        val stopEmitter = newStopEmitter("mutableChildBuildScope")
        val childScope = coroutineScope.childScope()
        childScope.coroutineContext.job.invokeOnCompletion { stopEmitter.emit(Unit) }
        // Ensure that once this transaction is done, the new child scope enters the completing
+3 −2
Original line number Diff line number Diff line
@@ -118,12 +118,12 @@ internal class Network(val coroutineScope: CoroutineScope) : NetworkScope {
    }

    /** Evaluates [block] inside of a new transaction when the network is ready. */
    fun <R> transaction(block: suspend EvalScope.() -> R): Deferred<R> =
    fun <R> transaction(reason: String, block: suspend EvalScope.() -> R): Deferred<R> =
        CompletableDeferred<R>(parent = coroutineScope.coroutineContext.job).also { onResult ->
            val job =
                coroutineScope.launch {
                    inputScheduleChan.send(
                        ScheduledAction(onStartTransaction = block, onResult = onResult)
                        ScheduledAction(reason, onStartTransaction = block, onResult = onResult)
                    )
                }
            onResult.invokeOnCompletion { job.cancel() }
@@ -222,6 +222,7 @@ internal class Network(val coroutineScope: CoroutineScope) : NetworkScope {
}

internal class ScheduledAction<T>(
    val reason: String,
    private val onResult: CompletableDeferred<T>? = null,
    private val onStartTransaction: suspend EvalScope.() -> T,
) {