From 075280076d373dad01666397f7b857aad8450b7c Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Fri, 21 Jun 2019 15:37:03 +0200 Subject: Simplification of the Redux module. --- .../kotlin/pl/treksoft/kvision/redux/ReduxStore.kt | 30 ++++++++-------------- .../pl/treksoft/kvision/redux/ReduxStoreSpec.kt | 19 +++++++------- .../pl/treksoft/kvision/redux/StateBindingSpec.kt | 2 -- 3 files changed, 20 insertions(+), 31 deletions(-) (limited to 'kvision-modules') diff --git a/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt b/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt index 246ccef5..bfe7bc50 100644 --- a/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt +++ b/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt @@ -21,11 +21,7 @@ */ package pl.treksoft.kvision.redux -import kotlinx.serialization.ImplicitReflectionSerializer -import kotlinx.serialization.KSerializer -import kotlinx.serialization.serializer import pl.treksoft.kvision.KVManagerRedux -import pl.treksoft.kvision.utils.JSON import redux.RAction import redux.Reducer import redux.Store @@ -43,49 +39,45 @@ typealias ActionCreator = (Dispatch, GetState) -> Unit * @param initialState an initial state * @param middlewares a list of optional Redux JS middlewares */ -@UseExperimental(ImplicitReflectionSerializer::class) -inline fun createReduxStore( - noinline reducer: Reducer, +fun createReduxStore( + reducer: Reducer, initialState: S, vararg middlewares: dynamic ): ReduxStore { @Suppress("SpreadOperator") - return ReduxStore(reducer, initialState, S::class.serializer(), *middlewares) + return ReduxStore(reducer, initialState, *middlewares) } /** * A class implementing redux pattern backed by the original Redux JS library. - * It requires @Serializable state. * * @constructor Creates a Redux store with given reducer function and initial state. - * @param S redux state type (@Serializable) + * @param S redux state type * @param A redux action type * @param reducer a reducer function * @param initialState an initial state - * @param stateSerializer a serializer for the state type * @param middlewares a list of optional Redux JS middlewares */ class ReduxStore( reducer: Reducer, initialState: S, - val stateSerializer: KSerializer, vararg middlewares: dynamic ) { - private val store: Store + private val store: Store init { @Suppress("UnsafeCastFromDynamic") store = KVManagerRedux.createStore( - { s: String, a: RAction -> + { s: S, a: RAction -> @Suppress("UnsafeCastFromDynamic") if (a == undefined || (a.asDynamic().type is String && a.asDynamic().type.startsWith("@@"))) { s } else { @Suppress("UNCHECKED_CAST") - JSON.plain.stringify(stateSerializer, reducer(JSON.plain.parse(stateSerializer, s), a as A)) + reducer(s, a as A) } }, - JSON.plain.stringify(stateSerializer, initialState), + initialState, @Suppress("SpreadOperator") KVManagerRedux.compose(KVManagerRedux.applyMiddleware(KVManagerRedux.reduxThunk, *middlewares), rEnhancer()) ) @@ -95,7 +87,7 @@ class ReduxStore( * Returns the current state. */ fun getState(): S { - return JSON.plain.parse(stateSerializer, store.getState()) + return store.getState() } /** @@ -109,7 +101,7 @@ class ReduxStore( * Dispatches an asynchronous action function. */ fun dispatch(actionCreator: ActionCreator): WrapperAction { - return store.dispatch({ reduxDispatch: Dispatch, reduxGetState: GetState -> + return store.dispatch({ reduxDispatch: Dispatch, reduxGetState: GetState -> val newDispatch: Dispatch = { elem -> @Suppress("UnsafeCastFromDynamic") if (js("typeof elem === 'function'")) { @@ -118,7 +110,7 @@ class ReduxStore( reduxDispatch(elem) } } - actionCreator(newDispatch) { JSON.plain.parse(stateSerializer, reduxGetState()) } + actionCreator(newDispatch) { reduxGetState() } }) } diff --git a/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/ReduxStoreSpec.kt b/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/ReduxStoreSpec.kt index 3be83556..b45eb9dd 100644 --- a/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/ReduxStoreSpec.kt +++ b/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/ReduxStoreSpec.kt @@ -21,15 +21,13 @@ */ package test.pl.treksoft.kvision.redux -import kotlinx.serialization.Serializable import pl.treksoft.kvision.redux.createReduxStore import redux.RAction import test.pl.treksoft.kvision.SimpleSpec import kotlin.test.Test import kotlin.test.assertEquals -@Serializable -data class TestState(val counter: Int) +data class TestState(val counter: Int, val values: List) sealed class TestAction : RAction { object Inc : TestAction() @@ -38,10 +36,10 @@ sealed class TestAction : RAction { fun testReducer(state: TestState, action: TestAction): TestState = when (action) { is TestAction.Inc -> { - state.copy(counter = state.counter + 1) + state.copy(counter = state.counter + 1, values = state.values + state.counter) } is TestAction.Dec -> { - state.copy(counter = state.counter - 1) + state.copy(counter = state.counter - 1, values = state.values + state.counter) } } @@ -50,20 +48,21 @@ class ReduxStoreSpec : SimpleSpec { @Test fun getState() { run { - val store = createReduxStore(::testReducer, TestState(10)) - assertEquals(TestState(10), store.getState()) + val store = createReduxStore(::testReducer, TestState(10, listOf())) + assertEquals(TestState(10, listOf()), store.getState()) } } @Test fun dispatch() { run { - val store = createReduxStore(::testReducer, TestState(10)) + val store = createReduxStore(::testReducer, TestState(10, listOf())) store.dispatch(TestAction.Inc) store.dispatch(TestAction.Inc) store.dispatch(TestAction.Inc) store.dispatch(TestAction.Dec) - assertEquals(TestState(12), store.getState()) + store.dispatch(TestAction.Dec) + assertEquals(TestState(11, listOf(10, 11, 12, 13, 12)), store.getState()) } } @@ -71,7 +70,7 @@ class ReduxStoreSpec : SimpleSpec { fun subscribe() { run { var counter = 0 - val store = createReduxStore(::testReducer, TestState(10)) + val store = createReduxStore(::testReducer, TestState(10, listOf())) store.subscribe { counter++ } diff --git a/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/StateBindingSpec.kt b/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/StateBindingSpec.kt index 5204402d..ec32d0d6 100644 --- a/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/StateBindingSpec.kt +++ b/kvision-modules/kvision-redux/src/test/kotlin/test/pl/treksoft/kvision/redux/StateBindingSpec.kt @@ -21,7 +21,6 @@ */ package test.pl.treksoft.kvision.redux -import kotlinx.serialization.Serializable import pl.treksoft.kvision.html.Div.Companion.div import pl.treksoft.kvision.panel.Root import pl.treksoft.kvision.panel.SimplePanel @@ -33,7 +32,6 @@ import test.pl.treksoft.kvision.DomSpec import kotlin.browser.document import kotlin.test.Test -@Serializable data class State(val counter: Int) sealed class StateAction : RAction { -- cgit