aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-10-13 20:04:35 +0200
committerRobert Jaros <rjaros@finn.pl>2019-10-13 20:04:35 +0200
commitfb41b9fbdb8e298d5cb69168dd53a6945a6348b1 (patch)
tree2713808a67e6360c28244dddaa6de202a3dfc9d3
parent780c340a481676e27398dccf4503e8f0b9c63060 (diff)
downloadkvision-fb41b9fbdb8e298d5cb69168dd53a6945a6348b1.tar.gz
kvision-fb41b9fbdb8e298d5cb69168dd53a6945a6348b1.tar.bz2
kvision-fb41b9fbdb8e298d5cb69168dd53a6945a6348b1.zip
Allow to unsubscribe from the ObservableState.
-rw-r--r--kvision-modules/kvision-redux-kotlin/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt6
-rw-r--r--kvision-modules/kvision-redux/src/main/kotlin/pl/treksoft/kvision/redux/ReduxStore.kt6
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/state/ObservableList.kt5
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt2
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt9
5 files changed, 19 insertions, 9 deletions
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
index a37f1ac9..c4889567 100644
--- 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
@@ -96,10 +96,10 @@ class ReduxStore<S : Any, A : RAction>(
store.dispatch(thunk)
}
- override fun subscribe(observer: (S) -> Unit) {
- store.subscribe {
+ override fun subscribe(observer: (S) -> Unit): () -> Unit {
+ observer(getState())
+ return store.subscribe {
observer(getState())
}
- observer(getState())
}
}
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 93c4f6a9..2fd0b744 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
@@ -112,10 +112,10 @@ class ReduxStore<S : Any, A : RAction>(
})
}
- override fun subscribe(observer: (S) -> Unit) {
- store.subscribe {
+ override fun subscribe(observer: (S) -> Unit): () -> Unit {
+ observer(getState())
+ return store.subscribe {
observer(getState())
}
- observer(getState())
}
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/state/ObservableList.kt b/src/main/kotlin/pl/treksoft/kvision/state/ObservableList.kt
index 92109814..7d41e05c 100644
--- a/src/main/kotlin/pl/treksoft/kvision/state/ObservableList.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/state/ObservableList.kt
@@ -35,9 +35,12 @@ interface ObservableList<T> : MutableList<T>, ObservableState<List<T>> {
class ObservableListWrapper<T>(val mutableList: MutableList<T> = mutableListOf()) : MutableList<T>, ObservableList<T>,
ObservableState<List<T>> {
- override fun subscribe(observer: (List<T>) -> Unit) {
+ override fun subscribe(observer: (List<T>) -> Unit): () -> Unit {
onUpdate += observer
observer(this)
+ return {
+ onUpdate -= observer
+ }
}
override val onUpdate: MutableCollection<(MutableList<T>) -> Unit> = mutableListOf()
diff --git a/src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt b/src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt
index 9dc9af0d..f1cba7f0 100644
--- a/src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt
@@ -28,5 +28,5 @@ interface ObservableState<S> {
/**
* Subscribe for the state change notifications.
*/
- fun subscribe(observer: (S) -> Unit)
+ fun subscribe(observer: (S) -> Unit): () -> Unit
}
diff --git a/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt b/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt
index 7df82adc..adb7d3be 100644
--- a/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt
+++ b/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt
@@ -22,8 +22,10 @@ class StateBinding<S : Any, CONT : Container, CONTENT>(
private val factory: (CONT.(S) -> CONTENT)
) : Widget(setOf()) {
+ private val unsubscribe: () -> Unit
+
init {
- observableState.subscribe { update(it) }
+ unsubscribe = observableState.subscribe { update(it) }
}
private var updateState: ((S, CONTENT) -> Unit)? = null
@@ -51,6 +53,11 @@ class StateBinding<S : Any, CONT : Container, CONTENT>(
this.updateState = updateState
}
+ override fun dispose() {
+ unsubscribe()
+ super.dispose()
+ }
+
companion object {
/**
* DSL builder extension function.