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

main.redux.Helpers.kt Maven / Gradle / Ivy

package redux

import kotlinext.js.Object
import kotlinext.js.assign
import kotlinext.js.js
import kotlin.collections.set

val  Store.state: S get() = getState()

fun  combineReducers(reducers: Map>): Reducer = combineReducers(js {
    reducers.forEach { this[it.key] = it.value }
}.unsafeCast>())

fun  bindActionCreators(actionCreators: Map) -> A>, dispatch: (A) -> R): Map) -> R> {
    val result = mutableMapOf) -> R>()
    with(bindActionCreators(js {
        actionCreators.forEach { this[it.key] = it.value }
    }.unsafeCast>(), dispatch)) {
        Object.keys(this).forEach {
            result[it] = asDynamic()[it]
        }
    }
    return result
}

fun  rEnhancer(): Enhancer = { next ->
    { reducer, initialState ->
        fun wrapperReducer(reducer: Reducer): Reducer {
            return { state, action -> reducer(state, action.action) }
        }

        val store = (next.unsafeCast>())(wrapperReducer(reducer), initialState)
        assign(Object.assign(js {}, store)) {
            dispatch = { action: RAction ->
                val result = store.dispatch(js {
                    type = action::class.simpleName
                    this.action = action
                }.unsafeCast())
                result
            }
            replaceReducer = { nextReducer: Reducer ->
                store.replaceReducer(wrapperReducer(nextReducer))
            }
        }.unsafeCast>()
    }
}

external interface WrapperAction : Action {
    override val type: String
    val action: RAction
}

external interface RAction