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

kernl.data.source.DataSourceExtensions.kt Maven / Gradle / Ivy

Go to download

Kernl: A Kotlin Symbol Processing (KSP) library for automatic repository generation.

There is a newer version: 0.0.1-beta6
Show newest version
package io.github.mattshoe.shoebox.kernl.data.source

import io.github.mattshoe.shoebox.kernl.data.DataResult
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map

fun  Flow>.unwrapDataResult(): Flow
    = filter {
        it is io.github.mattshoe.shoebox.kernl.data.DataResult.Success
    }.map {
        (it as io.github.mattshoe.shoebox.kernl.data.DataResult.Success).data
    }

fun  Flow>.catchDataResult(flowCollector: FlowCollector): Flow
    = filter {
        if (it is io.github.mattshoe.shoebox.kernl.data.DataResult.Error) {
            flowCollector.emit(it.error)
        }
        it is io.github.mattshoe.shoebox.kernl.data.DataResult.Success
    }.map {
        (it as io.github.mattshoe.shoebox.kernl.data.DataResult.Success).data
    }

fun  Flow>.onInvalidation(flowCollector: FlowCollector): Flow>
    = filter {
        if (it is io.github.mattshoe.shoebox.kernl.data.DataResult.Invalidated) {
            flowCollector.emit(Unit)
        }
        it is io.github.mattshoe.shoebox.kernl.data.DataResult.Success
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy