All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.plusmobileapps.firebase.auth.util.DerivedStateFlow.kt Maven / Gradle / Ivy

Go to download

A kotlin multiplatform mobile library for authenticating with Firebase for Android, iOS, and JVM

There is a newer version: 0.4
Show newest version
package com.plusmobileapps.firebase.auth.util

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

/**
 * Does not produce the same value in a raw, so respect "distinct until changed emissions"
 * */
class DerivedStateFlow(
    private val getValue: () -> T,
    private val flow: Flow
) : StateFlow {

    override val replayCache: List
        get () = listOf(value)

    override val value: T
        get () = getValue()

    override suspend fun collect(collector: FlowCollector): Nothing {
        coroutineScope { flow.distinctUntilChanged().stateIn(this).collect(collector) }
    }
}

fun  StateFlow.mapState(transform: (a: T1) -> R): StateFlow {
    return DerivedStateFlow(
        getValue = { transform(this.value) },
        flow = this.map { a -> transform(a) }
    )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy