kdux.StoreDsl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Kdux Show documentation
Show all versions of Kdux Show documentation
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.
package kdux
import kdux.dsl.StoreDslMenu
import kdux.tools.*
import org.mattshoe.shoebox.kdux.Enhancer
import org.mattshoe.shoebox.kdux.Middleware
import org.mattshoe.shoebox.kdux.Reducer
import org.mattshoe.shoebox.kdux.Store
/**
* Creates and configures a [Store] using a DSL.
*
* This function provides a DSL for configuring a [Store] with an initial state, a reducer,
* and optional middleware, enhancers, or a custom store creator.
*
* @param State The type representing the state managed by the store.
* @param Action The type representing the actions that can be dispatched to the store.
* @param initialState The initial state of the store.
* @param reducer The reducer that handles actions and updates the state.
* @param configuration A lambda function used to configure the store, such as adding middleware, enhancers, or a custom store creator.
* @return A fully configured [Store] instance.
*/
fun store(
initialState: State,
reducer: Reducer,
configuration: StoreDslMenu.() -> Unit = { }
): Store {
return StoreDslMenu(initialState, reducer)
.apply(configuration)
.builder
.apply {
KduxGlobal.loggers.forEach {
add(LoggingEnhancer(it))
}
KduxGlobal.performanceMonitors.forEach {
add(PerformanceEnhancer(it))
}
}
.build()
}
/**
* A DSL utility that creates a [Middleware] from a given function. This allows you to define
* middleware logic inline without needing to create a separate class.
*
* The middleware function intercepts actions as they are dispatched to the store, allowing you to
* perform side effects, modify the action, or block the action from reaching the reducer.
*
* @param function A suspend function that takes three parameters:
* - [store]: The [Store] instance managing the state and actions.
* - [action]: The action being dispatched.
* - [next]: A suspend function representing the next middleware or reducer in the chain. Calling `next(action)` passes the action to the next stage.
*
* @return A [Middleware] instance that applies the provided function to intercept and process actions.
*/
fun middleware(
function: suspend (
store: Store,
action: Action,
next: suspend (Action
) -> Unit
) -> Unit
): Middleware {
return object : Middleware {
override suspend fun apply(
store: Store,
action: Action,
next: suspend (Action) -> Unit
) {
function.invoke(store, action, next)
}
}
}
/**
* A DSL utility that creates an [Enhancer] from a given function. This allows you to define
* enhancer logic inline without needing to create a separate class.
*
* The enhancer function modifies the behavior of the store by wrapping it with additional functionality.
* This can include altering how actions are dispatched, adding new methods, or modifying how the state is accessed.
*
* @param function A function that takes a [Store] as a parameter and returns a modified [Store].
*
* @return An [Enhancer] instance that applies the provided function to modify or extend the store's behavior.
*/
fun enhancer(
function: (store: Store) -> Store
): Enhancer {
return object : Enhancer {
override fun enhance(store: Store): Store {
return function.invoke(store)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy