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

me.aartikov.sesame.loading.simple.internal.LoadingImpl.kt Maven / Gradle / Ivy

package me.aartikov.sesame.loading.simple.internal

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.StateFlow
import me.aartikov.sesame.loading.simple.Loading
import me.aartikov.sesame.loading.simple.Loading.Event
import me.aartikov.sesame.loading.simple.Loading.State
import me.aartikov.sesame.loop.ActionSource
import me.aartikov.sesame.loop.EffectHandler
import me.aartikov.sesame.loop.startIn

internal class LoadingImpl(
    scope: CoroutineScope,
    loadingEffectHandler: EffectHandler, Action>,
    loadingActionSource: ActionSource>?,
    initialState: State
) : Loading {

    private val mutableEventFlow = MutableSharedFlow>(
        extraBufferCapacity = 100, onBufferOverflow = BufferOverflow.DROP_OLDEST
    )

    private val loop: LoadingLoop = LoadingLoop(
        initialState = initialState,
        reducer = LoadingReducer(),
        effectHandlers = listOf(
            loadingEffectHandler,
            EventEffectHandler { event -> mutableEventFlow.tryEmit(event) }
        ),
        actionSources = listOfNotNull(
            loadingActionSource
        )
    )

    override val stateFlow: StateFlow>
        get() = loop.stateFlow

    override val eventFlow: Flow>
        get() = mutableEventFlow

    init {
        loop.startIn(scope)
    }

    override fun load(fresh: Boolean, reset: Boolean) {
        loop.dispatch(Action.Load(fresh, reset))
    }

    override fun cancel(reset: Boolean) {
        loop.dispatch(Action.Cancel(reset))
    }

    override fun mutateData(transform: (T) -> T) {
        loop.dispatch(Action.MutateData(transform))
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy