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

commonMain.org.reduxkotlin.Definitions.kt Maven / Gradle / Ivy

The newest version!
package org.reduxkotlin


/**
 * See also https://github.com/reactjs/redux/blob/master/docs/Glossary.md#reducer
 */
typealias Reducer = (state: State, action: Any) -> State

/**
 * Reducer for a particular subclass of actions.  Useful for Sealed classes &
 * exhaustive when statements.  See [reducerForActionType].
 */
typealias ReducerForActionType = (state: TState, action: TAction) -> TState

typealias GetState = () -> State
typealias StoreSubscriber = () -> Unit
typealias StoreSubscription = () -> Unit
typealias Dispatcher = (Any) -> Any
// Enhancer is type Any? to avoid a circular dependency of types.
typealias StoreCreator = (
    reducer: Reducer,
    initialState: State,
    enhancer: Any?
) -> Store

/**
 * Take a store creator and return a new enhanced one
 * see https://github.com/reactjs/redux/blob/master/docs/Glossary.md#store-enhancer
 */
typealias StoreEnhancer = (StoreCreator) -> StoreCreator

/**
 *  https://github.com/reactjs/redux/blob/master/docs/Glossary.md#middleware
 */
typealias Middleware = (store: Store) -> (next: Dispatcher) -> (action: Any) -> Any


interface Store {
    val getState: GetState
    var dispatch: Dispatcher
    val subscribe: (StoreSubscriber) -> StoreSubscription
    val replaceReducer: (Reducer) -> Unit
    val state: State
        get() = getState()
}

/**
 * Convenience function for creating a [Middleware]
 * usage:
 *    val myMiddleware = middleware { store, next, action -> doStuff() }
 */
fun  middleware(dispatch: (Store, next: Dispatcher, action: Any) -> Any): Middleware =
    { store ->
        { next ->
            { action: Any ->
                dispatch(store, next, action)
            }
        }
    }


/**
 * Convenience function for creating a [ReducerForActionType]
 * usage:
 *   sealed class LoginScreenAction
 *   data class LoginComplete(val user: User): LoginScreenAction()
 *
 *   val loginReducer = reducerForActionType { state, action ->
 *       when(action) {
 *           is LoginComplete -> state.copy(user = action.user)
 *       }
 *   }
 *
 *   sealed class FeedScreenAction
 *   data class FeedLoaded(val items: FeedItems): FeedScreenAction
 *   data class FeedLoadError(val msg: String): FeedScreenAction
 *
 *   val feedReducer = reducerForActionType { state, action ->
 *       when(action) {
 *          is FeedLoaded -> state.copy(feedItems = action.items)
 *          is FeedLoadError -> state.copy(errorMsg = action.msg)
 *       }
 *   }
 *
 *   val rootReducer = combineReducers(loginReducer, feedReducer)
 *   val store = createStore(rootReducer, AppState())
 *      **or**
 *   val store = createThreadSafeStore(rootReducer, AppState())
 */
inline fun  reducerForActionType(
    crossinline reducer: ReducerForActionType
): Reducer =
    { state, action ->
        when (action) {
            is TAction -> reducer(state, action)
            else -> state
        }
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy