Add synchronization to Config listeners

This commit is contained in:
Alexander Nozik 2020-12-13 19:58:20 +03:00
parent e317b67a48
commit 4c5f0e9e43
4 changed files with 6 additions and 4 deletions

View File

@ -2,7 +2,7 @@ plugins {
id("ru.mipt.npm.project")
}
val dataforgeVersion by extra("0.2.1-dev-3")
val dataforgeVersion by extra("0.2.1-dev-4")
val bintrayRepo by extra("dataforge")
val githubProject by extra("dataforge-core")

View File

@ -10,6 +10,7 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.collections.set
import kotlin.jvm.Synchronized
//TODO add validator to configuration
@ -31,6 +32,7 @@ public class Config() : AbstractMutableMeta<Config>(), ObservableMeta {
private val listeners = HashSet<MetaListener>()
@Synchronized
private fun itemChanged(name: Name, oldItem: MetaItem<*>?, newItem: MetaItem<*>?) {
listeners.forEach { it.action(name, oldItem, newItem) }
}
@ -38,6 +40,7 @@ public class Config() : AbstractMutableMeta<Config>(), ObservableMeta {
/**
* Add change listener to this meta. Owner is declared to be able to remove listeners later. Listener without owner could not be removed
*/
@Synchronized
override fun onChange(owner: Any?, action: (Name, MetaItem<*>?, MetaItem<*>?) -> Unit) {
listeners.add(MetaListener(owner, action))
}
@ -45,6 +48,7 @@ public class Config() : AbstractMutableMeta<Config>(), ObservableMeta {
/**
* Remove all listeners belonging to given owner
*/
@Synchronized
override fun removeListener(owner: Any?) {
listeners.removeAll { it.owner === owner }
}

View File

@ -4,8 +4,6 @@ import hep.dataforge.names.*
public interface MutableMeta<out M : MutableMeta<M>> : MetaNode<M>, MutableItemProvider {
override val items: Map<NameToken, MetaItem<M>>
// fun onChange(owner: Any? = null, action: (Name, MetaItem<*>?, MetaItem<*>?) -> Unit)
// fun removeListener(owner: Any? = null)
}
/**

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists