From 25e56fcab092c5c6343e7a57e54e193f2cdbf4ae Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Fri, 30 Aug 2019 17:56:25 +0200 Subject: New module for Kotlin Redux. --- .../kotlin/pl/treksoft/kvision/redux/ReduxStore.kt | 110 +++++++++++++++++++++ .../pl/treksoft/kvision/redux/StateBinding.kt | 108 ++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt create mode 100644 kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt (limited to 'kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux') diff --git a/kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt b/kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt new file mode 100644 index 00000000..58cf86c6 --- /dev/null +++ b/kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.redux + +import org.reduxkotlin.ActionTypes +import org.reduxkotlin.Middleware +import org.reduxkotlin.Store +import org.reduxkotlin.applyMiddleware +import org.reduxkotlin.createStore +import org.reduxkotlin.createThunk +import org.reduxkotlin.createThunkMiddleware + +interface RAction +typealias ReducerFun = (S, A) -> S +typealias Dispatch = (A) -> Unit +typealias GetState = () -> S +typealias ActionCreator = (Dispatch, GetState) -> Unit + +/** + * An inline helper function for creating Redux store. + * + * @param reducer a reducer function + * @param initialState an initial state + * @param middlewares a list of optional Redux JS middlewares + */ +fun createReduxStore( + reducer: ReducerFun, + initialState: S, + vararg middlewares: Middleware +): ReduxStore { + @Suppress("SpreadOperator") + return ReduxStore(reducer, initialState, *middlewares) +} + +/** + * A class implementing redux pattern backed by the Redux Kotlin library. + * + * @constructor Creates a Redux store with given reducer function and initial state. + * @param S redux state type + * @param A redux action type + * @param reducer a reducer function + * @param initialState an initial state + * @param middlewares a list of optional Redux Kotlin middlewares + */ +class ReduxStore( + reducer: ReducerFun, + initialState: S, + vararg middlewares: Middleware +) { + @Suppress("UNCHECKED_CAST") + private val store: Store = createStore({ s: S, a: Any -> + if (a == ActionTypes.INIT || a == ActionTypes.REPLACE) { + s + } else { + reducer(s, a as A) + } + }, initialState, applyMiddleware(createThunkMiddleware(), *middlewares)) + + /** + * Returns the current state. + */ + fun getState(): S { + @Suppress("UNCHECKED_CAST") + return store.getState() + } + + /** + * Dispatches a synchronous action object. + */ + fun dispatch(action: A) { + store.dispatch(action) + } + + /** + * Dispatches an asynchronous action function. + */ + fun dispatch(actionCreator: ActionCreator) { + @Suppress("UNCHECKED_CAST") + val thunk = createThunk { dispatch, getState, _ -> actionCreator(dispatch as ((A) -> Unit), getState) } + store.dispatch(thunk) + } + + /** + * Subscribes a client for the change state notifications. + */ + fun subscribe(listener: (S) -> Unit): () -> Unit { + return store.subscribe { + listener(getState()) + } + } +} diff --git a/kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt b/kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt new file mode 100644 index 00000000..a980a79a --- /dev/null +++ b/kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2017-present Robert Jaros + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package pl.treksoft.kvision.redux + +import pl.treksoft.kvision.core.Container +import pl.treksoft.kvision.core.Widget + +/** + * A class which binds the redux store with the given container. + * + * @constructor Creates StateBinding which binds the redux store with the given container. + * @param S redux state type + * @param A redux action type + * @param CONT container type + * @param store a redux store + * @param container a container + * @param factory a function which re-creates the view based on the given state + */ +class StateBinding( + store: ReduxStore, + private val container: CONT, + private val factory: (CONT.(S) -> CONTENT) +) : Widget(setOf()) { + + init { + update(store.getState()) + store.subscribe { update(it) } + } + + private var updateState: ((S, CONTENT) -> Unit)? = null + private var content: CONTENT? = null + + /** + * Updates view from the current state. + */ + @Suppress("ComplexMethod") + fun update(state: S) { + singleRender { + if (updateState == null || content == null) { + container.removeAll() + container.add(this) + content = container.factory(state) + } else { + content?.let { + updateState?.invoke(state, it) + } + } + } + } + + private fun setUpdateState(updateState: (S, CONTENT) -> Unit) { + this.updateState = updateState + } + + companion object { + /** + * DSL builder extension function. + * + * It takes the same parameters as the constructor of the built component. + */ + fun CONT.stateBinding( + store: ReduxStore, + factory: (CONT.(S) -> Unit) + ): StateBinding { + return StateBinding(store, this, factory) + } + + /** + * DSL builder extension function for updateable redux content. + * + * It takes the same parameters as the constructor of the built component. + */ + fun CONT.stateUpdate( + store: ReduxStore, + factory: (CONT.(S) -> CONTENT) + ): Updateable { + return Updateable(StateBinding(store, this, factory)::setUpdateState) + } + } +} + +/** + * A helper class for updateable redux content. + */ +class Updateable(private val setUpdateState: ((S, CONTENT) -> Unit) -> Unit) { + infix fun updateWith(updateState: (S, CONTENT) -> Unit) { + setUpdateState(updateState) + } +} -- cgit