diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt index ef265f0d..2e6f3452 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/MetaRef.kt @@ -7,6 +7,7 @@ import space.kscience.dataforge.meta.descriptors.MetaDescriptorBuilder import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.asName +import space.kscience.dataforge.names.startsWith import kotlin.properties.PropertyDelegateProvider import kotlin.properties.ReadOnlyProperty @@ -35,6 +36,24 @@ public operator fun MutableMetaProvider.set(ref: MetaRef, value: T) { set(ref.name, ref.converter.convert(value)) } +/** + * Observe changes to specific property via given [ref]. + * + * This listener should be removed in a same way as [ObservableMeta.onChange]. + * + * @param callback an action to be performed on each change of item. Null means that the item is not present or malformed. + */ +@DFExperimental +public fun ObservableMeta.onValueChange(owner: Any?, ref: MetaRef, callback: (T?) -> Unit) { + onChange(owner) { name -> + if (name.startsWith(ref.name)) { + get(name)?.let { value -> + callback(ref.converter.readOrNull(value)) + } + } + } +} + /** * Remove a node corresponding to [ref] from a mutable provider if it exists */