Void/Unit subscripable

This commit is contained in:
DBotThePony 2024-01-21 23:33:03 +07:00
parent 9ad1c8e82a
commit d18399795b
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -66,6 +66,44 @@ interface ISubscriptable<V> {
}
}
interface IUnitSubscripable : ISubscriptable<Unit> {
fun addListener(listener: Runnable): ISubscriptable.L
override fun addListener(listener: Consumer<Unit>): ISubscriptable.L {
return addListener(Runnable { listener.accept(Unit) })
}
class Impl : IUnitSubscripable, Runnable {
private inner class L(val callback: Runnable) : ISubscriptable.L {
private var isRemoved = false
init {
subscribers.add(this)
}
override fun remove() {
if (!isRemoved) {
isRemoved = true
queue.add(this)
}
}
}
private val subscribers = ReferenceLinkedOpenHashSet<L>(0)
private val queue = ReferenceArraySet<L>(0)
override fun addListener(listener: Runnable): ISubscriptable.L {
return L(listener)
}
override fun run() {
queue.forEach { subscribers.remove(it) }
queue.clear()
subscribers.forEach { it.callback.run() }
}
}
}
interface IFloatSubcripable : ISubscriptable<Float> {
@Deprecated("Use type specific listener")
override fun addListener(listener: Consumer<Float>): ISubscriptable.L {