Fix property usage for ObservableItemProvider

This commit is contained in:
Alexander Nozik 2021-04-07 15:34:27 +03:00
parent 187094d942
commit 874a253292
4 changed files with 17 additions and 6 deletions

View File

@ -4,7 +4,7 @@ plugins {
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.4.0-dev-6" version = "0.4.0-dev-7"
} }
subprojects { subprojects {

View File

@ -414,8 +414,7 @@ public abstract interface class space/kscience/dataforge/meta/ObservableItemProv
} }
public final class space/kscience/dataforge/meta/ObservableItemProviderKt { public final class space/kscience/dataforge/meta/ObservableItemProviderKt {
public static final fun onChange (Lspace/kscience/dataforge/meta/ObservableItemProvider;Lkotlin/reflect/KProperty1;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)V public static synthetic fun useProperty$default (Lspace/kscience/dataforge/meta/ObservableItemProvider;Lkotlin/reflect/KProperty1;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
public static synthetic fun onChange$default (Lspace/kscience/dataforge/meta/ObservableItemProvider;Lkotlin/reflect/KProperty1;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
} }
public abstract interface class space/kscience/dataforge/meta/ReadOnlySpecification { public abstract interface class space/kscience/dataforge/meta/ReadOnlySpecification {

View File

@ -1,5 +1,6 @@
package space.kscience.dataforge.meta package space.kscience.dataforge.meta
import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.toName import space.kscience.dataforge.names.toName
import kotlin.reflect.KProperty1 import kotlin.reflect.KProperty1
@ -18,12 +19,21 @@ public interface ObservableItemProvider : ItemProvider {
public interface ItemPropertyProvider: ObservableItemProvider, MutableItemProvider public interface ItemPropertyProvider: ObservableItemProvider, MutableItemProvider
public fun <O : ObservableItemProvider, T : Any> O.onChange( /**
* Use the value of the property in a [callBack].
* The callback is called once immediately after subscription to pass the initial value.
*
* Optional [owner] property is used for
*/
@DFExperimental
public fun <O : ObservableItemProvider, T : Any> O.useProperty(
property: KProperty1<O, T?>, property: KProperty1<O, T?>,
owner: Any? = null, owner: Any? = null,
callBack: O.(T?) -> Unit, callBack: O.(T?) -> Unit,
) { ) {
onChange(null) { name, oldItem, newItem -> //Pass initial value.
callBack(property.get(this))
onChange(owner) { name, oldItem, newItem ->
if (name == property.name.toName() && oldItem != newItem) { if (name == property.name.toName() && oldItem != newItem) {
callBack(property.get(this)) callBack(property.get(this))
} }

View File

@ -1,8 +1,10 @@
package space.kscience.dataforge.meta package space.kscience.dataforge.meta
import space.kscience.dataforge.misc.DFExperimental
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@DFExperimental
class SchemeTest { class SchemeTest {
@Test @Test
fun testSchemeWrappingBeforeEdit(){ fun testSchemeWrappingBeforeEdit(){
@ -25,7 +27,7 @@ class SchemeTest {
fun testSchemeSubscription(){ fun testSchemeSubscription(){
val scheme = TestScheme.empty() val scheme = TestScheme.empty()
var flag: Int? = null var flag: Int? = null
scheme.onChange(TestScheme::a){a-> scheme.useProperty(TestScheme::a){a->
flag = a flag = a
} }
scheme.a = 2 scheme.a = 2