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

kdux.tools.LoggingEnhancer.kt Maven / Gradle / Ivy

Go to download

Kdux is a Kotlin-based, platform-agnostic state management library that implements the Redux pattern, providing structured concurrency with built-in coroutine support. It is designed to integrate seamlessly with any Kotlin project, particularly excelling in Android applications using MVI architecture.

There is a newer version: 1.0.10
Show newest version
package kdux.tools

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import org.mattshoe.shoebox.kdux.Enhancer
import org.mattshoe.shoebox.kdux.Store

/**
 * An enhancer that logs every action dispatched to the store. The logging is handled
 * asynchronously, allowing you to log actions without blocking the dispatch process.
 *
 * This enhancer is useful for debugging or monitoring purposes, providing insight into
 * the actions being dispatched and allowing you to track how the state changes over time.
 *
 * @param log A suspendable function that takes an `Action` as a parameter and logs it.
 *            The logging function is called every time an action is dispatched.
 */
class LoggingEnhancer(
    private val log: suspend (Action) -> Unit
): Enhancer {

    override fun enhance(store: Store): Store {
        return object : Store {
            override val state: Flow
                get() = store.state
            override val currentState: State
                get() = store.currentState

            override suspend fun dispatch(action: Action) = coroutineScope {
                launch {
                    log(action)
                }
                store.dispatch(action)
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy