Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use caffeine instead of pair set #3

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.noxcrew.interfaces.properties

import java.lang.ref.WeakReference
import java.util.concurrent.ConcurrentHashMap
import com.github.benmanes.caffeine.cache.Caffeine
import java.util.Queue
import java.util.concurrent.ConcurrentLinkedDeque

/** A trigger that delegates triggers to its listeners. */
public open class DelegateTrigger : Trigger {

private val updateListeners = ConcurrentHashMap.newKeySet<Pair<WeakReference<Any>, Any.() -> Unit>>()
private val updateListeners = Caffeine.newBuilder()
.weakKeys()
.build<Any, Queue<Any.() -> Unit>>()
.asMap()

override fun trigger() {
val iterator = updateListeners.iterator()
while (iterator.hasNext()) {
val (reference, consumer) = iterator.next()
val obj = reference.get()
if (obj == null) {
iterator.remove()
continue
}
obj.apply(consumer)
updateListeners.forEach { (_, listeners) ->
listeners.forEach { it() }
}
}

override fun <T : Any> addListener(reference: T, listener: T.() -> Unit) {
updateListeners.removeIf { it.first.get() == null }
updateListeners.add(WeakReference(reference) as WeakReference<Any> to listener as (Any.() -> Unit))
@Suppress("UNCHECKED_CAST")
updateListeners.computeIfAbsent(reference) {
ConcurrentLinkedDeque()
}.add(listener as (Any.() -> Unit))
}
}