Proxy listeners of synched delegates (so they are removed when slot is removed)

This commit is contained in:
DBotThePony 2024-02-25 14:26:13 +07:00
parent 6f60a4cd24
commit 07c2353911
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 57 additions and 2 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons
projectVersion=2.9.7
projectVersion=2.9.8
guavaDepVersion=33.0.0
gsonDepVersion=2.8.9

View File

@ -138,8 +138,9 @@ class DelegateSyncher : Observer {
}
inner class Slot<V>(val delegate: ListenableDelegate<V>, val codec: StreamCodec<V>) : AbstractSlot(), ListenableDelegate<V> {
private val l = delegate.addListener(Runnable {
private val l = delegate.addListener(Consumer {
markDirty()
listeners.accept(it)
})
init {
@ -314,9 +315,36 @@ class DelegateSyncher : Observer {
}
private val l = delegate.addListener(listener)
private val listeners = CopyOnWriteArrayList<Listener>()
override fun remove0() {
l.remove()
listeners.clear()
}
private inner class Listener(val listener: ListenableSet.SetListener<V>): Listenable.L {
private var isRemoved = false
init {
listeners.add(this)
}
override fun remove() {
if (!isRemoved) {
isRemoved = true
listeners.remove(this)
}
}
}
fun addListener(listener: ListenableSet.SetListener<V>): Listenable.L {
check(!isRemoved.get()) { "This network slot was removed" }
return Listener(listener)
}
fun addListener(listener: Runnable): Listenable.L {
check(!isRemoved.get()) { "This network slot was removed" }
return Listener(ListenableSet.RunnableAdapter(listener))
}
override fun read(stream: DataInputStream) {
@ -391,9 +419,36 @@ class DelegateSyncher : Observer {
}
private val l = delegate.addListener(listener)
private val listeners = CopyOnWriteArrayList<Listener>()
override fun remove0() {
l.remove()
listeners.clear()
}
private inner class Listener(val listener: ListenableMap.MapListener<K, V>): Listenable.L {
private var isRemoved = false
init {
listeners.add(this)
}
override fun remove() {
if (!isRemoved) {
isRemoved = true
listeners.remove(this)
}
}
}
fun addListener(listener: ListenableMap.MapListener<K, V>): Listenable.L {
check(!isRemoved.get()) { "This network slot was removed" }
return Listener(listener)
}
fun addListener(listener: Runnable): Listenable.L {
check(!isRemoved.get()) { "This network slot was removed" }
return Listener(ListenableMap.RunnableAdapter(listener))
}
override fun read(stream: DataInputStream) {