diff options
author | Robert Jaros <rjaros@finn.pl> | 2020-05-20 17:12:56 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2020-05-20 17:12:56 +0200 |
commit | ef1abc3b975e1c77fc8663c5e5e95769fc934551 (patch) | |
tree | 343236378e2f24514635fb5e892043a054c86170 /kvision-modules/kvision-react/src/main/kotlin/pl | |
parent | 35628c364d9768a5589559b99cb698ebc2b56eba (diff) | |
download | kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.tar.gz kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.tar.bz2 kvision-ef1abc3b975e1c77fc8663c5e5e95769fc934551.zip |
Make all stateful components implement ObservableState interface
Diffstat (limited to 'kvision-modules/kvision-react/src/main/kotlin/pl')
-rw-r--r-- | kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt b/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt index 7424159f..79f4d1a0 100644 --- a/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt +++ b/kvision-modules/kvision-react/src/main/kotlin/pl/treksoft/kvision/react/React.kt @@ -27,6 +27,7 @@ import org.w3c.dom.HTMLElement import pl.treksoft.kvision.KVManagerReact import pl.treksoft.kvision.core.Container import pl.treksoft.kvision.core.Widget +import pl.treksoft.kvision.state.ObservableState import pl.treksoft.kvision.utils.set import react.RBuilder import react.dom.render as ReactRender @@ -41,12 +42,15 @@ class React<S>( state: S, classes: Set<String> = setOf(), private val builder: RBuilder.(getState: () -> S, changeState: ((S) -> S) -> Unit) -> Unit -) : Widget(classes) { +) : Widget(classes), ObservableState<S> { + + private val observers = mutableListOf<(S) -> Unit>() var state = state set(value) { field = value refreshFunction?.invoke() + observers.forEach { it(state) } } private var refreshFunction: (() -> Unit)? = null @@ -76,6 +80,16 @@ class React<S>( KVManagerReact.reactDom.unmountComponentAtNode(it) } } + + override fun getState(): S = state + + override fun subscribe(observer: (S) -> Unit): () -> Unit { + observers += observer + observer(state) + return { + observers -= observer + } + } } /** |