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

Commit 97a2df5b authored by Steve Elliott's avatar Steve Elliott
Browse files

[kairos] Rename GroupedEvents -> KeyedEvents

Flag: com.android.systemui.status_bar_mobile_icon_kairos
Test: kairos-tests
Change-Id: I12a80e19255b947808660cca5706f3a403bb4c74
parent 5c9d588e
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ import com.android.systemui.activated
import com.android.systemui.kairos.BuildScope
import com.android.systemui.kairos.Events
import com.android.systemui.kairos.ExperimentalKairosApi
import com.android.systemui.kairos.GroupedEvents
import com.android.systemui.kairos.Incremental
import com.android.systemui.kairos.KeyedEvents
import com.android.systemui.kairos.State
import com.android.systemui.kairos.TransactionScope
import com.android.systemui.kairos.asIncremental
@@ -84,13 +84,13 @@ constructor(
            event?.let { (event.subId ?: lastSeenSubId.sample())?.let { it to event } }
        }

    private val mobileEventsBySubId: GroupedEvents<Int, FakeNetworkEventModel> =
    private val mobileEventsBySubId: KeyedEvents<Int, FakeNetworkEventModel> =
        mobileEventsWithSubId.map { mapOf(it) }.groupByKey()

    private val carrierMergedEvents: Events<FakeWifiEventModel.CarrierMerged> =
        wifiEvents.filterIsInstance<FakeWifiEventModel.CarrierMerged>()

    private val wifiEventsBySubId: GroupedEvents<Int, FakeWifiEventModel.CarrierMerged> =
    private val wifiEventsBySubId: KeyedEvents<Int, FakeWifiEventModel.CarrierMerged> =
        carrierMergedEvents.groupBy { it.subscriptionId }

    private val lastSeenSubId: State<Int?> = buildState {
+1 −1
Original line number Diff line number Diff line
@@ -186,7 +186,7 @@ fun <A, B> Events<A>.map(transform: TransactionScope.(A) -> B): Events<B> {

/**
 * Like [map], but the emission is not cached during the transaction. Use only if [transform] is
 * fast and pure.
 * fast and pure. If you are unsure if you need this, then you should prefer [map].
 *
 * @sample com.android.systemui.kairos.KairosSamples.mapCheap
 * @see map
+9 −9
Original line number Diff line number Diff line
@@ -26,12 +26,12 @@ import com.android.systemui.kairos.util.maybeSecond
import com.android.systemui.kairos.util.orError

/**
 * Returns a [GroupedEvents] that can be used to efficiently split a single [Events] into multiple
 * Returns a [KeyedEvents] that can be used to efficiently split a single [Events] into multiple
 * downstream [Events].
 *
 * The input [Events] emits [Map] instances that specify which downstream [Events] the associated
 * value will be emitted from. These downstream [Events] can be obtained via
 * [GroupedEvents.eventsForKey].
 * [KeyedEvents.eventsForKey].
 *
 * An example:
 * ```
@@ -48,18 +48,18 @@ import com.android.systemui.kairos.util.orError
 *
 * The optional [numKeys] argument is an optimization used to initialize the internal [HashMap].
 *
 * Note that the returned [GroupedEvents] should be cached and re-used to gain the performance
 * Note that the returned [KeyedEvents] should be cached and re-used to gain the performance
 * benefit.
 *
 * @sample com.android.systemui.kairos.KairosSamples.groupByKey
 * @see selector
 */
@ExperimentalKairosApi
fun <K, A> Events<Map<K, A>>.groupByKey(numKeys: Int? = null): GroupedEvents<K, A> =
    GroupedEvents(demuxMap({ init.connect(this) }, numKeys))
fun <K, A> Events<Map<K, A>>.groupByKey(numKeys: Int? = null): KeyedEvents<K, A> =
    KeyedEvents(demuxMap({ init.connect(this) }, numKeys))

/**
 * Returns a [GroupedEvents] that can be used to efficiently split a single [Events] into multiple
 * Returns a [KeyedEvents] that can be used to efficiently split a single [Events] into multiple
 * downstream [Events]. The downstream [Events] are associated with a [key][K], which is derived
 * from each emission of the original [Events] via [extractKey].
 *
@@ -77,7 +77,7 @@ fun <K, A> Events<Map<K, A>>.groupByKey(numKeys: Int? = null): GroupedEvents<K,
fun <K, A> Events<A>.groupBy(
    numKeys: Int? = null,
    extractKey: TransactionScope.(A) -> K,
): GroupedEvents<K, A> = map { mapOf(extractKey(it) to it) }.groupByKey(numKeys)
): KeyedEvents<K, A> = map { mapOf(extractKey(it) to it) }.groupByKey(numKeys)

/**
 * A mapping from keys of type [K] to [Events] emitting values of type [A].
@@ -85,7 +85,7 @@ fun <K, A> Events<A>.groupBy(
 * @see groupByKey
 */
@ExperimentalKairosApi
class GroupedEvents<in K, out A> internal constructor(internal val impl: DemuxImpl<K, A>) {
class KeyedEvents<in K, out A> internal constructor(internal val impl: DemuxImpl<K, A>) {
    /**
     * Returns an [Events] that emits values of type [A] that correspond to the given [key].
     *
@@ -121,7 +121,7 @@ class GroupedEvents<in K, out A> internal constructor(internal val impl: DemuxIm
fun <A> Events<A>.partition(
    predicate: TransactionScope.(A) -> Boolean
): Pair<Events<A>, Events<A>> {
    val grouped: GroupedEvents<Boolean, A> = groupBy(numKeys = 2, extractKey = predicate)
    val grouped: KeyedEvents<Boolean, A> = groupBy(numKeys = 2, extractKey = predicate)
    return Pair(grouped.eventsForKey(true), grouped.eventsForKey(false))
}

+1 −1
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ fun <A> State<A>.selector(numDistinctValues: Int? = null): StateSelector<A> =
class StateSelector<in A>
internal constructor(
    private val upstream: State<A>,
    private val groupedChanges: GroupedEvents<A, Boolean>,
    private val groupedChanges: KeyedEvents<A, Boolean>,
) {
    /**
     * Returns a [State] that tracks whether the upstream [State] is currently holding the given
+2 −2
Original line number Diff line number Diff line
@@ -25,9 +25,9 @@ import com.android.systemui.kairos.EffectScope
import com.android.systemui.kairos.EventProducerScope
import com.android.systemui.kairos.Events
import com.android.systemui.kairos.EventsInit
import com.android.systemui.kairos.GroupedEvents
import com.android.systemui.kairos.KairosCoroutineScope
import com.android.systemui.kairos.KairosNetwork
import com.android.systemui.kairos.KeyedEvents
import com.android.systemui.kairos.LocalNetwork
import com.android.systemui.kairos.MutableEvents
import com.android.systemui.kairos.TransactionScope
@@ -204,7 +204,7 @@ internal class BuildScopeImpl(val stateScope: StateScopeImpl, val coroutineScope
        initialSpecs: DeferredValue<Map<K, BuildSpec<B>>>,
        numKeys: Int?,
    ): Pair<Events<Map<K, Maybe<A>>>, DeferredValue<Map<K, B>>> {
        val eventsByKey: GroupedEvents<K, Maybe<BuildSpec<A>>> = groupByKey(numKeys)
        val eventsByKey: KeyedEvents<K, Maybe<BuildSpec<A>>> = groupByKey(numKeys)
        val initOut: Lazy<Map<K, B>> = deferAsync {
            initialSpecs.unwrapped.value.mapValues { (k, spec) ->
                val newEnd = eventsByKey[k]
Loading