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

com.airbnb.mvrx.MavericksRepositoryExtensions.kt Maven / Gradle / Ivy

@file:Suppress("FunctionName")

package com.airbnb.mvrx

import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlin.reflect.KProperty1

@InternalMavericksApi
fun , S : MavericksState> Repository._internal(
    action: suspend (S) -> Unit
) = stateFlow.resolveSubscription(action)

@InternalMavericksApi
fun , S : MavericksState, A> Repository._internal1(
    prop1: KProperty1,
    action: suspend (A) -> Unit
) = stateFlow
    .map { MavericksTuple1(prop1.get(it)) }
    .distinctUntilChanged()
    .resolveSubscription() { (a) ->
        action(a)
    }

@InternalMavericksApi
fun , S : MavericksState, A, B> Repository._internal2(
    prop1: KProperty1,
    prop2: KProperty1,
    action: suspend (A, B) -> Unit
) = stateFlow
    .map { MavericksTuple2(prop1.get(it), prop2.get(it)) }
    .distinctUntilChanged()
    .resolveSubscription() { (a, b) ->
        action(a, b)
    }

@InternalMavericksApi
fun , S : MavericksState, A, B, C> Repository._internal3(
    prop1: KProperty1,
    prop2: KProperty1,
    prop3: KProperty1,
    action: suspend (A, B, C) -> Unit
) = stateFlow
    .map { MavericksTuple3(prop1.get(it), prop2.get(it), prop3.get(it)) }
    .distinctUntilChanged()
    .resolveSubscription() { (a, b, c) ->
        action(a, b, c)
    }

@InternalMavericksApi
fun , S : MavericksState, A, B, C, D> Repository._internal4(
    prop1: KProperty1,
    prop2: KProperty1,
    prop3: KProperty1,
    prop4: KProperty1,
    action: suspend (A, B, C, D) -> Unit
) = stateFlow
    .map { MavericksTuple4(prop1.get(it), prop2.get(it), prop3.get(it), prop4.get(it)) }
    .distinctUntilChanged()
    .resolveSubscription() { (a, b, c, d) ->
        action(a, b, c, d)
    }

@InternalMavericksApi
fun , S : MavericksState, A, B, C, D, E> Repository._internal5(
    prop1: KProperty1,
    prop2: KProperty1,
    prop3: KProperty1,
    prop4: KProperty1,
    prop5: KProperty1,
    action: suspend (A, B, C, D, E) -> Unit
) = stateFlow
    .map { MavericksTuple5(prop1.get(it), prop2.get(it), prop3.get(it), prop4.get(it), prop5.get(it)) }
    .distinctUntilChanged()
    .resolveSubscription() { (a, b, c, d, e) ->
        action(a, b, c, d, e)
    }

@InternalMavericksApi
fun , S : MavericksState, A, B, C, D, E, F> Repository._internal6(
    prop1: KProperty1,
    prop2: KProperty1,
    prop3: KProperty1,
    prop4: KProperty1,
    prop5: KProperty1,
    prop6: KProperty1,
    action: suspend (A, B, C, D, E, F) -> Unit
) = stateFlow
    .map { MavericksTuple6(prop1.get(it), prop2.get(it), prop3.get(it), prop4.get(it), prop5.get(it), prop6.get(it)) }
    .distinctUntilChanged()
    .resolveSubscription() { (a, b, c, d, e, f) ->
        action(a, b, c, d, e, f)
    }

@InternalMavericksApi
fun , S : MavericksState, A, B, C, D, E, F, G> Repository._internal7(
    prop1: KProperty1,
    prop2: KProperty1,
    prop3: KProperty1,
    prop4: KProperty1,
    prop5: KProperty1,
    prop6: KProperty1,
    prop7: KProperty1,
    action: suspend (A, B, C, D, E, F, G) -> Unit
) = stateFlow
    .map { MavericksTuple7(prop1.get(it), prop2.get(it), prop3.get(it), prop4.get(it), prop5.get(it), prop6.get(it), prop7.get(it)) }
    .distinctUntilChanged()
    .resolveSubscription() { (a, b, c, d, e, f, g) ->
        action(a, b, c, d, e, f, g)
    }

@InternalMavericksApi
fun , S : MavericksState, T> Repository._internalSF(
    asyncProp: KProperty1>,
    onFail: (suspend (Throwable) -> Unit)? = null,
    onSuccess: (suspend (T) -> Unit)? = null
) = _internal1(asyncProp) { asyncValue ->
    if (onSuccess != null && asyncValue is Success) {
        onSuccess(asyncValue())
    } else if (onFail != null && asyncValue is Fail) {
        onFail(asyncValue.error)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy