From 822ce8ad4355c3673c0c2debc3df7abb64da5386 Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Sun, 24 Mar 2019 03:23:38 +0100 Subject: Experimental updatable redux content. --- .../pl/treksoft/kvision/redux/StateBinding.kt | 49 ++++++++++++++++++---- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft') diff --git a/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt b/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt index 55fcdb34..383e734e 100644 --- a/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt +++ b/kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/StateBinding.kt @@ -36,30 +36,42 @@ import redux.RAction * @param container a container * @param factory a function which re-creates the view based on the given state */ -class StateBinding( +class StateBinding( store: ReduxStore, private val container: CONT, - private val factory: (CONT.(S) -> Unit) + private val factory: (CONT.(S) -> CONTENT) ) : Widget(setOf()) { init { - container.add(this) - container.factory(store.getState()) + 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 { - container.removeAll() - container.add(this) - container.factory(state) + 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. @@ -69,8 +81,29 @@ class StateBinding( fun CONT.stateBinding( store: ReduxStore, factory: (CONT.(S) -> Unit) - ): StateBinding { + ): 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