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

commonMain.pro.respawn.flowmvi.modules.PluginModule.kt Maven / Gradle / Ivy

Go to download

A Kotlin Multiplatform MVI library based on plugins that is simple, fast, powerful & flexible

There is a newer version: 3.0.0
Show newest version
package pro.respawn.flowmvi.modules

import kotlinx.coroutines.CoroutineScope
import pro.respawn.flowmvi.api.MVIAction
import pro.respawn.flowmvi.api.MVIIntent
import pro.respawn.flowmvi.api.MVIState
import pro.respawn.flowmvi.api.PipelineContext
import pro.respawn.flowmvi.api.StorePlugin
import pro.respawn.flowmvi.plugins.AbstractStorePlugin

internal class PluginModule(
    private val plugins: Set>,
) : AbstractStorePlugin(null) {

    override suspend fun PipelineContext.onStart(): Unit = plugins { onStart() }
    override fun onStop(e: Exception?): Unit = plugins { onStop(e) }
    override suspend fun PipelineContext.onState(old: S, new: S): S? = plugins(new) { onState(old, it) }
    override suspend fun PipelineContext.onIntent(intent: I): I? = plugins(intent) { onIntent(it) }
    override suspend fun PipelineContext.onAction(action: A): A? = plugins(action) { onAction(it) }
    override suspend fun PipelineContext.onException(e: Exception): Exception? = plugins(e) { onException(it) }
    override fun PipelineContext.onUnsubscribe(
        subscriberCount: Int
    ) = plugins { onUnsubscribe(subscriberCount) }

    override fun PipelineContext.onSubscribe(
        subscriberScope: CoroutineScope,
        subscriberCount: Int
    ) = plugins { onSubscribe(subscriberScope, subscriberCount) }

    private inline fun plugins(block: StorePlugin.() -> Unit) = plugins.forEach(block)
    private inline fun  plugins(
        initial: R,
        block: StorePlugin.(R) -> R?
    ) = plugins.fold<_, R?>(initial) { acc, it -> it.block(acc ?: return@plugins acc) }

    inline operator fun invoke(block: StorePlugin.() -> Unit) = block()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy